Skip to content

Commit

Permalink
refactor: migration teams adapters to urql
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBastin committed Feb 4, 2022
1 parent 4836948 commit eae94e3
Show file tree
Hide file tree
Showing 15 changed files with 534 additions and 364 deletions.
105 changes: 104 additions & 1 deletion packages/hoppscotch-app/helpers/backend/GQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import * as E from "fp-ts/Either"
import * as TE from "fp-ts/TaskEither"
import { pipe, constVoid } from "fp-ts/function"
import { Source, subscribe, pipe as wonkaPipe, onEnd } from "wonka"
import { Subject } from "rxjs"
import { keyDefs } from "./caching/keys"
import { optimisticDefs } from "./caching/optimistic"
import { updatesDef } from "./caching/updates"
Expand Down Expand Up @@ -122,7 +123,6 @@ const createHoppClient = () =>
fetchExchange,
subscriptionExchange({
forwardSubscription: (operation) =>
// @ts-expect-error: An issue with the Urql typing
subscriptionClient.request(operation),
}),
],
Expand All @@ -145,6 +145,11 @@ type UseQueryOptions<T = any, V = object> = {
pollDuration?: number | undefined
}

type RunQueryOptions<T = any, V = object> = {
query: TypedDocumentNode<T, V>
variables?: V
}

/**
* A wrapper type for defining errors possible in a GQL operation
*/
Expand All @@ -158,6 +163,104 @@ export type GQLError<T extends string> =
error: T
}

export const runGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
args: RunQueryOptions<DocType, DocVarType>
): Promise<E.Either<GQLError<DocErrorType>, DocType>> => {
const request = createRequest<DocType, DocVarType>(args.query, args.variables)
const source = client.value.executeQuery(request)

return new Promise((resolve) => {
const sub = wonkaPipe(
source,
subscribe((res) => {
if (sub) {
sub.unsubscribe()
}

pipe(
// The target
res.data as DocType | undefined,
// Define what happens if data does not exist (it is an error)
E.fromNullable(
pipe(
// Take the network error value
res.error?.networkError,
// If it null, set the left to the generic error name
E.fromNullable(res.error?.message),
E.match(
// The left case (network error was null)
(gqlErr) =>
<GQLError<DocErrorType>>{
type: "gql_error",
error: parseGQLErrorString(gqlErr ?? "") as DocErrorType,
},
// The right case (it was a GraphQL Error)
(networkErr) =>
<GQLError<DocErrorType>>{
type: "network_error",
error: networkErr,
}
)
)
),
resolve
)
})
)
})
}

export const runGQLSubscription = <
DocType,
DocVarType,
DocErrorType extends string
>(
args: RunQueryOptions<DocType, DocVarType>
) => {
const result$ = new Subject<E.Either<GQLError<DocErrorType>, DocType>>()

const source = client.value.executeSubscription(
createRequest(args.query, args.variables)
)

wonkaPipe(
source,
subscribe((res) => {
result$.next(
pipe(
// The target
res.data as DocType | undefined,
// Define what happens if data does not exist (it is an error)
E.fromNullable(
pipe(
// Take the network error value
res.error?.networkError,
// If it null, set the left to the generic error name
E.fromNullable(res.error?.message),
E.match(
// The left case (network error was null)
(gqlErr) =>
<GQLError<DocErrorType>>{
type: "gql_error",
error: parseGQLErrorString(gqlErr ?? "") as DocErrorType,
},
// The right case (it was a GraphQL Error)
(networkErr) =>
<GQLError<DocErrorType>>{
type: "network_error",
error: networkErr,
}
)
)
)
)
)
})
)

return result$
}

export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
_args: UseQueryOptions<DocType, DocVarType>
) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query GetCollectionChildren($collectionID: ID!) {
collection(collectionID: $collectionID) {
children {
id
title
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query GetCollectionRequests($collectionID: ID!, $cursor: ID) {
requestsInCollection(collectionID: $collectionID, cursor: $cursor) {
id
title
request
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query GetTeamMembers($teamID: ID!, $cursor: ID) {
team(teamID: $teamID) {
members(cursor: $cursor) {
membershipID
user {
uid
email
}
role
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query RootCollectionsOfTeam($teamID: ID!, $cursor: ID) {
rootCollectionsOfTeam(teamID: $teamID, cursor: $cursor) {
id
title
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
subscription TeamCollectionAdded($teamID: ID!) {
teamCollectionAdded(teamID: $teamID) {
id
title
parent {
id
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
subscription TeamCollectionRemoved($teamID: ID!) {
teamCollectionRemoved(teamID: $teamID)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
subscription TeamCollectionUpdated($teamID: ID!) {
teamCollectionUpdated(teamID: $teamID) {
id
title
parent {
id
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
subscription TeamMemberAdded($teamID: ID!) {
teamMemberAdded(teamID: $teamID) {
membershipID
user {
uid
email
}
role
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
subscription TeamMemberUpdated($teamID: ID!) {
teamMemberUpdated(teamID: $teamID) {
membershipID
user {
uid
email
}
role
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
subscription TeamRequestAdded($teamID: ID!) {
teamRequestAdded(teamID: $teamID) {
id
collectionID
request
title
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
subscription TeamRequestDeleted($teamID: ID!) {
teamRequestDeleted(teamID: $teamID)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
subscription TeamRequestUpdated($teamID: ID!) {
teamRequestUpdated(teamID: $teamID) {
id
collectionID
request
title
}
}
Loading

0 comments on commit eae94e3

Please sign in to comment.