From a35a3a5f4cb634383da343e69eba4be5624098c0 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Thu, 29 Aug 2024 16:33:09 +0200 Subject: [PATCH 1/3] fix: import `graphql` lazily --- src/core/utils/internal/parseGraphQLRequest.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/utils/internal/parseGraphQLRequest.ts b/src/core/utils/internal/parseGraphQLRequest.ts index 62fa55379..4435da175 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,9 @@ export function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery { } } -function parseQuery(query: string): ParsedGraphQLQuery | Error { +async function parseQuery(query: string): Promise { + const { parse } = await import('graphql') + try { const ast = parse(query) return parseDocumentNode(ast) @@ -181,7 +182,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) From 38618b9ab26dcb1a0ff575d3194c71d455ba20ef Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Thu, 29 Aug 2024 16:33:43 +0200 Subject: [PATCH 2/3] fix: add `graphql` to "peerDependencies" --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 572258d1b..e9e8da268 100644 --- a/package.json +++ b/package.json @@ -200,6 +200,7 @@ "webpack-http-server": "^0.5.0" }, "peerDependencies": { + "graphql": ">=16.8.0", "typescript": ">= 4.7.x" }, "peerDependenciesMeta": { From 9393d4b647d9c8ce93849444c7efd5b744901a85 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Thu, 29 Aug 2024 16:58:48 +0200 Subject: [PATCH 3/3] fix(parseGraphQLRequest): add meaningful error message --- src/core/utils/internal/parseGraphQLRequest.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/utils/internal/parseGraphQLRequest.ts b/src/core/utils/internal/parseGraphQLRequest.ts index 4435da175..81f6b1097 100644 --- a/src/core/utils/internal/parseGraphQLRequest.ts +++ b/src/core/utils/internal/parseGraphQLRequest.ts @@ -40,7 +40,12 @@ export function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery { } async function parseQuery(query: string): Promise { - const { parse } = await import('graphql') + 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)