From ebe786c6317bfa6ff03e91609ee55cd0ecb697c5 Mon Sep 17 00:00:00 2001 From: John Daly Date: Mon, 1 Aug 2022 13:20:01 -0700 Subject: [PATCH 1/2] feat: move 'graphql' to peerDependencies. Add support for v15 of 'graphql' --- package.json | 3 ++- src/graphql.ts | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index bc9e08bd4..0d7a3d264 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,6 @@ "chalk": "4.1.1", "chokidar": "^3.4.2", "cookie": "^0.4.2", - "graphql": "^16.3.0", "headers-polyfill": "^3.0.4", "inquirer": "^8.2.0", "is-node-process": "^1.0.1", @@ -116,6 +115,7 @@ "eslint-plugin-prettier": "^3.4.0", "fs-extra": "^10.0.0", "fs-teardown": "^0.3.0", + "graphql": "^16.3.0", "jest": "26", "json-bigint": "^1.0.0", "lint-staged": "^11.0.1", @@ -134,6 +134,7 @@ "webpack-dev-server": "^3.11.2" }, "peerDependencies": { + "graphql": "^15.0.0 || ^16.0.0", "typescript": ">= 4.2.x <= 4.7.x" }, "peerDependenciesMeta": { diff --git a/src/graphql.ts b/src/graphql.ts index 94f46723c..e74338ec9 100644 --- a/src/graphql.ts +++ b/src/graphql.ts @@ -83,7 +83,7 @@ const standardGraphQLHandlers = { * }) * @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`} */ - query: createScopedGraphQLHandler(OperationTypeNode.QUERY, '*'), + query: createScopedGraphQLHandler('query' as OperationTypeNode, '*'), /** * Captures a GraphQL mutation by a given name. @@ -93,14 +93,14 @@ const standardGraphQLHandlers = { * }) * @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`} */ - mutation: createScopedGraphQLHandler(OperationTypeNode.MUTATION, '*'), + mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, '*'), } function createGraphQLLink(url: Path): typeof standardGraphQLHandlers { return { operation: createGraphQLOperationHandler(url), - query: createScopedGraphQLHandler(OperationTypeNode.QUERY, url), - mutation: createScopedGraphQLHandler(OperationTypeNode.MUTATION, url), + query: createScopedGraphQLHandler('query' as OperationTypeNode, url), + mutation: createScopedGraphQLHandler('mutation' as OperationTypeNode, url), } } From 7a8482ab8a1745533d9b845c0a0e6713eb79c2d1 Mon Sep 17 00:00:00 2001 From: John Daly Date: Wed, 3 Aug 2022 09:30:07 -0700 Subject: [PATCH 2/2] feat: marking 'graphql' as an optional peer dependency The 'graphql' code that is needed at runtime will be dynamically loaded. Existing static imports of 'graphql' are designated as type imports. --- package.json | 3 +++ src/context/errors.ts | 2 +- src/graphql.ts | 2 +- src/handlers/GraphQLHandler.ts | 2 +- src/utils/internal/parseGraphQLRequest.ts | 6 ++++-- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 0d7a3d264..424950f47 100644 --- a/package.json +++ b/package.json @@ -138,6 +138,9 @@ "typescript": ">= 4.2.x <= 4.7.x" }, "peerDependenciesMeta": { + "graphql": { + "optional": true + }, "typescript": { "optional": true } diff --git a/src/context/errors.ts b/src/context/errors.ts index c20f505f5..c64d97331 100644 --- a/src/context/errors.ts +++ b/src/context/errors.ts @@ -1,4 +1,4 @@ -import { GraphQLError } from 'graphql' +import type { GraphQLError } from 'graphql' import { ResponseTransformer } from '../response' import { jsonParse } from '../utils/internal/jsonParse' import { mergeRight } from '../utils/internal/mergeRight' diff --git a/src/graphql.ts b/src/graphql.ts index e74338ec9..7f971bdf6 100644 --- a/src/graphql.ts +++ b/src/graphql.ts @@ -1,4 +1,4 @@ -import { DocumentNode, OperationTypeNode } from 'graphql' +import type { DocumentNode, OperationTypeNode } from 'graphql' import { ResponseResolver } from './handlers/RequestHandler' import { GraphQLHandler, diff --git a/src/handlers/GraphQLHandler.ts b/src/handlers/GraphQLHandler.ts index d4ee555f2..c1c85b576 100644 --- a/src/handlers/GraphQLHandler.ts +++ b/src/handlers/GraphQLHandler.ts @@ -1,4 +1,4 @@ -import { DocumentNode, OperationTypeNode } from 'graphql' +import type { DocumentNode, OperationTypeNode } from 'graphql' import { SerializedResponse } from '../setupWorker/glossary' import { data } from '../context/data' import { extensions } from '../context/extensions' diff --git a/src/utils/internal/parseGraphQLRequest.ts b/src/utils/internal/parseGraphQLRequest.ts index 1b648f0d9..83835334c 100644 --- a/src/utils/internal/parseGraphQLRequest.ts +++ b/src/utils/internal/parseGraphQLRequest.ts @@ -1,8 +1,7 @@ -import { +import type { DocumentNode, OperationDefinitionNode, OperationTypeNode, - parse, } from 'graphql' import { GraphQLVariables } from '../../handlers/GraphQLHandler' import { getPublicUrlFromRequest } from '../request/getPublicUrlFromRequest' @@ -41,6 +40,9 @@ export function parseDocumentNode(node: DocumentNode): ParsedGraphQLQuery { function parseQuery(query: string): ParsedGraphQLQuery | Error { try { + // Since 'graphql' is an optional peer dependency, + // we'll attempt to import it dynamically + const { parse } = require('graphql') const ast = parse(query) return parseDocumentNode(ast) } catch (error) {