@graphql-codegen/testing 1.18.0-alpha-20221102102337-d2153d8bd → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mockGraphQLServer = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const nock_1 = tslib_1.__importDefault(require("nock"));
6
- const graphql_yoga_1 = require("graphql-yoga");
6
+ const graphql_helix_1 = require("graphql-helix");
7
7
  function mockGraphQLServer({ schema, host, path, intercept, method = 'POST', }) {
8
- const yoga = (0, graphql_yoga_1.createYoga)({ schema });
9
8
  const handler = async function (uri, body) {
10
9
  if (intercept) {
11
10
  intercept(this);
@@ -13,18 +12,32 @@ function mockGraphQLServer({ schema, host, path, intercept, method = 'POST', })
13
12
  const uriObj = new URL(host + uri);
14
13
  const queryObj = {};
15
14
  uriObj.searchParams.forEach((val, key) => (queryObj[key] = val));
15
+ // Create a generic Request object that can be consumed by Graphql Helix's API
16
16
  const request = {
17
- body: JSON.stringify(body),
17
+ body,
18
18
  headers: this.req.headers,
19
19
  method,
20
20
  query: queryObj,
21
21
  };
22
- const response = await yoga.fetch('http://localhost:4000/graphql', request);
23
- if (response) {
22
+ // Extract the GraphQL parameters from the request
23
+ const { operationName, query, variables } = (0, graphql_helix_1.getGraphQLParameters)(request);
24
+ // Validate and execute the query
25
+ const result = await (0, graphql_helix_1.processRequest)({
26
+ operationName,
27
+ query,
28
+ variables,
29
+ request,
30
+ schema,
31
+ });
32
+ // processRequest returns one of three types of results depending on how the server should respond
33
+ // 1) RESPONSE: a regular JSON payload
34
+ // 2) MULTIPART RESPONSE: a multipart response (when @stream or @defer directives are used)
35
+ // 3) PUSH: a stream of events to push back down the client for a subscription
36
+ if (result.type === 'RESPONSE') {
24
37
  const headers = {};
25
38
  // We set the provided status and headers and just the send the payload back to the client
26
- response.headers.forEach(([value, name]) => (headers[name] = value));
27
- return [response.status, await response.json(), headers];
39
+ result.headers.forEach(({ name, value }) => (headers[name] = value));
40
+ return [result.status, result.payload, headers];
28
41
  }
29
42
  return [500, 'Not implemented'];
30
43
  };
@@ -1,7 +1,6 @@
1
1
  import nock from 'nock';
2
- import { createYoga } from 'graphql-yoga';
2
+ import { getGraphQLParameters, processRequest as processGraphQLHelixRequest } from 'graphql-helix';
3
3
  export function mockGraphQLServer({ schema, host, path, intercept, method = 'POST', }) {
4
- const yoga = createYoga({ schema });
5
4
  const handler = async function (uri, body) {
6
5
  if (intercept) {
7
6
  intercept(this);
@@ -9,18 +8,32 @@ export function mockGraphQLServer({ schema, host, path, intercept, method = 'POS
9
8
  const uriObj = new URL(host + uri);
10
9
  const queryObj = {};
11
10
  uriObj.searchParams.forEach((val, key) => (queryObj[key] = val));
11
+ // Create a generic Request object that can be consumed by Graphql Helix's API
12
12
  const request = {
13
- body: JSON.stringify(body),
13
+ body,
14
14
  headers: this.req.headers,
15
15
  method,
16
16
  query: queryObj,
17
17
  };
18
- const response = await yoga.fetch('http://localhost:4000/graphql', request);
19
- if (response) {
18
+ // Extract the GraphQL parameters from the request
19
+ const { operationName, query, variables } = getGraphQLParameters(request);
20
+ // Validate and execute the query
21
+ const result = await processGraphQLHelixRequest({
22
+ operationName,
23
+ query,
24
+ variables,
25
+ request,
26
+ schema,
27
+ });
28
+ // processRequest returns one of three types of results depending on how the server should respond
29
+ // 1) RESPONSE: a regular JSON payload
30
+ // 2) MULTIPART RESPONSE: a multipart response (when @stream or @defer directives are used)
31
+ // 3) PUSH: a stream of events to push back down the client for a subscription
32
+ if (result.type === 'RESPONSE') {
20
33
  const headers = {};
21
34
  // We set the provided status and headers and just the send the payload back to the client
22
- response.headers.forEach(([value, name]) => (headers[name] = value));
23
- return [response.status, await response.json(), headers];
35
+ result.headers.forEach(({ name, value }) => (headers[name] = value));
36
+ return [result.status, result.payload, headers];
24
37
  }
25
38
  return [500, 'Not implemented'];
26
39
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/testing",
3
- "version": "1.18.0-alpha-20221102102337-d2153d8bd",
3
+ "version": "1.18.0",
4
4
  "description": "GraphQL Codegen Testing Utils",
5
5
  "peerDependencies": {
6
6
  "jest-diff": "^23.0.0 || ^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0",
@@ -10,7 +10,7 @@
10
10
  "@graphql-codegen/plugin-helpers": "^2.7.2",
11
11
  "common-tags": "^1.8.0",
12
12
  "lz-string": "^1.4.4",
13
- "graphql-yoga": "3.0.0-next.8",
13
+ "graphql-helix": "1.13.0",
14
14
  "nock": "13.2.9",
15
15
  "tslib": "~2.4.0"
16
16
  },