Skip to content

Commit

Permalink
refactor: replace graphql-helix by graphql-yoga
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypoly committed Nov 2, 2022
1 parent 868b4ee commit 238ce94
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 100 deletions.
2 changes: 1 addition & 1 deletion packages/utils/graphql-codegen-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@graphql-codegen/plugin-helpers": "^2.7.2",
"common-tags": "^1.8.0",
"lz-string": "^1.4.4",
"graphql-helix": "1.13.0",
"graphql-yoga": "three",
"nock": "13.2.9",
"tslib": "~2.4.0"
},
Expand Down
29 changes: 9 additions & 20 deletions packages/utils/graphql-codegen-testing/src/mock-graphql-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLSchema } from 'graphql';
import nock from 'nock';
import { getGraphQLParameters, processRequest as processGraphQLHelixRequest } from 'graphql-helix';
import { createYoga } from 'graphql-yoga';

export function mockGraphQLServer({
schema,
Expand All @@ -15,40 +15,29 @@ export function mockGraphQLServer({
intercept?: (obj: nock.ReplyFnContext) => void;
method?: string;
}) {
const yoga = createYoga({ schema });

const handler = async function (this: nock.ReplyFnContext, uri: string, body: any) {
if (intercept) {
intercept(this);
}
const uriObj = new URL(host + uri);
const queryObj: any = {};
uriObj.searchParams.forEach((val, key) => (queryObj[key] = val));
// Create a generic Request object that can be consumed by Graphql Helix's API
const request = {
body,
body: JSON.stringify(body),
headers: this.req.headers,
method,
query: queryObj,
};
// Extract the GraphQL parameters from the request
const { operationName, query, variables } = getGraphQLParameters(request);

// Validate and execute the query
const result = await processGraphQLHelixRequest({
operationName,
query,
variables,
request,
schema,
});
// processRequest returns one of three types of results depending on how the server should respond
// 1) RESPONSE: a regular JSON payload
// 2) MULTIPART RESPONSE: a multipart response (when @stream or @defer directives are used)
// 3) PUSH: a stream of events to push back down the client for a subscription
if (result.type === 'RESPONSE') {
const response = await yoga.fetch('http://localhost:4000/graphql', request);

if (response) {
const headers = {};
// We set the provided status and headers and just the send the payload back to the client
result.headers.forEach(({ name, value }) => (headers[name] = value));
return [result.status, result.payload, headers];
response.headers.forEach(([value, name]) => (headers[name] = value));
return [response.status, await response.json(), headers];
}
return [500, 'Not implemented'];
};
Expand Down
Loading

0 comments on commit 238ce94

Please sign in to comment.