Skip to content

Commit

Permalink
feat: declare graphql.subscription type
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 14, 2024
1 parent c40eac6 commit d7dd3ac
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/core/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,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 +90,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 +103,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 +115,34 @@ const standardGraphQLHandlers = {
*
* @see {@link https://mswjs.io/docs/api/graphql#graphloperationresolver `graphql.operation()` API reference}
*/
operation: GraphQLOperationHandler
}

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

function createGraphQLLink(url: Path): typeof standardGraphQLHandlers {
export interface GraphQLLink extends GraphQLHandlers {
/**
* Intercepts a GraphQL subscription by its name.
*
* @example
* graphql.subscription('OnPostAdded', resolver)
*/
subscription: GraphQLRequestHandler
}

function createGraphQLLink(url: Path): GraphQLLink {
return {
operation: createGraphQLOperationHandler(url),
query: createScopedGraphQLHandler('query' as OperationTypeNode, url),
mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, url),
subscription: createScopedGraphQLHandler(
'subscription' as OperationTypeNode,
url,
),
operation: createGraphQLOperationHandler(url),
}
}

Expand Down

0 comments on commit d7dd3ac

Please sign in to comment.