Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support graphql v16 #998

Merged
merged 13 commits into from
Oct 3, 2022
4 changes: 2 additions & 2 deletions plugins/node/opentelemetry-instrumentation-graphql/.tav.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
graphql:
# Taking a sample from the most downloaded versions in the range "14 || 15"
versions: "^15.8.0 || 15.7.2 || 15.6.1 || 15.6.0 || 15.5.3 || 15.5.1 || 15.5.0 || 15.4.0 || 15.3.0 || 14.6.0 || 14.5.8 || 14.5.0 || 14.0.0"
# Taking a sample from the most downloaded versions in the range "14 || 15 || 16"
versions: "^16.4.0 || ^16.0.0 || ^15.8.0 || 15.7.2 || 15.6.1 || 15.6.0 || 15.5.3 || 15.5.1 || 15.5.0 || 15.4.0 || 15.3.0 || 14.6.0 || 14.5.8 || 14.5.0 || 14.0.0"
dchambers marked this conversation as resolved.
Show resolved Hide resolved
commands: npm run test
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
"access": "public"
},
"peerDependencies": {
"@opentelemetry/api": "^1.0.0"
"@opentelemetry/api": "^1.0.0",
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
},
"devDependencies": {
"@opentelemetry/api": "1.0.2",
"@opentelemetry/sdk-trace-base": "1.2.0",
"@types/mocha": "8.2.3",
"@types/node": "16.11.21",
"graphql": "^15.5.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not update it among the dev-deps as well to the latest and leave testing on v15 for TAV?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great feedback @rauno56. Those changes are now made.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rauno56, I finally had some time to get the tests working while using v16 internally. However, since graphql v16 doesn't appear to support Node.js v8 or Node.js v10, that has meant conditionally disabling a few of the tests. Perhaps, therefore, you might prefer to use the latest 15.x internally, so that running npm test locally exercises all tests.

BTW the failing code coverage check appears to be as a result of an as Error cast I perform, which is weird since casting is only a notional concept, and does not translate to code paths that can be tested.

"gts": "3.1.0",
"mocha": "7.2.0",
"nyc": "15.1.0",
Expand All @@ -60,7 +62,6 @@
"typescript": "4.3.5"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.28.0",
"graphql": "^15.5.1"
"@opentelemetry/instrumentation": "^0.28.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 || 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,8 +322,12 @@ describe('graphql', () => {

beforeEach(async () => {
create({});
await graphql(schema, sourceFindUsingVariable, null, null, {
id: 2,
await graphql({
schema,
source: sourceFindUsingVariable,
variableValues: {
id: 2,
},
});
spans = exporter.getFinishedSpans();
});
Expand Down Expand Up @@ -420,7 +424,7 @@ describe('graphql', () => {
create({
depth: 0,
});
await graphql(schema, sourceList1);
await graphql({ schema, source: sourceList1 });
spans = exporter.getFinishedSpans();
});

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

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

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

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

Expand Down Expand Up @@ -763,8 +767,12 @@ describe('graphql', () => {
create({
allowValues: true,
});
await graphql(schema, sourceFindUsingVariable, null, null, {
id: 2,
await graphql({
schema,
source: sourceFindUsingVariable,
variableValues: {
id: 2,
},
});
spans = exporter.getFinishedSpans();
});
Expand Down Expand Up @@ -861,7 +869,7 @@ describe('graphql', () => {
create({
// allowValues: true
});
await graphql(schema, sourceAddBook);
await graphql({ schema, source: sourceAddBook });
spans = exporter.getFinishedSpans();
});

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

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

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

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

Expand Down Expand Up @@ -1051,7 +1059,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 +1082,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 +1099,7 @@ describe('graphql', () => {
responseHook:
invalidTypeHook as GraphQLInstrumentationExecutionResponseHook,
});
graphqlResult = await graphql(schema, sourceList1);
graphqlResult = await graphql({ schema, source: sourceList1 });
spans = exporter.getFinishedSpans();
});

Expand Down