Skip to content

Commit

Permalink
feat: support graphql v16
Browse files Browse the repository at this point in the history
  • Loading branch information
dchambers committed May 3, 2022
1 parent f6f7ff1 commit 4a2f382
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type {
GraphQLFieldResolver,
GraphQLSchema,
GraphQLTypeResolver,
Source,
} from "graphql";
import { graphql as origGraphql, version } from "graphql";
import { Maybe } from "graphql/jsutils/Maybe";

const variantGraphql = origGraphql as Function;

interface GraphQLArgs {
schema: GraphQLSchema;
source: string | Source;
rootValue?: unknown;
contextValue?: unknown;
variableValues?: Maybe<{
readonly [variable: string]: unknown;
}>;
operationName?: Maybe<string>;
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
}

export const graphql = (args: GraphQLArgs) =>
version.startsWith("14.") || version.startsWith("15.")
? variantGraphql(
args.schema,
args.source,
args.rootValue,
args.contextValue,
args.variableValues,
args.operationName,
args.fieldResolver,
args.typeResolver
)
: variantGraphql(args);
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ graphQLInstrumentation.disable();
// now graphql can be required

import { buildSchema } from './schema';
import { graphql } from 'graphql';
import { graphql } from './graphql-adaptor';
// Construct a schema, using GraphQL schema language
const schema = buildSchema();

Expand Down Expand Up @@ -107,7 +107,7 @@ describe('graphql', () => {
let spans: ReadableSpan[];
beforeEach(async () => {
create({});
await graphql(schema, sourceList1);
await graphql({schema, source: sourceList1});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -234,7 +234,7 @@ describe('graphql', () => {

beforeEach(async () => {
create({});
await graphql(schema, sourceBookById);
await graphql({schema, source: sourceBookById});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -322,9 +322,9 @@ describe('graphql', () => {

beforeEach(async () => {
create({});
await graphql(schema, sourceFindUsingVariable, null, null, {
await graphql({schema, source: sourceFindUsingVariable, variableValues: {
id: 2,
});
}});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -420,7 +420,7 @@ describe('graphql', () => {
create({
depth: 0,
});
await graphql(schema, sourceList1);
await graphql({schema, source: sourceList1});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -488,7 +488,7 @@ describe('graphql', () => {
create({
mergeItems: true,
});
await graphql(schema, sourceList1);
await graphql({schema, source: sourceList1});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -555,7 +555,7 @@ describe('graphql', () => {
mergeItems: true,
depth: 0,
});
await graphql(schema, sourceList1);
await graphql({schema, source: sourceList1});
spans = exporter.getFinishedSpans();
});

Expand All @@ -579,7 +579,7 @@ describe('graphql', () => {
create({
allowValues: true,
});
await graphql(schema, sourceBookById);
await graphql({schema, source: sourceBookById});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -669,7 +669,7 @@ describe('graphql', () => {
create({
allowValues: true,
});
await graphql(schema, sourceAddBook);
await graphql({schema, source: sourceAddBook});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -763,9 +763,9 @@ describe('graphql', () => {
create({
allowValues: true,
});
await graphql(schema, sourceFindUsingVariable, null, null, {
await graphql({schema, source: sourceFindUsingVariable, variableValues: {
id: 2,
});
}});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -861,7 +861,7 @@ describe('graphql', () => {
create({
// allowValues: true
});
await graphql(schema, sourceAddBook);
await graphql({schema, source: sourceAddBook});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -954,7 +954,7 @@ describe('graphql', () => {

beforeEach(async () => {
create({});
await graphql(schema, badQuery);
await graphql({schema, source: badQuery});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -989,7 +989,7 @@ describe('graphql', () => {

beforeEach(async () => {
create({});
await graphql(schema, queryInvalid);
await graphql({schema, source: queryInvalid});
spans = exporter.getFinishedSpans();
});

Expand Down Expand Up @@ -1051,7 +1051,7 @@ describe('graphql', () => {
span.setAttribute(dataAttributeName, JSON.stringify(data));
},
});
graphqlResult = await graphql(schema, sourceList1);
graphqlResult = await graphql({schema, source: sourceList1});
spans = exporter.getFinishedSpans();
});

Expand All @@ -1074,7 +1074,7 @@ describe('graphql', () => {
throw 'some kind of failure!';
},
});
graphqlResult = await graphql(schema, sourceList1);
graphqlResult = await graphql({schema, source: sourceList1});
spans = exporter.getFinishedSpans();
});

Expand All @@ -1091,7 +1091,7 @@ describe('graphql', () => {
responseHook:
invalidTypeHook as GraphQLInstrumentationExecutionResponseHook,
});
graphqlResult = await graphql(schema, sourceList1);
graphqlResult = await graphql({schema, source: sourceList1});
spans = exporter.getFinishedSpans();
});

Expand Down

0 comments on commit 4a2f382

Please sign in to comment.