Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support GraphQL subscriptions #2357

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
"fs-extra": "^11.2.0",
"fs-teardown": "^0.3.0",
"glob": "^11.0.0",
"graphql-ws": "^5.16.0",
"jsdom": "^25.0.1",
"json-bigint": "^1.0.0",
"lint-staged": "^15.2.10",
Expand Down
37 changes: 25 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 48 additions & 18 deletions src/core/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DocumentNode, OperationTypeNode } from 'graphql'
import { DocumentNode, OperationTypeNode } from 'graphql'
import {
ResponseResolver,
RequestHandlerOptions,
Expand All @@ -13,6 +13,12 @@ import {
GraphQLQuery,
} from './handlers/GraphQLHandler'
import type { Path } from './utils/matching/matchRequestUrl'
import {
GraphQLPubsub,
GraphQLInternalPubsub,
createGraphQLSubscriptionHandler,
GraphQLSubscriptionHandler,
} from './handlers/GraphQLSubscriptionHandler'

export interface TypedDocumentNode<
Result = { [key: string]: any },
Expand Down Expand Up @@ -62,22 +68,24 @@ function createScopedGraphQLHandler(
}
}

function createGraphQLOperationHandler(url: Path) {
return <
Query extends GraphQLQuery = GraphQLQuery,
Variables extends GraphQLVariables = GraphQLVariables,
>(
resolver: ResponseResolver<
GraphQLResolverExtras<Variables>,
null,
GraphQLResponseBody<Query>
>,
) => {
export type GraphQLOperationHandler = <
Query extends GraphQLQuery = GraphQLQuery,
Variables extends GraphQLVariables = GraphQLVariables,
>(
resolver: ResponseResolver<
GraphQLResolverExtras<Variables>,
null,
GraphQLResponseBody<Query>
>,
) => GraphQLHandler

function createGraphQLOperationHandler(url: Path): GraphQLOperationHandler {
return (resolver) => {
return new GraphQLHandler('all', new RegExp('.*'), url, resolver)
}
}

const standardGraphQLHandlers = {
export interface GraphQLHandlers {
/**
* Intercepts a GraphQL query by a given name.
*
Expand All @@ -88,7 +96,7 @@ const standardGraphQLHandlers = {
*
* @see {@link https://mswjs.io/docs/api/graphql#graphqlqueryqueryname-resolver `graphql.query()` API reference}
*/
query: createScopedGraphQLHandler('query' as OperationTypeNode, '*'),
query: GraphQLRequestHandler

/**
* Intercepts a GraphQL mutation by its name.
Expand All @@ -101,7 +109,7 @@ const standardGraphQLHandlers = {
* @see {@link https://mswjs.io/docs/api/graphql#graphqlmutationmutationname-resolver `graphql.query()` API reference}
*
*/
mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, '*'),
mutation: GraphQLRequestHandler

/**
* Intercepts any GraphQL operation, regardless of its type or name.
Expand All @@ -113,14 +121,36 @@ const standardGraphQLHandlers = {
*
* @see {@link https://mswjs.io/docs/api/graphql#graphloperationresolver `graphql.operation()` API reference}
*/
operation: GraphQLOperationHandler
}

const standardGraphQLHandlers: GraphQLHandlers = {
query: createScopedGraphQLHandler(OperationTypeNode.QUERY, '*'),
mutation: createScopedGraphQLHandler(OperationTypeNode.MUTATION, '*'),
operation: createGraphQLOperationHandler('*'),
}

function createGraphQLLink(url: Path): typeof standardGraphQLHandlers {
export interface GraphQLLink extends GraphQLHandlers {
pubsub: GraphQLPubsub

/**
* Intercepts a GraphQL subscription by its name.
*
* @example
* graphql.subscription('OnPostAdded', resolver)
*/
subscription: GraphQLSubscriptionHandler
}

function createGraphQLLink(url: Path): GraphQLLink {
const internalPubSub = new GraphQLInternalPubsub(url)

return {
query: createScopedGraphQLHandler(OperationTypeNode.QUERY, url),
mutation: createScopedGraphQLHandler(OperationTypeNode.MUTATION, url),
subscription: createGraphQLSubscriptionHandler(internalPubSub),
pubsub: internalPubSub.pubsub,
operation: createGraphQLOperationHandler(url),
query: createScopedGraphQLHandler('query' as OperationTypeNode, url),
mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, url),
}
}

Expand Down
Loading
Loading