Skip to content

Commit

Permalink
feat: GraphQL v16 compatibility (#6867)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Oct 20, 2021
1 parent fefc340 commit 97ddb48
Show file tree
Hide file tree
Showing 70 changed files with 502 additions and 275 deletions.
46 changes: 46 additions & 0 deletions .changeset/eighty-ligers-repair.md
@@ -0,0 +1,46 @@
---
'@graphql-codegen/core': minor
'@graphql-codegen/c-sharp': minor
'@graphql-codegen/c-sharp-operations': minor
'@graphql-codegen/flow': minor
'@graphql-codegen/flow-operations': minor
'@graphql-codegen/flow-resolvers': minor
'@graphql-codegen/java-apollo-android': minor
'@graphql-codegen/java': minor
'@graphql-codegen/kotlin': minor
'@graphql-codegen/java-resolvers': minor
'@graphql-codegen/fragment-matcher': minor
'@graphql-codegen/jsdoc': minor
'@graphql-codegen/schema-ast': minor
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-apollo-angular': minor
'@graphql-codegen/typescript-document-nodes': minor
'@graphql-codegen/typescript-generic-sdk': minor
'@graphql-codegen/typescript-graphql-apollo': minor
'@graphql-codegen/typescript-graphql-request': minor
'@graphql-codegen/typescript-jit-sdk': minor
'@graphql-codegen/typescript-mongodb': minor
'@graphql-codegen/named-operations-object': minor
'@graphql-codegen/typescript-oclif': minor
'@graphql-codegen/typescript-operations': minor
'@graphql-codegen/typescript-react-apollo': minor
'@graphql-codegen/typescript-react-offix': minor
'@graphql-codegen/typescript-react-query': minor
'@graphql-codegen/typescript-resolvers': minor
'@graphql-codegen/typescript-rtk-query': minor
'@graphql-codegen/typescript-stencil-apollo': minor
'@graphql-codegen/typescript-type-graphql': minor
'@graphql-codegen/typed-document-node': minor
'@graphql-codegen/typescript': minor
'@graphql-codegen/typescript-urql': minor
'@graphql-codegen/typescript-urql-graphcache': minor
'@graphql-codegen/typescript-vue-apollo': minor
'@graphql-codegen/typescript-vue-apollo-smart-ops': minor
'@graphql-codegen/typescript-vue-urql': minor
'@graphql-codegen/graphql-modules-preset': minor
'@graphql-codegen/near-operation-file-preset': minor
'@graphql-codegen/testing': minor
'@graphql-codegen/plugin-helpers': minor
---

feat: GraphQL v16 compatibility
104 changes: 87 additions & 17 deletions .github/workflows/main.yml
@@ -1,4 +1,4 @@
name: CI
name: Testing

on:
push:
Expand All @@ -9,13 +9,89 @@ on:
- master

jobs:
lint:
name: Linting Check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node
uses: actions/setup-node@master
with:
node-version: 16.8.0
- name: Cache Yarn
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-16.8.0-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-16.8.0-yarn-
- name: Install Dependencies using Yarn
run: yarn install
- name: Lint
run: yarn lint
dev-tests:
name: Validating dev-tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node
uses: actions/setup-node@master
with:
node-version: 16.8.0
- name: Cache Yarn
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-16.8.0-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-16.8.0-yarn-
- name: Install Dependencies using Yarn
run: yarn install
- name: Build
run: yarn build
env:
CI: true
- name: Test dev-tests
run: |
yarn run generate:examples
git diff --exit-code -- dev-test/
esm:
name: Testing ESM
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node
uses: actions/setup-node@master
with:
node-version: 16.8.0
- name: Cache Yarn
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-16.8.0-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-16.8.0-yarn-
- name: Install Dependencies using Yarn
run: yarn install
- name: Build
run: yarn build
env:
CI: true
- name: Test ESM
run: node scripts/test-esm.mjs
test:
name: Testing on Node ${{matrix.node_version}}
name: Unit Test on Node ${{matrix.node_version}} (${{matrix.os}}) and GraphQL v${{matrix.graphql_version}}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest] # remove windows to speed up the tests
node_version: [12, '16.8.0']
graphql_version:
- 15
- 16.0.0-rc.5
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -27,29 +103,23 @@ jobs:
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-${{matrix.node_version}}-yarn-${{ hashFiles('yarn.lock') }}
key: ${{ runner.os }}-${{matrix.node_version}}-${{matrix.graphql_version}}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{matrix.node_version}}-yarn-
${{ runner.os }}-${{matrix.node_version}}-${{matrix.graphql_version}}-yarnn
- name: Use GraphQL v${{matrix.graphql_version}}
run: node ./scripts/match-graphql.js ${{matrix.graphql_version}}
- name: Install Dependencies using Yarn
run: yarn install
- name: Cache Jest
uses: actions/cache@v2
with:
path: .cache/jest
key: ${{ runner.os }}-${{matrix.node_version}}-jest-${{ hashFiles('yarn.lock') }}
key: ${{ runner.os }}-${{matrix.node_version}}-${{matrix.graphql_version}}-jest-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{matrix.node_version}}-jest-
- name: Install Dependencies using Yarn
run: yarn install
- name: Lint
run: yarn lint
${{ runner.os }}-${{matrix.node_version}}-${{matrix.graphql_version}}-jest-
- name: Build
run: yarn build
- name: Test
run: yarn test
env:
CI: true
- name: Test dev-tests
run: |
yarn run generate:examples
git diff --exit-code -- dev-test/
- name: Test ESM
run: node scripts/test-esm.mjs
4 changes: 2 additions & 2 deletions packages/graphql-codegen-core/src/codegen.ts
Expand Up @@ -6,7 +6,7 @@ import {
getCachedDocumentNodeFromSchema,
AddToSchemaResult,
} from '@graphql-codegen/plugin-helpers';
import { visit, DefinitionNode, Kind, print, NameNode, specifiedRules } from 'graphql';
import { visit, DefinitionNode, Kind, print, NameNode, specifiedRules, DocumentNode } from 'graphql';
import { executePlugin } from './execute-plugin';
import { checkValidationErrors, validateGraphQlDocuments, Source, asArray } from '@graphql-tools/utils';

Expand Down Expand Up @@ -81,7 +81,7 @@ export async function codegen(options: Types.GenerateOptions): Promise<string> {
...documents,
...extraFragments.map(f => ({
location: f.importFrom,
document: { kind: Kind.DOCUMENT, definitions: [f.node] },
document: { kind: Kind.DOCUMENT, definitions: [f.node] } as DocumentNode,
})),
],
specifiedRules.filter(rule => !ignored.some(ignoredRule => rule.name.startsWith(ignoredRule)))
Expand Down
5 changes: 3 additions & 2 deletions packages/plugins/c-sharp/c-sharp-operations/src/index.ts
Expand Up @@ -3,8 +3,9 @@ import {
PluginValidateFn,
PluginFunction,
getCachedDocumentNodeFromSchema,
oldVisit,
} from '@graphql-codegen/plugin-helpers';
import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql';
import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql';
import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
import { CSharpOperationsVisitor } from './visitor';
import { extname } from 'path';
Expand All @@ -31,7 +32,7 @@ export const plugin: PluginFunction<CSharpOperationsRawPluginConfig> = (
];

const visitor = new CSharpOperationsVisitor(schema, allFragments, config, documents);
const visitorResult = visit(allAst, { leave: visitor });
const visitorResult = oldVisit(allAst, { leave: visitor });
const imports = visitor.getCSharpImports();
const openNameSpace = `namespace ${visitor.config.namespaceName} {`;
return {
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/c-sharp/c-sharp-operations/src/visitor.ts
Expand Up @@ -438,6 +438,7 @@ export class CSharpOperationsVisitor extends ClientSideBaseVisitor<
].join('\n');
}
}
throw new Error(`Unexpected operation type: ${node.operation}`);
}

public OperationDefinition(node: OperationDefinitionNode): string {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/c-sharp/c-sharp/src/index.ts
@@ -1,5 +1,5 @@
import { GraphQLSchema, visit } from 'graphql';
import { PluginFunction, Types, getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
import { GraphQLSchema } from 'graphql';
import { PluginFunction, Types, getCachedDocumentNodeFromSchema, oldVisit } from '@graphql-codegen/plugin-helpers';
import { CSharpResolversVisitor } from './visitor';
import { CSharpResolversPluginRawConfig } from './config';

Expand All @@ -10,7 +10,7 @@ export const plugin: PluginFunction<CSharpResolversPluginRawConfig> = async (
): Promise<string> => {
const visitor = new CSharpResolversVisitor(config, schema);
const astNode = getCachedDocumentNodeFromSchema(schema);
const visitorResult = visit(astNode, { leave: visitor as any });
const visitorResult = oldVisit(astNode, { leave: visitor });
const imports = visitor.getImports();
const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n');
const wrappedBlockContent = visitor.wrapWithClass(blockContent);
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/flow/flow/src/index.ts
@@ -1,5 +1,5 @@
import { Types, PluginFunction, getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
import { visit, GraphQLSchema } from 'graphql';
import { Types, PluginFunction, getCachedDocumentNodeFromSchema, oldVisit } from '@graphql-codegen/plugin-helpers';
import { GraphQLSchema } from 'graphql';
import { FlowVisitor } from './visitor';
import { FlowPluginConfig } from './config';

Expand All @@ -15,7 +15,7 @@ export const plugin: PluginFunction<FlowPluginConfig, Types.ComplexPluginOutput>
const astNode = getCachedDocumentNodeFromSchema(schema);
const visitor = new FlowVisitor(schema, config);

const visitorResult = visit(astNode, {
const visitorResult = oldVisit(astNode, {
leave: visitor,
});

Expand Down
14 changes: 7 additions & 7 deletions packages/plugins/flow/flow/src/visitor.ts
Expand Up @@ -55,7 +55,7 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
InputValueDefinition(node: InputValueDefinitionNode, key?: number | string, parent?: any): string {
const originalFieldNode = parent[key] as FieldDefinitionNode;
const addOptionalSign = originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
const comment = transformComment((node.description as any) as string, 1);
const comment = transformComment(node.description as any as string, 1);

return comment + indent(`${node.name}${addOptionalSign ? '?' : ''}: ${node.type},`);
}
Expand All @@ -79,9 +79,9 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
}

FieldDefinition(node: FieldDefinitionNode): string {
const typeString = (node.type as any) as string;
const typeString = node.type as any as string;
const namePostfix = typeString.startsWith('?') ? '?' : '';
const comment = transformComment((node.description as any) as string, 1);
const comment = transformComment(node.description as any as string, 1);

return comment + indent(`${this.config.useFlowReadOnlyTypes ? '+' : ''}${node.name}${namePostfix}: ${typeString},`);
}
Expand All @@ -92,7 +92,7 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
...node,
interfaces:
node.interfaces && node.interfaces.length > 0
? node.interfaces.map(name => ((name as any) as string).replace('?', ''))
? node.interfaces.map(name => (name as any as string).replace('?', ''))
: ([] as any),
},
key,
Expand Down Expand Up @@ -148,7 +148,7 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
}

EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
const typeName = (node.name as any) as string;
const typeName = node.name as any as string;

if (this.config.enumValues[typeName] && this.config.enumValues[typeName].sourceFile) {
return null;
Expand All @@ -167,7 +167,7 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
.withBlock(
node.values
.map(enumOption => {
const comment = transformComment((enumOption.description as any) as string, 1);
const comment = transformComment(enumOption.description as any as string, 1);
const optionName = this.convertName(enumOption, { transformUnderscore: true, useTypesPrefix: false });
let enumValue: string | number = enumOption.name as any;

Expand All @@ -188,7 +188,7 @@ export class FlowVisitor extends BaseTypesVisitor<FlowPluginConfig, FlowPluginPa
.export()
.asKind('type')
.withName(this.convertName(node, { useTypesPrefix: this.config.enumPrefix }))
.withComment((node.description as any) as string)
.withComment(node.description as any as string)
.withContent(`$Values<typeof ${enumValuesName}>`).string;

return [enumValues, enumType].join('\n\n');
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/flow/operations/src/index.ts
@@ -1,5 +1,5 @@
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
import { visit, concatAST, GraphQLSchema, Kind, FragmentDefinitionNode } from 'graphql';
import { oldVisit, PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
import { concatAST, GraphQLSchema, Kind, FragmentDefinitionNode } from 'graphql';
import { FlowDocumentsVisitor } from './visitor';
import { LoadedFragment, optimizeOperations } from '@graphql-codegen/visitor-plugin-common';
import { FlowDocumentsPluginConfig } from './config';
Expand Down Expand Up @@ -32,7 +32,7 @@ export const plugin: PluginFunction<FlowDocumentsPluginConfig> = (

const visitor = new FlowDocumentsVisitor(schema, config, allFragments);

const visitorResult = visit(allAst, {
const visitorResult = oldVisit(allAst, {
leave: visitor,
});

Expand Down
5 changes: 3 additions & 2 deletions packages/plugins/flow/resolvers/src/index.ts
Expand Up @@ -4,8 +4,9 @@ import {
PluginFunction,
addFederationReferencesToSchema,
getCachedDocumentNodeFromSchema,
oldVisit,
} from '@graphql-codegen/plugin-helpers';
import { visit, GraphQLSchema } from 'graphql';
import { GraphQLSchema } from 'graphql';
import { FlowResolversVisitor } from './visitor';

/**
Expand All @@ -31,7 +32,7 @@ export const plugin: PluginFunction<RawFlowResolversConfig, Types.ComplexPluginO

const astNode = getCachedDocumentNodeFromSchema(transformedSchema);
const visitor = new FlowResolversVisitor(config, transformedSchema);
const visitorResult = visit(astNode, { leave: visitor });
const visitorResult = oldVisit(astNode, { leave: visitor });

const defsToInclude: string[] = [visitor.getResolverTypeWrapperSignature()];

Expand Down
12 changes: 6 additions & 6 deletions packages/plugins/flow/resolvers/src/visitor.ts
Expand Up @@ -110,13 +110,13 @@ export class FlowResolversVisitor extends BaseResolversVisitor<RawResolversConfi
typeName: string,
relevantFields: { fieldName: string; replaceWithType: string }[]
): string {
return `$Diff<${typeName}, { ${relevantFields
.map(f => `${f.fieldName}: * `)
.join(', ')} }> & { ${relevantFields.map(f => `${f.fieldName}: ${f.replaceWithType}`).join(', ')} }`;
return `$Diff<${typeName}, { ${relevantFields.map(f => `${f.fieldName}: * `).join(', ')} }> & { ${relevantFields
.map(f => `${f.fieldName}: ${f.replaceWithType}`)
.join(', ')} }`;
}

ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string {
const nameAsString = (node.name as any) as string;
const nameAsString = node.name as any as string;
const baseName = this.getTypeToUse(nameAsString);
this._collectedResolvers[node.name as any] = 'GraphQLScalarType';

Expand All @@ -143,7 +143,7 @@ export class FlowResolversVisitor extends BaseResolversVisitor<RawResolversConfi

protected buildEnumResolverContentBlock(node: EnumTypeDefinitionNode, mappedEnumType: string): string {
const valuesMap = `{| ${(node.values || [])
.map(v => `${(v.name as any) as string}${this.config.avoidOptionals ? '' : '?'}: *`)
.map(v => `${v.name as any as string}${this.config.avoidOptionals ? '' : '?'}: *`)
.join(', ')} |}`;

this._globalDeclarations.add(ENUM_RESOLVERS_SIGNATURE);
Expand All @@ -157,7 +157,7 @@ export class FlowResolversVisitor extends BaseResolversVisitor<RawResolversConfi
): string {
return `{| ${(node.values || [])
.map(v => {
const valueName = (v.name as any) as string;
const valueName = v.name as any as string;
const mappedValue = valuesMapping[valueName];
return `${valueName}: ${typeof mappedValue === 'number' ? mappedValue : `'${mappedValue}'`}`;
Expand Down

0 comments on commit 97ddb48

Please sign in to comment.