Skip to content

Commit

Permalink
fix(deps): include "graphql" as a dependency (#1381)
Browse files Browse the repository at this point in the history
* 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

* 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

* fix: use type import for graphql types

* fix: make graphql a dependency instead of peer
  • Loading branch information
mattcosta7 committed Sep 1, 2022
1 parent 083dd9a commit 8436515
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 26 deletions.
8 changes: 2 additions & 6 deletions package.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -134,13 +134,9 @@
"webpack-dev-server": "^3.11.2"
},
"peerDependencies": {
"graphql": "^15.0.0 || ^16.0.0",
"typescript": ">= 4.2.x <= 4.8.x"
},
"peerDependenciesMeta": {
"graphql": {
"optional": true
},
"typescript": {
"optional": true
}
Expand All @@ -153,4 +149,4 @@
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
4 changes: 1 addition & 3 deletions src/utils/internal/parseGraphQLRequest.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions 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'
Expand Down
9 changes: 3 additions & 6 deletions 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`
Expand Down
2 changes: 1 addition & 1 deletion 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'

Expand Down
3 changes: 1 addition & 2 deletions test/graphql-api/response-patching.node.test.ts
Expand Up @@ -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'

Expand Down
3 changes: 2 additions & 1 deletion 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'
Expand Down
2 changes: 1 addition & 1 deletion 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.
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -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"
Expand Down

0 comments on commit 8436515

Please sign in to comment.