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

fix: import graphql lazily #2250

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading