Skip to content

Commit

Permalink
fix: getting ready for GraphQL v16 (#3716)
Browse files Browse the repository at this point in the history
* Test GraphQL v16

* Fix TS

* Oops dont push package.json

* Fix older versions

* More

* Move on

* Move on again

* Fix TS for v14'

* Try

* Next attempt

* What?

* Ah

* Clear locations completely

* ...

* Remove nodes as well

* Come on

* Ok?

* Now?

* Anothjer

* Remove nodes

* Another try

* Add changeset

* oops

* This is minor actually
  • Loading branch information
ardatan committed Oct 19, 2021
1 parent b1b5eb2 commit 149afdd
Show file tree
Hide file tree
Showing 48 changed files with 265 additions and 336 deletions.
14 changes: 14 additions & 0 deletions .changeset/funny-clocks-report.md
@@ -0,0 +1,14 @@
---
'@graphql-tools/batch-delegate': minor
'@graphql-tools/batch-execute': minor
'@graphql-tools/delegate': minor
'@graphql-tools/load': minor
'@graphql-tools/merge': minor
'@graphql-tools/schema': minor
'@graphql-tools/stitch': minor
'@graphql-tools/stitching-directives': minor
'@graphql-tools/utils': minor
'@graphql-tools/wrap': minor
---

fix: getting ready for GraphQL v16
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Expand Up @@ -38,7 +38,7 @@ jobs:
graphql_version:
- 14
- 15
# - 16.0.0-rc.1
- 16.0.0-rc.5
steps:
- name: Checkout Master
uses: actions/checkout@v2
Expand All @@ -57,7 +57,7 @@ jobs:
run: node ./scripts/match-graphql.js ${{matrix.graphql_version}}
- name: Install Dependencies using Yarn
run: yarn install --ignore-engines && git checkout yarn.lock
- name: Build
- name: Type Check
run: yarn ts:check
test:
name: Unit Test on Node ${{matrix.node_version}} (${{matrix.os}}) and GraphQL v${{matrix.graphql_version}}
Expand All @@ -69,7 +69,7 @@ jobs:
graphql_version:
- 14
- 15
# - 16.0.0-rc.1
- 16.0.0-rc.5
include:
- node_version: 14
os: windows-latest
Expand Down
6 changes: 3 additions & 3 deletions packages/batch-delegate/tests/basic.example.test.ts
@@ -1,4 +1,4 @@
import { execute, parse } from 'graphql';
import { execute, OperationTypeNode, parse } from 'graphql';

import { makeExecutableSchema } from '@graphql-tools/schema';
import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('batch delegation within basic stitching example', () => {
resolve(chirp, _args, context, info) {
return batchDelegateToSchema({
schema: authorSchema,
operation: 'query',
operation: 'query' as OperationTypeNode,
fieldName: 'usersByIds',
key: chirp.chirpedAtUserId,
argsFromKeys: (ids) => ({ ids }),
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('batch delegation within basic stitching example', () => {
resolve(user, _args, context, info) {
return batchDelegateToSchema({
schema: postsSchema,
operation: 'query',
operation: 'query' as OperationTypeNode,
fieldName: 'posts',
key: user.postIds,
context,
Expand Down
4 changes: 2 additions & 2 deletions packages/batch-delegate/tests/withTransforms.test.ts
@@ -1,4 +1,4 @@
import { execute, GraphQLList, GraphQLObjectType, Kind, parse } from 'graphql';
import { execute, GraphQLList, GraphQLObjectType, Kind, OperationTypeNode, parse } from 'graphql';

import { makeExecutableSchema } from '@graphql-tools/schema';
import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('works with complex transforms', () => {
selectionSet: `{ id }`,
resolve: (user, _args, context, info) => batchDelegateToSchema({
schema: bookSchema,
operation: 'query',
operation: 'query' as OperationTypeNode,
fieldName: 'booksByUserIds',
key: user.id,
argsFromKeys: (userIds) => ({ userIds }),
Expand Down
4 changes: 1 addition & 3 deletions packages/batch-execute/src/mergeRequests.ts
Expand Up @@ -12,8 +12,6 @@ import {
SelectionNode,
FragmentSpreadNode,
VariableNode,
VisitorKeyMap,
ASTKindToNode,
InlineFragmentNode,
FieldNode,
} from 'graphql';
Expand Down Expand Up @@ -176,7 +174,7 @@ function aliasTopLevelFields(prefix: string, document: DocumentNode): DocumentNo
};
return visit(document, transformer, {
[Kind.DOCUMENT]: [`definitions`],
} as unknown as VisitorKeyMap<ASTKindToNode>);
} as any);
}

/**
Expand Down
35 changes: 18 additions & 17 deletions packages/batch-execute/tests/batchExecute.test.ts
Expand Up @@ -3,7 +3,8 @@ import {
parse,
print,
OperationDefinitionNode,
ExecutionResult
ExecutionResult,
OperationTypeNode
} from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { createBatchingExecutor } from '@graphql-tools/batch-execute';
Expand Down Expand Up @@ -68,8 +69,8 @@ describe('batch execution', () => {

it('batchs multiple executions', async () => {
const [first, second] = await Promise.all([
batchExec({ document: parse('{ field1 field2 }'), operationType: 'query' }),
batchExec({ document: parse('{ field2 field3(input: "3") }'), operationType: 'query' }),
batchExec({ document: parse('{ field1 field2 }'), operationType: 'query' as OperationTypeNode, }),
batchExec({ document: parse('{ field2 field3(input: "3") }'), operationType: 'query' as OperationTypeNode, }),
]) as ExecutionResult[];

expect(first?.data).toEqual({ field1: '1', field2: '2' });
Expand All @@ -80,8 +81,8 @@ describe('batch execution', () => {

it('preserves root field aliases in the final result', async () => {
const [first, second] = await Promise.all([
batchExec({ document: parse('{ a: field1 b: field2 }'), operationType: 'query' }),
batchExec({ document: parse('{ c: field2 d: field3(input: "3") }'), operationType: 'query' }),
batchExec({ document: parse('{ a: field1 b: field2 }'), operationType: 'query' as OperationTypeNode, }),
batchExec({ document: parse('{ c: field2 d: field3(input: "3") }'), operationType: 'query' as OperationTypeNode, }),
]) as ExecutionResult[];

expect(first?.data).toEqual({ a: '1', b: '2' });
Expand All @@ -95,12 +96,12 @@ describe('batch execution', () => {
batchExec({
document: parse('query($a: String){ field3(input: $a) }'),
variables: { a: '1' },
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
batchExec({
document: parse('query($a: String){ field3(input: $a) }'),
variables: { a: '2' },
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
]) as ExecutionResult[];

Expand All @@ -112,8 +113,8 @@ describe('batch execution', () => {

it('renames fields within inline spreads', async () => {
const [first, second] = await Promise.all([
batchExec({ document: parse('{ ...on Query { field1 } }'), operationType: 'query' }),
batchExec({ document: parse('{ ...on Query { field2 } }'), operationType: 'query' }),
batchExec({ document: parse('{ ...on Query { field1 } }'), operationType: 'query' as OperationTypeNode, }),
batchExec({ document: parse('{ ...on Query { field2 } }'), operationType: 'query' as OperationTypeNode, }),
]) as ExecutionResult[];

const squishedDoc = executorDocument?.replace(/\s+/g, ' ');
Expand All @@ -128,11 +129,11 @@ describe('batch execution', () => {
const [first, second] = await Promise.all([
batchExec({
document: parse('fragment A on Widget { name } query{ widget { ...A } }'),
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
batchExec({
document: parse('fragment A on Widget { name } query{ widget { ...A } }'),
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
]) as ExecutionResult[];

Expand All @@ -150,11 +151,11 @@ describe('batch execution', () => {
const [first, second] = await Promise.all([
batchExec({
document: parse('fragment A on Query { field1 } query{ ...A }'),
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
batchExec({
document: parse('fragment A on Query { field2 } query{ ...A }'),
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
]) as ExecutionResult[];

Expand All @@ -167,11 +168,11 @@ describe('batch execution', () => {
const [first, second] = await Promise.all([
batchExec({
document: parse('{ first: boom(message: "first error") }'),
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
batchExec({
document: parse('{ second: boom(message: "second error") }'),
operationType: 'query',
operationType: 'query' as OperationTypeNode,
}),
]) as ExecutionResult[];

Expand All @@ -184,8 +185,8 @@ describe('batch execution', () => {

it('returns request-level errors to all results', async () => {
const [first, second] = await Promise.all([
batchExec({ document: parse('{ field1 field2 }'), operationType: 'query' }),
batchExec({ document: parse('{ notgonnawork }'), operationType: 'query' }),
batchExec({ document: parse('{ field1 field2 }'), operationType: 'query' as OperationTypeNode, }),
batchExec({ document: parse('{ notgonnawork }'), operationType: 'query' as OperationTypeNode, }),
]) as ExecutionResult[];

expect(first?.errors?.length).toEqual(1);
Expand Down
9 changes: 5 additions & 4 deletions packages/delegate/src/createRequest.ts
Expand Up @@ -14,6 +14,7 @@ import {
SelectionSetNode,
DefinitionNode,
DocumentNode,
NameNode,
} from 'graphql';

import {
Expand All @@ -26,12 +27,12 @@ import { ICreateRequestFromInfo, ICreateRequest } from './types';

export function getDelegatingOperation(parentType: GraphQLObjectType, schema: GraphQLSchema): OperationTypeNode {
if (parentType === schema.getMutationType()) {
return 'mutation';
return 'mutation' as OperationTypeNode;
} else if (parentType === schema.getSubscriptionType()) {
return 'subscription';
return 'subscription' as OperationTypeNode;
}

return 'query';
return 'query' as OperationTypeNode;
}

export function createRequestFromInfo({
Expand Down Expand Up @@ -149,7 +150,7 @@ export function createRequest({
selectionSet: newSelectionSet,
};

const operationName = targetOperationName
const operationName: NameNode | undefined = targetOperationName
? {
kind: Kind.NAME,
value: targetOperationName,
Expand Down
34 changes: 19 additions & 15 deletions packages/delegate/src/finalizeGatewayRequest.ts
@@ -1,6 +1,6 @@
import {
ArgumentNode,
ASTKindToNode,
DocumentNode,
FragmentDefinitionNode,
getNamedType,
GraphQLField,
Expand All @@ -19,7 +19,6 @@ import {
VariableDefinitionNode,
versionInfo as graphqlVersionInfo,
visit,
VisitorKeyMap,
visitWithTypeInfo,
} from 'graphql';

Expand All @@ -31,6 +30,7 @@ import {
createVariableNameGenerator,
implementsAbstractType,
inspect,
ASTVisitorKeyMap,
} from '@graphql-tools/utils';

import { DelegationContext } from './types';
Expand Down Expand Up @@ -94,12 +94,14 @@ function finalizeGatewayDocument(
});
}

const newDocument: DocumentNode = {
kind: Kind.DOCUMENT,
definitions: [...newOperations, ...newFragments],
};

return {
usedVariables,
newDocument: {
kind: Kind.DOCUMENT,
definitions: [...newOperations, ...newFragments],
},
newDocument,
};
}

Expand Down Expand Up @@ -158,7 +160,7 @@ function addVariablesToRootFields(

const type = getDefinedRootType(targetSchema, operation.operation);

const newSelectionSet: Array<SelectionNode> = [];
const newSelections: Array<SelectionNode> = [];

for (const selection of operation.selectionSet.selections) {
if (selection.kind === Kind.FIELD) {
Expand All @@ -178,22 +180,24 @@ function addVariablesToRootFields(
updateArguments(targetField, argumentNodeMap, variableDefinitionMap, newVariables, args);
}

newSelectionSet.push({
newSelections.push({
...selection,
arguments: Object.values(argumentNodeMap),
});
} else {
newSelectionSet.push(selection);
newSelections.push(selection);
}
}

const newSelectionSet: SelectionSetNode = {
kind: Kind.SELECTION_SET,
selections: newSelections,
};

return {
...operation,
variableDefinitions: Object.values(variableDefinitionMap),
selectionSet: {
kind: Kind.SELECTION_SET,
selections: newSelectionSet,
},
selectionSet: newSelectionSet,
};
});

Expand Down Expand Up @@ -284,14 +288,14 @@ function collectFragmentVariables(
};
}

const filteredSelectionSetVisitorKeys: Partial<VisitorKeyMap<ASTKindToNode>> = {
const filteredSelectionSetVisitorKeys: ASTVisitorKeyMap = {
SelectionSet: ['selections'],
Field: ['selectionSet'],
InlineFragment: ['selectionSet'],
FragmentDefinition: ['selectionSet'],
};

const variablesVisitorKeys: Partial<VisitorKeyMap<ASTKindToNode>> = {
const variablesVisitorKeys: ASTVisitorKeyMap = {
SelectionSet: ['selections'],
Field: ['arguments', 'directives', 'selectionSet'],
Argument: ['value'],
Expand Down
10 changes: 4 additions & 6 deletions packages/delegate/src/prepareGatewayDocument.ts
Expand Up @@ -16,11 +16,9 @@ import {
GraphQLOutputType,
isObjectType,
FieldNode,
VisitorKeyMap,
ASTKindToNode,
} from 'graphql';

import { implementsAbstractType, getRootTypeNames, memoize2 } from '@graphql-tools/utils';
import { implementsAbstractType, getRootTypeNames, memoize2, ASTVisitorKeyMap } from '@graphql-tools/utils';

import { getDocumentMetadata } from './getDocumentMetadata';
import { StitchingInfo } from './types';
Expand Down Expand Up @@ -52,12 +50,12 @@ export function prepareGatewayDocument(

const typeInfo = new TypeInfo(transformedSchema);

const expandedDocument = {
const expandedDocument: DocumentNode = {
kind: Kind.DOCUMENT,
definitions: [...operations, ...fragments, ...expandedFragments],
};

const visitorKeyMap: Partial<VisitorKeyMap<ASTKindToNode>> = {
const visitorKeyMap: ASTVisitorKeyMap = {
Document: ['definitions'],
OperationDefinition: ['selectionSet'],
SelectionSet: ['selections'],
Expand Down Expand Up @@ -392,7 +390,7 @@ function wrapConcreteTypes(

const typeInfo = new TypeInfo(targetSchema);

const visitorKeys: Partial<VisitorKeyMap<ASTKindToNode>> = {
const visitorKeys: ASTVisitorKeyMap = {
Document: ['definitions'],
OperationDefinition: ['selectionSet'],
SelectionSet: ['selections'],
Expand Down

1 comment on commit 149afdd

@vercel
Copy link

@vercel vercel bot commented on 149afdd Oct 19, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.