From 212ebb36ac3c959e1018d1a0ee430928a612056a Mon Sep 17 00:00:00 2001 From: Matthew Costabile Date: Tue, 30 Aug 2022 12:25:16 +0000 Subject: [PATCH 1/4] fix(package.json): make graphql non-optional peerDependency this makes the graphql peer dependency required. While the hope is to eventually avoid the need to require the graphql library for users who don't declare graphql requests directly, the current architecture means that many tools fails to properly compile without error/warning without this. Since NPM 7, npm will automatically install peer dependencies, which should avoid consumers of msw having to manually install graphql. re #1371 --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index f5836e440..8721d45f9 100644 --- a/package.json +++ b/package.json @@ -138,9 +138,6 @@ "typescript": ">= 4.2.x <= 4.8.x" }, "peerDependenciesMeta": { - "graphql": { - "optional": true - }, "typescript": { "optional": true } From 618989dbb2e4d9ab240b4cbb664643cda9d71fc0 Mon Sep 17 00:00:00 2001 From: Matthew Costabile Date: Tue, 30 Aug 2022 12:31:12 +0000 Subject: [PATCH 2/4] refactor: graphql imported by import instead of require removes the previous attempt at making graphql optional by lazily requiring it instead of importing it. This didn't have the intended affect of helping to avoid parsing in many environments, and might be confusing and/or cause friction with a future migration to using native modules and providing an export that can be treeshaken in all environments. re #1371 --- src/utils/internal/parseGraphQLRequest.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils/internal/parseGraphQLRequest.ts b/src/utils/internal/parseGraphQLRequest.ts index 83835334c..b3d757040 100644 --- a/src/utils/internal/parseGraphQLRequest.ts +++ b/src/utils/internal/parseGraphQLRequest.ts @@ -3,6 +3,7 @@ import type { OperationDefinitionNode, OperationTypeNode, } from 'graphql' +import { parse } from 'graphql' import { GraphQLVariables } from '../../handlers/GraphQLHandler' import { getPublicUrlFromRequest } from '../request/getPublicUrlFromRequest' import { MockedRequest } from '../request/MockedRequest' @@ -40,9 +41,6 @@ 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) { From 1dbc669ce1b9d2bfb229bc655542c336098804f9 Mon Sep 17 00:00:00 2001 From: Matthew Costabile Date: Tue, 30 Aug 2022 16:27:46 +0000 Subject: [PATCH 3/4] fix: use type import for graphql types --- test/graphql-api/compatibility.node.test.ts | 3 +-- test/graphql-api/extensions.node.test.ts | 9 +++------ test/graphql-api/extensions.test.ts | 2 +- test/graphql-api/response-patching.node.test.ts | 3 +-- test/graphql-api/response-patching.test.ts | 3 ++- test/support/graphql.ts | 2 +- 6 files changed, 9 insertions(+), 13 deletions(-) diff --git a/test/graphql-api/compatibility.node.test.ts b/test/graphql-api/compatibility.node.test.ts index 62fdf7ac2..c317e6d81 100644 --- a/test/graphql-api/compatibility.node.test.ts +++ b/test/graphql-api/compatibility.node.test.ts @@ -1,6 +1,5 @@ import fetch from 'cross-fetch' -import { graphql as executeGraphql } from 'graphql' -import { buildSchema } from 'graphql/utilities' +import { graphql as executeGraphql, buildSchema } from 'graphql' import { graphql } from 'msw' import { setupServer } from 'msw/node' import { createGraphQLClient, gql } from '../support/graphql' diff --git a/test/graphql-api/extensions.node.test.ts b/test/graphql-api/extensions.node.test.ts index d8bef7b3c..918294795 100644 --- a/test/graphql-api/extensions.node.test.ts +++ b/test/graphql-api/extensions.node.test.ts @@ -1,14 +1,11 @@ /** * @jest-environment node */ -import fetch from 'node-fetch' -import { - graphql as executeGraphql, - buildSchema, - ExecutionResult, -} from 'graphql' +import type { ExecutionResult } from 'graphql' +import { buildSchema, graphql as executeGraphql } from 'graphql' import { graphql } from 'msw' import { setupServer } from 'msw/node' +import fetch from 'node-fetch' import { gql } from '../support/graphql' const schema = gql` diff --git a/test/graphql-api/extensions.test.ts b/test/graphql-api/extensions.test.ts index 8510ae93a..a65d9abfe 100644 --- a/test/graphql-api/extensions.test.ts +++ b/test/graphql-api/extensions.test.ts @@ -1,6 +1,6 @@ import * as path from 'path' import { pageWith } from 'page-with' -import { ExecutionResult } from 'graphql' +import type { ExecutionResult } from 'graphql' import { executeGraphQLQuery } from './utils/executeGraphQLQuery' import { gql } from '../support/graphql' diff --git a/test/graphql-api/response-patching.node.test.ts b/test/graphql-api/response-patching.node.test.ts index 04e7b6cb3..ad7967b3a 100644 --- a/test/graphql-api/response-patching.node.test.ts +++ b/test/graphql-api/response-patching.node.test.ts @@ -4,8 +4,7 @@ import { graphql } from 'msw' import { setupServer } from 'msw/node' import fetch from 'cross-fetch' -import { graphql as executeGraphql } from 'graphql' -import { buildSchema } from 'graphql/utilities' +import { graphql as executeGraphql, buildSchema } from 'graphql' import { ServerApi, createServer } from '@open-draft/test-server' import { createGraphQLClient, gql } from '../support/graphql' diff --git a/test/graphql-api/response-patching.test.ts b/test/graphql-api/response-patching.test.ts index a003c4d7d..83074f7a5 100644 --- a/test/graphql-api/response-patching.test.ts +++ b/test/graphql-api/response-patching.test.ts @@ -1,6 +1,7 @@ import * as path from 'path' import { pageWith } from 'page-with' -import { ExecutionResult, buildSchema, graphql } from 'graphql' +import type { ExecutionResult } from 'graphql' +import { buildSchema, graphql } from 'graphql' import { ServerApi, createServer } from '@open-draft/test-server' import { SetupWorkerApi } from 'msw' import { gql } from '../support/graphql' diff --git a/test/support/graphql.ts b/test/support/graphql.ts index 12b2d2145..766e47c5d 100644 --- a/test/support/graphql.ts +++ b/test/support/graphql.ts @@ -1,4 +1,4 @@ -import { ExecutionResult } from 'graphql' +import type { ExecutionResult } from 'graphql' /** * Identity function that returns a given template string array. From a1564c37aed185febc48381b3476806d6e69dd3e Mon Sep 17 00:00:00 2001 From: Matthew Costabile Date: Tue, 30 Aug 2022 16:28:11 +0000 Subject: [PATCH 4/4] fix: make graphql a dependency instead of peer --- package.json | 5 ++--- yarn.lock | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 8721d45f9..451c6ade3 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "chalk": "4.1.1", "chokidar": "^3.4.2", "cookie": "^0.4.2", + "graphql": "^15.0.0 || ^16.0.0", "headers-polyfill": "^3.0.4", "inquirer": "^8.2.0", "is-node-process": "^1.0.1", @@ -115,7 +116,6 @@ "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,7 +134,6 @@ "webpack-dev-server": "^3.11.2" }, "peerDependencies": { - "graphql": "^15.0.0 || ^16.0.0", "typescript": ">= 4.2.x <= 4.8.x" }, "peerDependenciesMeta": { @@ -150,4 +149,4 @@ "path": "./node_modules/cz-conventional-changelog" } } -} \ No newline at end of file +} diff --git a/yarn.lock b/yarn.lock index 25fdaf88a..744375edb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5345,10 +5345,10 @@ graceful-fs@^4.2.9: resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.9.tgz#041b05df45755e587a24942279b9d113146e1c96" integrity sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ== -graphql@^16.3.0: - version "16.3.0" - resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.3.0.tgz#a91e24d10babf9e60c706919bb182b53ccdffc05" - integrity sha512-xm+ANmA16BzCT5pLjuXySbQVFwH3oJctUVdy81w1sV0vBU0KgDdBGtxQOUd5zqOBk/JayAFeG8Dlmeq74rjm/A== +"graphql@^15.0.0 || ^16.0.0": + version "16.6.0" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.6.0.tgz#c2dcffa4649db149f6282af726c8c83f1c7c5fdb" + integrity sha512-KPIBPDlW7NxrbT/eh4qPXz5FiFdL5UbaA0XUNz2Rp3Z3hqBSkbj0GVjwFDztsWVauZUWsbKHgMg++sk8UX0bkw== growly@^1.3.0: version "1.3.0"