Skip to content

Commit

Permalink
fix: import graphql lazily (mswjs#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Aug 29, 2024
1 parent 8a9568a commit 1799e06
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/core/utils/internal/parseGraphQLRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
OperationDefinitionNode,
OperationTypeNode,
} from 'graphql'
import { parse } from 'graphql'
import type { GraphQLVariables } from '../../handlers/GraphQLHandler'
import { toPublicUrl } from '../request/toPublicUrl'
import { devUtils } from './devUtils'
Expand Down Expand Up @@ -40,7 +39,14 @@ export function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery {
}
}

function parseQuery(query: string): ParsedGraphQLQuery | Error {
async function parseQuery(query: string): Promise<ParsedGraphQLQuery | Error> {
const { parse } = await import('graphql').catch((error) => {
devUtils.error(
'Failed to parse a GraphQL query: cannot import the "graphql" module. Please make sure you install it if you wish to intercept GraphQL requests. See the original import error below.',
)
throw error
})

try {
const ast = parse(query)
return parseDocumentNode(ast)
Expand Down Expand Up @@ -181,7 +187,7 @@ export async function parseGraphQLRequest(
}

const { query, variables } = input
const parsedResult = parseQuery(query)
const parsedResult = await parseQuery(query)

if (parsedResult instanceof Error) {
const requestPublicUrl = toPublicUrl(request.url)
Expand Down

0 comments on commit 1799e06

Please sign in to comment.