Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/bold-webs-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@graphql-tools/executor-urql-exchange': minor
'@graphql-tools/executor-apollo-link': minor
'@graphql-tools/git-loader': minor
'@graphql-tools/executor': minor
'@graphql-tools/optimize': minor
'@graphql-tools/schema': minor
'@graphql-tools/utils': minor
---

Add support for graphql-js v17
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"eslint-plugin-n": "18.2.2",
"eslint-plugin-promise": "7.3.0",
"globby": "11.1.0",
"graphql": "16.14.0",
"graphql": "17.0.2",
"graphql-yoga": "5.21.2",
"husky": "9.1.7",
"jest": "30.4.2",
Expand All @@ -108,7 +108,7 @@
"extract-zip": {
"yauzl": "3.3.0"
},
"graphql": "16.14.0",
"graphql": "17.0.2",
"miniflare": {
"undici": "8.7.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/executor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
"devDependencies": {
"cross-inspect": "1.0.1",
"graphql": "^16.14.0"
"graphql": "^17.0.2"
},
"publishConfig": {
"directory": "dist",
Expand Down
5 changes: 4 additions & 1 deletion packages/executor/src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
GraphQLUnionType,
Kind,
parse,
versionInfo,
} from 'graphql';
import { expectJSON } from '../../__testUtils__/expectJSON.js';
import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js';
Expand Down Expand Up @@ -207,6 +208,8 @@ describe('Execute: Handles basic execution tasks', () => {
'operation',
'variableValues',
'signal',
'getAbortSignal',
'getAsyncHelpers',
]);

const operation = document.definitions[0];
Expand All @@ -226,7 +229,7 @@ describe('Execute: Handles basic execution tasks', () => {
expect(resolvedInfo).toMatchObject({
fieldNodes: [field],
path: { prev: undefined, key: 'result', typename: 'Test' },
variableValues: { var: 'abc' },
variableValues: versionInfo.major >= 17 ? { coerced: { var: 'abc' } } : { var: 'abc' },
});
});

Expand Down
11 changes: 2 additions & 9 deletions packages/executor/src/execution/__tests__/oneOf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,10 @@ if (versionInfo.major !== 16) {
const result = executeQuery(query, rootValue);

expectJSON(result).toDeepEqual({
data: {
test: null,
},
errors: [
{
locations: [{ column: 23, line: 3 }],
message:
// This type of error would be caught at validation-time
// hence the vague error message here.
'Argument "input" of non-null type "TestInputObject!" must not be null.',
path: ['test'],
locations: [{ column: 43, line: 2 }],
message: 'Variable "$input" has invalid default value.',
},
],
});
Expand Down
32 changes: 24 additions & 8 deletions packages/executor/src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" got invalid value null at "input.c"; Expected non-nullable type "String!" not to be null.',
versionInfo.major >= 17
? 'Variable "$input" got invalid value null at "input.c"; Expected value of non-null type "String!" not to be null.'
: 'Variable "$input" got invalid value null at "input.c"; Expected non-nullable type "String!" not to be null.',
locations: [{ line: 2, column: 16 }],
},
],
Expand All @@ -432,7 +434,9 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" got invalid value "foo bar"; Expected type "TestInputObject" to be an object.',
versionInfo.major >= 17
? 'Variable "$input" got invalid value "foo bar"; Expected value of type "TestInputObject" to be an object, found: "foo bar".'
: 'Variable "$input" got invalid value "foo bar"; Expected type "TestInputObject" to be an object.',
locations: [{ line: 2, column: 16 }],
},
],
Expand All @@ -446,7 +450,9 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" got invalid value { a: "foo", b: "bar" }; Field "c" of required type "String!" was not provided.',
versionInfo.major >= 17
? 'Variable "$input" got invalid value { a: "foo", b: "bar" }; Expected value of type "TestInputObject" to include required field "c", found: { a: "foo", b: "bar" }.'
: 'Variable "$input" got invalid value { a: "foo", b: "bar" }; Field "c" of required type "String!" was not provided.',
locations: [{ line: 2, column: 16 }],
},
],
Expand All @@ -465,12 +471,16 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" got invalid value { a: "foo" } at "input.na"; Field "c" of required type "String!" was not provided.',
versionInfo.major >= 17
? 'Variable "$input" got invalid value { a: "foo" } at "input.na"; Expected value of type "TestInputObject" to include required field "c", found: { a: "foo" }.'
: 'Variable "$input" got invalid value { a: "foo" } at "input.na"; Field "c" of required type "String!" was not provided.',
locations: [{ line: 2, column: 18 }],
},
{
message:
'Variable "$input" got invalid value { na: { a: "foo" } }; Field "nb" of required type "String!" was not provided.',
versionInfo.major >= 17
? 'Variable "$input" got invalid value { na: { a: "foo" } }; Expected value of type "TestNestedInputObject" to include required field "nb", found: { na: { a: "foo" } }.'
: 'Variable "$input" got invalid value { na: { a: "foo" } }; Field "nb" of required type "String!" was not provided.',
locations: [{ line: 2, column: 18 }],
},
],
Expand All @@ -487,7 +497,9 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" got invalid value { a: "foo", b: "bar", c: "baz", extra: "dog" }; Field "extra" is not defined by type "TestInputObject".',
versionInfo.major >= 17
? 'Variable "$input" got invalid value { a: "foo", b: "bar", c: "baz", extra: "dog" }; Expected value of type "TestInputObject" not to include unknown field "extra", found: { a: "foo", b: "bar", c: "baz", extra: "dog" }.'
: 'Variable "$input" got invalid value { a: "foo", b: "bar", c: "baz", extra: "dog" }; Field "extra" is not defined by type "TestInputObject".',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down Expand Up @@ -889,7 +901,9 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.',
versionInfo.major >= 17
? 'Variable "$input" got invalid value null at "input[1]"; Expected value of non-null type "String!" not to be null.'
: 'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down Expand Up @@ -937,7 +951,9 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.',
versionInfo.major >= 17
? 'Variable "$input" got invalid value null at "input[1]"; Expected value of non-null type "String!" not to be null.'
: 'Variable "$input" got invalid value null at "input[1]"; Expected non-nullable type "String!" not to be null.',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down
30 changes: 26 additions & 4 deletions packages/executor/src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
Path,
pathToArray,
promiseReduce,
toGraphQLJSVariableValues,
} from '@graphql-tools/utils';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import { DisposableSymbols } from '@whatwg-node/disposablestack';
Expand Down Expand Up @@ -816,7 +817,8 @@ export function buildResolveInfo(
): GraphQLResolveInfo {
// The resolve function's optional fourth argument is a collection of
// information about the current execution state.
return {
const { signal } = exeContext;
const info = {
fieldName: fieldDef.name,
fieldNodes,
returnType: fieldDef.type,
Expand All @@ -826,9 +828,29 @@ export function buildResolveInfo(
fragments: exeContext.fragments,
rootValue: exeContext.rootValue,
operation: exeContext.operation,
variableValues: exeContext.variableValues,
signal: exeContext.signal,
// graphql-js >= 17's `GraphQLResolveInfo.variableValues` is the structured
// `{ coerced, sources }` shape, not the flat map this package uses internally.
variableValues: toGraphQLJSVariableValues(exeContext.variableValues),
signal,
getAbortSignal: () => signal,
getAsyncHelpers: () => ({
promiseAll: <T>(values: ReadonlyArray<PromiseLike<T> | T>) => Promise.all(values),
// graphql-js >= 17's real `track()` registers background promises. This
// package doesn't implement graphql-js's optional `asyncWorkFinished` hook,
// so mirror just that safety property instead of building out the full hook feature.
track: (maybePromises: ReadonlyArray<unknown>) => {
for (const maybePromise of maybePromises) {
if (isPromise(maybePromise)) {
Promise.resolve(maybePromise).then(
() => {},
() => {},
);
}
}
},
}),
};
return info;
}

export const CRITICAL_ERROR = 'CRITICAL_ERROR' as const;
Expand Down Expand Up @@ -989,7 +1011,7 @@ function getStreamValues(
const stream = getDirectiveValues(
GraphQLStreamDirective,
fieldNodes[0],
exeContext.variableValues,
toGraphQLJSVariableValues(exeContext.variableValues),
) as {
initialCount: number;
label: string;
Expand Down
70 changes: 65 additions & 5 deletions packages/executor/src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,34 @@ import {
valueFromAST,
VariableDefinitionNode,
} from 'graphql';
import { createGraphQLError, hasOwnProperty, inspect, printPathArray } from '@graphql-tools/utils';
import {
createGraphQLError,
getOptionalGraphQLJSExport,
hasOwnProperty,
inspect,
printPathArray,
} from '@graphql-tools/utils';

// `validateInputValue` was introduced in graphql-js@17, replacing the `onError`
// callback that `coerceInputValue` used to accept.
const validateInputValue = getOptionalGraphQLJSExport('validateInputValue');

// `coerceInputLiteral` was introduced in graphql-js@17. Unlike `valueFromAST`, it
// correctly rejects AST literals whose kind doesn't match the target type (e.g. an
// enum literal provided for a String variable default), which `valueFromAST`
// stopped doing in graphql-js@17. See the same pattern in getArgumentValues.ts.
const coerceInputLiteral = getOptionalGraphQLJSExport('coerceInputLiteral');

function getAtPath(value: unknown, path: ReadonlyArray<string | number>): unknown {
let current = value;
for (const key of path) {
if (current == null || typeof current !== 'object' || !hasOwnProperty(current, String(key))) {
return undefined;
}
current = (current as Record<string | number, unknown>)[key];
}
return current;
}

type CoercedVariableValues =
| { errors: ReadonlyArray<GraphQLError>; coerced?: never }
Expand Down Expand Up @@ -78,7 +105,22 @@ function coerceVariableValues(

if (!hasOwnProperty(inputs, varName)) {
if (varDefNode.defaultValue) {
coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
const coercedDefaultValue = coerceInputLiteral
? coerceInputLiteral(varDefNode.defaultValue, varType)
: valueFromAST(varDefNode.defaultValue, varType);
if (coercedDefaultValue === undefined) {
// Schema/document validation should catch an invalid default value
// before execution; this mirrors graphql-js's own
// `coerceDefaultValue`/`maybeUseDefaultValue`, which report this
// case as an error rather than silently producing `undefined`.
onError(
createGraphQLError(`Variable "$${varName}" has invalid default value.`, {
nodes: varDefNode.defaultValue,
}),
);
} else {
coercedValues[varName] = coercedDefaultValue;
}
} else if (isNonNullType(varType)) {
const varTypeStr = inspect(varType);
onError(
Expand Down Expand Up @@ -107,8 +149,8 @@ function coerceVariableValues(
continue;
}

coercedValues[varName] = coerceInputValue(value, varType, (path, invalidValue, error) => {
let prefix = `Variable "$${varName}" got invalid value ` + inspect(invalidValue);
const reportInvalidValue = (path: ReadonlyArray<string | number>, error: GraphQLError) => {
let prefix = `Variable "$${varName}" got invalid value ` + inspect(getAtPath(value, path));
if (path.length > 0) {
prefix += ` at "${varName}${printPathArray(path)}"`;
}
Expand All @@ -118,7 +160,25 @@ function coerceVariableValues(
originalError: error,
}),
);
});
};

if (validateInputValue) {
// graphql-js >= 17: `coerceInputValue` no longer reports diagnostics via a
// callback, so fall back to `validateInputValue` when coercion fails.
const coercedValue = coerceInputValue(value, varType);
if (coercedValue !== undefined) {
coercedValues[varName] = coercedValue;
} else {
validateInputValue(value, varType, (error, path) => reportInvalidValue(path, error));
}
} else {
coercedValues[varName] = (coerceInputValue as any)(
value,
varType,
(path: ReadonlyArray<string | number>, _invalidValue: unknown, error: GraphQLError) =>
reportInvalidValue(path, error),
);
}
}

return coercedValues;
Expand Down
2 changes: 1 addition & 1 deletion packages/executors/apollo-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"typings": "dist/typings/index.d.ts",
"peerDependencies": {
"@apollo/client": "^4.0.4",
"graphql": "^15.2.0 || ^16.0.0"
"graphql": "^15.2.0 || ^16.0.0 || ^17.0.0"
},
"dependencies": {
"@graphql-tools/utils": "^11.2.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/executors/urql-exchange/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"peerDependencies": {
"@urql/core": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0",
"graphql": "^15.2.0 || ^16.0.0",
"graphql": "^15.2.0 || ^16.0.0 || ^17.0.0",
"wonka": "^6.0.0"
},
"dependencies": {
Expand Down
Loading
Loading