diff --git a/src/core/utils/internal/parseGraphQLRequest.ts b/src/core/utils/internal/parseGraphQLRequest.ts index 62fa55379..81f6b1097 100644 --- a/src/core/utils/internal/parseGraphQLRequest.ts +++ b/src/core/utils/internal/parseGraphQLRequest.ts @@ -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' @@ -40,7 +39,14 @@ export function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery { } } -function parseQuery(query: string): ParsedGraphQLQuery | Error { +async function parseQuery(query: string): Promise { + 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) @@ -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)