From 97ddb487ae2d4f54eca9974c960b78c078a2982e Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Wed, 20 Oct 2021 08:33:19 -0400 Subject: [PATCH] feat: GraphQL v16 compatibility (#6867) --- .changeset/eighty-ligers-repair.md | 46 ++++++ .github/workflows/main.yml | 104 ++++++++++--- packages/graphql-codegen-core/src/codegen.ts | 4 +- .../c-sharp/c-sharp-operations/src/index.ts | 5 +- .../c-sharp/c-sharp-operations/src/visitor.ts | 1 + packages/plugins/c-sharp/c-sharp/src/index.ts | 6 +- packages/plugins/flow/flow/src/index.ts | 6 +- packages/plugins/flow/flow/src/visitor.ts | 14 +- packages/plugins/flow/operations/src/index.ts | 6 +- packages/plugins/flow/resolvers/src/index.ts | 5 +- .../plugins/flow/resolvers/src/visitor.ts | 12 +- .../apollo-android/src/base-java-visitor.ts | 2 +- .../apollo-android/src/field-arguments.ts | 4 +- .../plugins/java/apollo-android/src/plugin.ts | 6 +- .../plugins/java/apollo-android/src/preset.ts | 13 +- packages/plugins/java/java/src/index.ts | 6 +- packages/plugins/java/kotlin/src/index.ts | 6 +- packages/plugins/java/kotlin/src/visitor.ts | 12 +- packages/plugins/java/resolvers/src/index.ts | 6 +- .../other/fragment-matcher/src/index.ts | 6 +- packages/plugins/other/jsdoc/src/index.ts | 4 +- .../plugins/other/schema-ast/src/index.ts | 2 +- .../other/schema-ast/tests/schema-ast.spec.ts | 30 ++-- .../src/base-documents-visitor.ts | 1 + .../src/client-side-base-visitor.ts | 5 +- .../src/selection-set-to-object.ts | 2 +- .../other/visitor-plugin-common/src/utils.ts | 6 +- .../typescript/apollo-angular/src/index.ts | 6 +- .../typescript/apollo-angular/src/visitor.ts | 4 +- .../typescript/document-nodes/src/index.ts | 6 +- .../typescript/generic-sdk/src/index.ts | 6 +- .../typescript/graphql-apollo/src/index.ts | 6 +- .../typescript/graphql-request/src/index.ts | 6 +- .../plugins/typescript/jit-sdk/src/index.ts | 6 +- .../tests/__snapshots__/jit-sdk.spec.ts.snap | 6 +- .../typescript/jit-sdk/tests/jit-sdk.spec.ts | 4 +- .../plugins/typescript/mongodb/src/index.ts | 5 +- .../plugins/typescript/mongodb/src/visitor.ts | 8 +- .../named-operations-object/src/index.ts | 6 +- .../plugins/typescript/oclif/src/index.ts | 6 +- .../typescript/operations/src/index.ts | 10 +- .../operations/tests/ts-documents.spec.ts | 3 +- .../react-apollo-offix/src/index.ts | 6 +- .../typescript/react-apollo/src/index.ts | 6 +- .../typescript/react-query/src/index.ts | 6 +- .../plugins/typescript/resolvers/src/index.ts | 5 +- .../typescript/resolvers/src/visitor.ts | 4 +- .../plugins/typescript/rtk-query/src/index.ts | 6 +- .../typescript/stencil-apollo/src/index.ts | 8 +- .../typescript/type-graphql/src/index.ts | 6 +- .../typescript/type-graphql/src/visitor.ts | 14 +- .../typed-document-node/src/index.ts | 6 +- .../typescript/typescript/src/index.ts | 6 +- .../typescript/urql-graphcache/src/index.ts | 19 +-- packages/plugins/typescript/urql/src/index.ts | 8 +- .../vue-apollo-smart-ops/src/index.ts | 6 +- .../typescript/vue-apollo/src/index.ts | 6 +- .../vue-apollo/tests/vue-apollo.spec.ts | 140 ++++++++++-------- .../plugins/typescript/vue-urql/src/index.ts | 8 +- packages/presets/graphql-modules/src/index.ts | 3 +- .../presets/near-operation-file/src/index.ts | 5 +- .../presets/near-operation-file/src/utils.ts | 7 +- .../src/mock-graphql-server.ts | 20 ++- .../utils/plugins-helpers/src/federation.ts | 4 +- packages/utils/plugins-helpers/src/helpers.ts | 8 +- packages/utils/plugins-helpers/src/index.ts | 1 + .../utils/plugins-helpers/src/oldVisit.ts | 27 ++++ packages/utils/plugins-helpers/src/utils.ts | 2 +- scripts/match-graphql.js | 21 +++ yarn.lock | 11 +- 70 files changed, 502 insertions(+), 275 deletions(-) create mode 100644 .changeset/eighty-ligers-repair.md create mode 100644 packages/utils/plugins-helpers/src/oldVisit.ts create mode 100644 scripts/match-graphql.js diff --git a/.changeset/eighty-ligers-repair.md b/.changeset/eighty-ligers-repair.md new file mode 100644 index 00000000000..4b2670c4ec5 --- /dev/null +++ b/.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 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index af636257c83..6ca2cb969a3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: CI +name: Testing on: push: @@ -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 @@ -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 diff --git a/packages/graphql-codegen-core/src/codegen.ts b/packages/graphql-codegen-core/src/codegen.ts index bf235b9556c..bf354581788 100644 --- a/packages/graphql-codegen-core/src/codegen.ts +++ b/packages/graphql-codegen-core/src/codegen.ts @@ -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'; @@ -81,7 +81,7 @@ export async function codegen(options: Types.GenerateOptions): Promise { ...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))) diff --git a/packages/plugins/c-sharp/c-sharp-operations/src/index.ts b/packages/plugins/c-sharp/c-sharp-operations/src/index.ts index 2275d54ad4b..b89577fc1e9 100644 --- a/packages/plugins/c-sharp/c-sharp-operations/src/index.ts +++ b/packages/plugins/c-sharp/c-sharp-operations/src/index.ts @@ -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'; @@ -31,7 +32,7 @@ export const plugin: PluginFunction = ( ]; 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 { diff --git a/packages/plugins/c-sharp/c-sharp-operations/src/visitor.ts b/packages/plugins/c-sharp/c-sharp-operations/src/visitor.ts index 25064f2767c..2bcc5b9532b 100644 --- a/packages/plugins/c-sharp/c-sharp-operations/src/visitor.ts +++ b/packages/plugins/c-sharp/c-sharp-operations/src/visitor.ts @@ -438,6 +438,7 @@ export class CSharpOperationsVisitor extends ClientSideBaseVisitor< ].join('\n'); } } + throw new Error(`Unexpected operation type: ${node.operation}`); } public OperationDefinition(node: OperationDefinitionNode): string { diff --git a/packages/plugins/c-sharp/c-sharp/src/index.ts b/packages/plugins/c-sharp/c-sharp/src/index.ts index 68874936238..09f6a623d2c 100644 --- a/packages/plugins/c-sharp/c-sharp/src/index.ts +++ b/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'; @@ -10,7 +10,7 @@ export const plugin: PluginFunction = async ( ): Promise => { 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); diff --git a/packages/plugins/flow/flow/src/index.ts b/packages/plugins/flow/flow/src/index.ts index 48f99e5961a..acd6d18a16b 100644 --- a/packages/plugins/flow/flow/src/index.ts +++ b/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'; @@ -15,7 +15,7 @@ export const plugin: PluginFunction const astNode = getCachedDocumentNodeFromSchema(schema); const visitor = new FlowVisitor(schema, config); - const visitorResult = visit(astNode, { + const visitorResult = oldVisit(astNode, { leave: visitor, }); diff --git a/packages/plugins/flow/flow/src/visitor.ts b/packages/plugins/flow/flow/src/visitor.ts index f0b173d799b..e9e27e2e3fc 100644 --- a/packages/plugins/flow/flow/src/visitor.ts +++ b/packages/plugins/flow/flow/src/visitor.ts @@ -55,7 +55,7 @@ export class FlowVisitor extends BaseTypesVisitor 0 - ? node.interfaces.map(name => ((name as any) as string).replace('?', '')) + ? node.interfaces.map(name => (name as any as string).replace('?', '')) : ([] as any), }, key, @@ -148,7 +148,7 @@ export class FlowVisitor extends BaseTypesVisitor { - 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; @@ -188,7 +188,7 @@ export class FlowVisitor extends BaseTypesVisitor`).string; return [enumValues, enumType].join('\n\n'); diff --git a/packages/plugins/flow/operations/src/index.ts b/packages/plugins/flow/operations/src/index.ts index f4cfab1a838..495bde952da 100644 --- a/packages/plugins/flow/operations/src/index.ts +++ b/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'; @@ -32,7 +32,7 @@ export const plugin: PluginFunction = ( const visitor = new FlowDocumentsVisitor(schema, config, allFragments); - const visitorResult = visit(allAst, { + const visitorResult = oldVisit(allAst, { leave: visitor, }); diff --git a/packages/plugins/flow/resolvers/src/index.ts b/packages/plugins/flow/resolvers/src/index.ts index 9c5a76c9c53..02f387fb02b 100644 --- a/packages/plugins/flow/resolvers/src/index.ts +++ b/packages/plugins/flow/resolvers/src/index.ts @@ -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'; /** @@ -31,7 +32,7 @@ export const plugin: PluginFunction `${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'; @@ -143,7 +143,7 @@ export class FlowResolversVisitor extends BaseResolversVisitor `${(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); @@ -157,7 +157,7 @@ export class FlowResolversVisitor extends BaseResolversVisitor { - 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}'`}`; diff --git a/packages/plugins/java/apollo-android/src/base-java-visitor.ts b/packages/plugins/java/apollo-android/src/base-java-visitor.ts index 9707626fb7f..de952e5a55b 100644 --- a/packages/plugins/java/apollo-android/src/base-java-visitor.ts +++ b/packages/plugins/java/apollo-android/src/base-java-visitor.ts @@ -67,7 +67,7 @@ export class BaseJavaVisitor extends BaseVis for (const graphqlType of Object.values(allTypesMap)) { if (graphqlType instanceof GraphQLObjectType) { const allInterfaces = graphqlType.getInterfaces(); - if (allInterfaces.find(int => int.name === ((node.name as any) as string))) { + if (allInterfaces.find(int => int.name === (node.name as any as string))) { implementingTypes.push(graphqlType.name); } } diff --git a/packages/plugins/java/apollo-android/src/field-arguments.ts b/packages/plugins/java/apollo-android/src/field-arguments.ts index 7e6dc462cd2..f3fb6b4de02 100644 --- a/packages/plugins/java/apollo-android/src/field-arguments.ts +++ b/packages/plugins/java/apollo-android/src/field-arguments.ts @@ -1,6 +1,5 @@ import { FieldNode, - visit, ArgumentNode, ObjectValueNode, ObjectFieldNode, @@ -11,6 +10,7 @@ import { } from 'graphql'; import { ImportsSet } from './types'; import { Imports } from './imports'; +import { oldVisit } from '@graphql-codegen/plugin-helpers'; export function visitFieldArguments(selection: FieldNode, imports: ImportsSet): string { if (!selection.arguments || selection.arguments.length === 0) { @@ -21,7 +21,7 @@ export function visitFieldArguments(selection: FieldNode, imports: ImportsSet): imports.add(Imports.String); imports.add(Imports.Object); - return visit(selection, { + return oldVisit(selection, { leave: { Field: (node: FieldNode) => { return ( diff --git a/packages/plugins/java/apollo-android/src/plugin.ts b/packages/plugins/java/apollo-android/src/plugin.ts index ccebf78d535..c0246d6e0c1 100644 --- a/packages/plugins/java/apollo-android/src/plugin.ts +++ b/packages/plugins/java/apollo-android/src/plugin.ts @@ -1,5 +1,5 @@ -import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers'; -import { GraphQLSchema, concatAST, visit, Kind, FragmentDefinitionNode } from 'graphql'; +import { oldVisit, PluginFunction, Types } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { RawConfig, LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { InputTypeVisitor } from './input-type-visitor'; import { BaseJavaVisitor } from './base-java-visitor'; @@ -100,7 +100,7 @@ export const plugin: PluginFunction { + const document: DocumentNode = { kind: Kind.DOCUMENT, definitions: [ast] }; return { filename: join(outDir, packageNameToDirectory(options.config.typePackage), ast.name.value + '.java'), plugins: options.plugins, @@ -66,7 +67,7 @@ export const preset: Types.OutputPreset = { skipDocumentsValidation: true, }, schema: options.schema, - documents: [{ document: { kind: Kind.DOCUMENT, definitions: [ast] }, location: '' }], + documents: [{ document, location: '' }], }; }), ...operationsAst.map((ast: OperationDefinitionNode) => { @@ -74,6 +75,7 @@ export const preset: Types.OutputPreset = { ? ast.name.value : `${ast.name.value}${pascalCase(ast.operation)}`; + const document: DocumentNode = { kind: Kind.DOCUMENT, definitions: [ast] }; return { filename: join(outDir, packageNameToDirectory(options.config.package), fileName + '.java'), plugins: options.plugins, @@ -84,10 +86,11 @@ export const preset: Types.OutputPreset = { externalFragments, }, schema: options.schema, - documents: [{ document: { kind: Kind.DOCUMENT, definitions: [ast] }, location: '' }], + documents: [{ document, location: '' }], }; }), ...fragments.map((ast: FragmentDefinitionNode) => { + const document: DocumentNode = { kind: Kind.DOCUMENT, definitions: [ast] }; return { filename: join(outDir, packageNameToDirectory(options.config.fragmentPackage), ast.name.value + '.java'), plugins: options.plugins, @@ -98,7 +101,7 @@ export const preset: Types.OutputPreset = { externalFragments, }, schema: options.schema, - documents: [{ document: { kind: Kind.DOCUMENT, definitions: [ast] }, location: '' }], + documents: [{ document, location: '' }], }; }), ]; diff --git a/packages/plugins/java/java/src/index.ts b/packages/plugins/java/java/src/index.ts index 22eff2e7d69..cd7f90c2ab2 100644 --- a/packages/plugins/java/java/src/index.ts +++ b/packages/plugins/java/java/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 { JavaResolversVisitor } from './visitor'; import { buildPackageNameFromPath } from '@graphql-codegen/java-common'; import { dirname, normalize } from 'path'; @@ -15,7 +15,7 @@ export const plugin: PluginFunction = async ( const defaultPackageName = buildPackageNameFromPath(relevantPath); const visitor = new JavaResolversVisitor(config, schema, defaultPackageName); const astNode = getCachedDocumentNodeFromSchema(schema); - const visitorResult = visit(astNode, { leave: visitor as any }); + const visitorResult = oldVisit(astNode, { leave: visitor }); const imports = visitor.getImports(); const packageName = visitor.getPackageName(); const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n'); diff --git a/packages/plugins/java/kotlin/src/index.ts b/packages/plugins/java/kotlin/src/index.ts index a40545abe11..f07047d6a9e 100644 --- a/packages/plugins/java/kotlin/src/index.ts +++ b/packages/plugins/java/kotlin/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 { KotlinResolversVisitor } from './visitor'; import { buildPackageNameFromPath } from '@graphql-codegen/java-common'; import { dirname, normalize } from 'path'; @@ -15,7 +15,7 @@ export const plugin: PluginFunction = async ( const defaultPackageName = buildPackageNameFromPath(relevantPath); const visitor = new KotlinResolversVisitor(config, schema, defaultPackageName); const astNode = getCachedDocumentNodeFromSchema(schema); - const visitorResult = visit(astNode, { leave: visitor as any }); + const visitorResult = oldVisit(astNode, { leave: visitor as any }); const packageName = visitor.getPackageName(); const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n\n'); diff --git a/packages/plugins/java/kotlin/src/visitor.ts b/packages/plugins/java/kotlin/src/visitor.ts index 98bff9d133e..3b5c66827c1 100644 --- a/packages/plugins/java/kotlin/src/visitor.ts +++ b/packages/plugins/java/kotlin/src/visitor.ts @@ -105,9 +105,13 @@ ${enumValues} }`; } - protected resolveInputFieldType( - typeNode: TypeNode - ): { baseType: string; typeName: string; isScalar: boolean; isArray: boolean; nullable: boolean } { + protected resolveInputFieldType(typeNode: TypeNode): { + baseType: string; + typeName: string; + isScalar: boolean; + isArray: boolean; + nullable: boolean; + } { const innerType = getBaseTypeNode(typeNode); const schemaType = this._schema.getType(innerType.name.value); const isArray = @@ -281,7 +285,7 @@ ${classMembers} ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string { const name = this.convertName(node); - const fields = (node.fields as unknown) as FieldDefinitionReturnType[]; + const fields = node.fields as unknown as FieldDefinitionReturnType[]; const fieldNodes = []; const argsTypes = []; diff --git a/packages/plugins/java/resolvers/src/index.ts b/packages/plugins/java/resolvers/src/index.ts index d046877cb64..6cef58db7ac 100644 --- a/packages/plugins/java/resolvers/src/index.ts +++ b/packages/plugins/java/resolvers/src/index.ts @@ -1,5 +1,5 @@ -import { GraphQLSchema, visit } from 'graphql'; -import { getCachedDocumentNodeFromSchema, PluginFunction, Types } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema } from 'graphql'; +import { getCachedDocumentNodeFromSchema, oldVisit, PluginFunction, Types } from '@graphql-codegen/plugin-helpers'; import { JavaResolversVisitor } from './visitor'; import { buildPackageNameFromPath } from '@graphql-codegen/java-common'; import { dirname, normalize } from 'path'; @@ -15,7 +15,7 @@ export const plugin: PluginFunction = async ( const defaultPackageName = buildPackageNameFromPath(relevantPath); const visitor = new JavaResolversVisitor(config, schema, defaultPackageName); const astNode = getCachedDocumentNodeFromSchema(schema); - const visitorResult = visit(astNode, { leave: visitor as any }); + const visitorResult = oldVisit(astNode, { leave: visitor }); const mappersImports = visitor.getImports(); const packageName = visitor.getPackageName(); const blockContent = visitorResult.definitions.filter(d => typeof d === 'string').join('\n'); diff --git a/packages/plugins/other/fragment-matcher/src/index.ts b/packages/plugins/other/fragment-matcher/src/index.ts index cc9166beae0..f07669cb2cd 100644 --- a/packages/plugins/other/fragment-matcher/src/index.ts +++ b/packages/plugins/other/fragment-matcher/src/index.ts @@ -109,7 +109,7 @@ export const plugin: PluginFunction = async ( const cleanSchema = config.federation ? removeFederation(schema) : schema; const useExplicitTyping = config.useExplicitTyping; - const introspection = await execute({ + const introspection = (await execute({ schema: cleanSchema, document: parse(` { @@ -124,7 +124,7 @@ export const plugin: PluginFunction = async ( } } `), - }); + })) as any; const ext = extname(info.outputFile).toLowerCase(); if (!introspection.data) { @@ -136,7 +136,7 @@ export const plugin: PluginFunction = async ( return { ...acc, ...{ [type.name]: type.possibleTypes.map(possibleType => possibleType.name) } }; }; - const filteredData: PossibleTypesResultData | IntrospectionResultData = + const filteredData: IntrospectionResultData | PossibleTypesResultData = apolloClientVersion === 2 ? { __schema: { diff --git a/packages/plugins/other/jsdoc/src/index.ts b/packages/plugins/other/jsdoc/src/index.ts index 1499ba71ba9..1e76ca6b958 100644 --- a/packages/plugins/other/jsdoc/src/index.ts +++ b/packages/plugins/other/jsdoc/src/index.ts @@ -45,7 +45,7 @@ export const plugin: PluginFunction = (schema, documents) => const mappedDocuments = documents.map(document => document.document).filter(document => document !== undefined); const ast = concatAST([parsedSchema, ...(mappedDocuments as Array)]); - const schemaTypes: Array = visit(ast, { + const schemaTypes = visit(ast, { Document: { leave(node) { return node.definitions; @@ -209,7 +209,7 @@ export const plugin: PluginFunction = (schema, documents) => return null; }, }, - }); + }) as unknown as string[]; return schemaTypes.join('\n\n'); }; diff --git a/packages/plugins/other/schema-ast/src/index.ts b/packages/plugins/other/schema-ast/src/index.ts index 28d7920db8c..a408dcfaa51 100644 --- a/packages/plugins/other/schema-ast/src/index.ts +++ b/packages/plugins/other/schema-ast/src/index.ts @@ -64,7 +64,7 @@ export const plugin: PluginFunction = async ( return print(transformedSchemaAndAst.ast); } - return printSchema(transformedSchemaAndAst.schema, { commentDescriptions: commentDescriptions }); + return (printSchema as any)(transformedSchemaAndAst.schema, { commentDescriptions }); }; export const validate: PluginValidateFn = async ( diff --git a/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts b/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts index 13eb2c6a56c..cc4b365f480 100644 --- a/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts +++ b/packages/plugins/other/schema-ast/tests/schema-ast.spec.ts @@ -1,5 +1,5 @@ import { validate, plugin } from '../src/index'; -import { buildSchema, parse } from 'graphql'; +import { buildSchema, parse, versionInfo } from 'graphql'; import '@graphql-codegen/testing'; import { Types } from '@graphql-codegen/plugin-helpers'; @@ -103,24 +103,26 @@ describe('Schema AST', () => { `); }); - it('Should print schema with as # when commentDescriptions=true', async () => { - const testSchema = buildSchema(/* GraphQL */ ` - type Query { - """ - test - """ - fieldTest: String - } - `); - const content = await plugin(testSchema, [], { commentDescriptions: true, includeDirectives: false }); - - expect(content).toBeSimilarStringTo(` + if (versionInfo.major < 16) { + it('Should print schema with as # when commentDescriptions=true', async () => { + const testSchema = buildSchema(/* GraphQL */ ` + type Query { + """ + test + """ + fieldTest: String + } + `); + const content = await plugin(testSchema, [], { commentDescriptions: true, includeDirectives: false }); + + expect(content).toBeSimilarStringTo(` type Query { # test fieldTest: String } `); - }); + }); + } it('Should print schema with directives when "includeDirectives" is set', async () => { const content = await plugin(schema, [], { includeDirectives: true }); diff --git a/packages/plugins/other/visitor-plugin-common/src/base-documents-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/base-documents-visitor.ts index f9ff9e85511..94f43c84602 100644 --- a/packages/plugins/other/visitor-plugin-common/src/base-documents-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/base-documents-visitor.ts @@ -24,6 +24,7 @@ function getRootType(operation: OperationTypeNode, schema: GraphQLSchema) { case 'subscription': return schema.getSubscriptionType(); } + throw new Error(`Unknown operation type: ${operation}`); } export interface ParsedDocumentsConfig extends ParsedTypesConfig { diff --git a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts index 3cde35e56d7..2047d77b135 100644 --- a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts @@ -4,14 +4,13 @@ import { FragmentDefinitionNode, print, OperationDefinitionNode, - visit, FragmentSpreadNode, GraphQLSchema, Kind, } from 'graphql'; import { DepGraph } from 'dependency-graph'; import gqlTag from 'graphql-tag'; -import { Types } from '@graphql-codegen/plugin-helpers'; +import { oldVisit, Types } from '@graphql-codegen/plugin-helpers'; import { getConfigValue, buildScalarsFromConfig } from './utils'; import { LoadedFragment, ParsedImport } from './types'; import { basename, extname } from 'path'; @@ -236,7 +235,7 @@ export class ClientSideBaseVisitor< const names: Set = new Set(); - visit(document, { + oldVisit(document, { enter: { FragmentSpread: (node: FragmentSpreadNode) => { names.add(node.name.value); diff --git a/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts b/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts index ccab5ee38df..ae5958eb390 100644 --- a/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts +++ b/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts @@ -101,7 +101,7 @@ export class SelectionSetToObject> ) { if (isListType(parentType) || isNonNullType(parentType)) { - return this._collectInlineFragments(parentType.ofType, nodes, types); + return this._collectInlineFragments(parentType.ofType as GraphQLNamedType, nodes, types); } else if (isObjectType(parentType)) { for (const node of nodes) { const typeOnSchema = node.typeCondition ? this._schema.getType(node.typeCondition.name.value) : parentType; diff --git a/packages/plugins/other/visitor-plugin-common/src/utils.ts b/packages/plugins/other/visitor-plugin-common/src/utils.ts index dcac435203b..d1831dddf7f 100644 --- a/packages/plugins/other/visitor-plugin-common/src/utils.ts +++ b/packages/plugins/other/visitor-plugin-common/src/utils.ts @@ -308,7 +308,7 @@ export function buildScalars( } else if (scalarType.extensions?.codegenScalarType) { result[name] = { isExternal: false, - type: scalarType.extensions.codegenScalarType, + type: scalarType.extensions.codegenScalarType as string, }; } else if (!defaultScalarsMapping[name]) { if (defaultScalarType === null) { @@ -394,7 +394,7 @@ export function mergeSelectionSets(selectionSet1: SelectionSetNode, selectionSet } return { - kind: 'SelectionSet', + kind: Kind.SELECTION_SET, selections: newSelections, }; } @@ -417,7 +417,7 @@ export function separateSelectionSet(selections: ReadonlyArray): export function getPossibleTypes(schema: GraphQLSchema, type: GraphQLNamedType): GraphQLObjectType[] { if (isListType(type) || isNonNullType(type)) { - return getPossibleTypes(schema, type.ofType); + return getPossibleTypes(schema, type.ofType as GraphQLNamedType); } else if (isObjectType(type)) { return [type]; } else if (isAbstractType(type)) { diff --git a/packages/plugins/typescript/apollo-angular/src/index.ts b/packages/plugins/typescript/apollo-angular/src/index.ts index bbbe9b73a78..656e4f2ed2f 100644 --- a/packages/plugins/typescript/apollo-angular/src/index.ts +++ b/packages/plugins/typescript/apollo-angular/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode, OperationDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode, OperationDefinitionNode } from 'graphql'; import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { ApolloAngularVisitor } from './visitor'; import { extname } from 'path'; @@ -26,7 +26,7 @@ export const plugin: PluginFunction = ( ]; const visitor = new ApolloAngularVisitor(schema, allFragments, operations, config, documents); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/apollo-angular/src/visitor.ts b/packages/plugins/typescript/apollo-angular/src/visitor.ts index b146d31fe6e..d88a110a120 100644 --- a/packages/plugins/typescript/apollo-angular/src/visitor.ts +++ b/packages/plugins/typescript/apollo-angular/src/visitor.ts @@ -251,9 +251,9 @@ export class ApolloAngularVisitor extends ClientSideBaseVisitor< } private _getDocumentNodeVariable(node: OperationDefinitionNode, documentVariableName: string): string { - if(this.config.importDocumentNodeExternallyFrom === 'near-operation-file') { + if (this.config.importDocumentNodeExternallyFrom === 'near-operation-file') { return `Operations.${documentVariableName}`; - } else if( this.config.importOperationTypesFrom) { + } else if (this.config.importOperationTypesFrom) { return `${this.config.importOperationTypesFrom}.${documentVariableName}`; } else { return documentVariableName; diff --git a/packages/plugins/typescript/document-nodes/src/index.ts b/packages/plugins/typescript/document-nodes/src/index.ts index 3b540e37b72..7d936d9536e 100644 --- a/packages/plugins/typescript/document-nodes/src/index.ts +++ b/packages/plugins/typescript/document-nodes/src/index.ts @@ -1,10 +1,10 @@ -import { PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers'; +import { oldVisit, PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers'; import { NamingConvention, LoadedFragment, RawClientSideBasePluginConfig, } from '@graphql-codegen/visitor-plugin-common'; -import { GraphQLSchema, visit, concatAST, FragmentDefinitionNode, Kind } from 'graphql'; +import { GraphQLSchema, concatAST, FragmentDefinitionNode, Kind } from 'graphql'; import { TypeScriptDocumentNodesVisitor } from './visitor'; /** @@ -118,7 +118,7 @@ export const plugin: PluginFunction = ( ]; const visitor = new TypeScriptDocumentNodesVisitor(schema, allFragments, config, documents); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/generic-sdk/src/index.ts b/packages/plugins/typescript/generic-sdk/src/index.ts index af744c25577..402a75bd5b9 100644 --- a/packages/plugins/typescript/generic-sdk/src/index.ts +++ b/packages/plugins/typescript/generic-sdk/src/index.ts @@ -1,6 +1,6 @@ -import { PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers'; +import { oldVisit, PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers'; import { LoadedFragment, RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-common'; -import { concatAST, FragmentDefinitionNode, GraphQLSchema, Kind, visit } from 'graphql'; +import { concatAST, FragmentDefinitionNode, GraphQLSchema, Kind } from 'graphql'; import { extname } from 'path'; import { RawGenericSdkPluginConfig } from './config'; import { GenericSdkVisitor } from './visitor'; @@ -27,7 +27,7 @@ export const plugin: PluginFunction = ( ...(config.externalFragments || []), ]; const visitor = new GenericSdkVisitor(schema, allFragments, config); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/graphql-apollo/src/index.ts b/packages/plugins/typescript/graphql-apollo/src/index.ts index 40cdb06e8a5..78dc7f6750e 100644 --- a/packages/plugins/typescript/graphql-apollo/src/index.ts +++ b/packages/plugins/typescript/graphql-apollo/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { RawClientSideBasePluginConfig, LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { GraphQLApolloVisitor } from './visitor'; import { extname } from 'path'; @@ -23,7 +23,7 @@ export const plugin: PluginFunction = ( ...(config.externalFragments || []), ]; const visitor = new GraphQLApolloVisitor(schema, allFragments, config); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/graphql-request/src/index.ts b/packages/plugins/typescript/graphql-request/src/index.ts index 4fa9ae2d779..1edbe1181af 100644 --- a/packages/plugins/typescript/graphql-request/src/index.ts +++ b/packages/plugins/typescript/graphql-request/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { RawClientSideBasePluginConfig, LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { GraphQLRequestVisitor } from './visitor'; import { extname } from 'path'; @@ -23,7 +23,7 @@ export const plugin: PluginFunction = ( ...(config.externalFragments || []), ]; const visitor = new GraphQLRequestVisitor(schema, allFragments, config); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/jit-sdk/src/index.ts b/packages/plugins/typescript/jit-sdk/src/index.ts index f322dd01629..5a57bd379ed 100644 --- a/packages/plugins/typescript/jit-sdk/src/index.ts +++ b/packages/plugins/typescript/jit-sdk/src/index.ts @@ -1,6 +1,6 @@ -import { PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers'; +import { oldVisit, PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers'; import { LoadedFragment, RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-common'; -import { concatAST, FragmentDefinitionNode, GraphQLSchema, Kind, visit } from 'graphql'; +import { concatAST, FragmentDefinitionNode, GraphQLSchema, Kind } from 'graphql'; import { extname } from 'path'; import { RawJitSdkPluginConfig } from './config'; import { JitSdkVisitor } from './visitor'; @@ -27,7 +27,7 @@ export const plugin: PluginFunction = ( ...(config.externalFragments || []), ]; const visitor = new JitSdkVisitor(schema, allFragments, config); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/jit-sdk/tests/__snapshots__/jit-sdk.spec.ts.snap b/packages/plugins/typescript/jit-sdk/tests/__snapshots__/jit-sdk.spec.ts.snap index f9750c2f8d3..45c82e656fd 100644 --- a/packages/plugins/typescript/jit-sdk/tests/__snapshots__/jit-sdk.spec.ts.snap +++ b/packages/plugins/typescript/jit-sdk/tests/__snapshots__/jit-sdk.spec.ts.snap @@ -477,8 +477,7 @@ enum VoteType { type Subscription { \\"\\"\\"Subscription fires on every comment added\\"\\"\\" commentAdded(repoFullName: String!): Comment -} -\`); +}\`); const sdk = getJitSdk(schema); await sdk.feed(); @@ -1215,8 +1214,7 @@ enum VoteType { type Subscription { \\"\\"\\"Subscription fires on every comment added\\"\\"\\" commentAdded(repoFullName: String!): Comment -} -\`); +}\`); const sdk = getJitSdk(schema); await sdk.feed(); diff --git a/packages/plugins/typescript/jit-sdk/tests/jit-sdk.spec.ts b/packages/plugins/typescript/jit-sdk/tests/jit-sdk.spec.ts index aa3a5c8612a..4a5f257b9f3 100644 --- a/packages/plugins/typescript/jit-sdk/tests/jit-sdk.spec.ts +++ b/packages/plugins/typescript/jit-sdk/tests/jit-sdk.spec.ts @@ -94,7 +94,7 @@ describe('jit-sdk', () => { const usage = ` async function test() { - const schema = buildSchema(\`${printSchema(schema)}\`); + const schema = buildSchema(\`${printSchema(schema).trim()}\`); const sdk = getJitSdk(schema); await sdk.feed(); @@ -123,7 +123,7 @@ async function test() { const usage = ` async function test() { - const schema = buildSchema(\`${printSchema(schema)}\`); + const schema = buildSchema(\`${printSchema(schema).trim()}\`); const sdk = getJitSdk(schema); await sdk.feed(); diff --git a/packages/plugins/typescript/mongodb/src/index.ts b/packages/plugins/typescript/mongodb/src/index.ts index 35eba42dabb..0b0741fe4c3 100644 --- a/packages/plugins/typescript/mongodb/src/index.ts +++ b/packages/plugins/typescript/mongodb/src/index.ts @@ -3,8 +3,9 @@ import { PluginFunction, PluginValidateFn, getCachedDocumentNodeFromSchema, + oldVisit, } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema } from 'graphql'; +import { GraphQLSchema } from 'graphql'; import { extname } from 'path'; import gql from 'graphql-tag'; import { TsMongoVisitor } from './visitor'; @@ -17,7 +18,7 @@ export const plugin: PluginFunction = ( ) => { const visitor = new TsMongoVisitor(schema, config); const astNode = getCachedDocumentNodeFromSchema(schema); - const visitorResult = visit(astNode, { leave: visitor as any }); + const visitorResult = oldVisit(astNode, { leave: visitor }); const header = visitor.objectIdImport; return [header, ...visitorResult.definitions.filter(d => typeof d === 'string')].join('\n'); diff --git a/packages/plugins/typescript/mongodb/src/visitor.ts b/packages/plugins/typescript/mongodb/src/visitor.ts index e4b762d9932..03609c56281 100644 --- a/packages/plugins/typescript/mongodb/src/visitor.ts +++ b/packages/plugins/typescript/mongodb/src/visitor.ts @@ -55,7 +55,7 @@ function resolveObjectId(pointer: string | null | undefined): { identifier: stri export class TsMongoVisitor extends BaseVisitor { constructor(private _schema: GraphQLSchema, pluginConfig: TypeScriptMongoPluginConfig) { - super(pluginConfig, ({ + super(pluginConfig, { dbTypeSuffix: pluginConfig.dbTypeSuffix || 'DbObject', dbInterfaceSuffix: pluginConfig.dbInterfaceSuffix || 'DbInterface', objectIdType: resolveObjectId(pluginConfig.objectIdType).identifier, @@ -64,7 +64,7 @@ export class TsMongoVisitor extends BaseVisitor(pluginConfig.enumsAsString, true), avoidOptionals: getConfigValue(pluginConfig.avoidOptionals, false), scalars: buildScalarsFromConfig(_schema, pluginConfig), - } as Partial) as any); + } as Partial as any); autoBind(this); } @@ -83,9 +83,9 @@ export class TsMongoVisitor extends BaseVisitor this._resolveDirectiveValue(v)) as any) as T; + return valueNode.values.map(v => this._resolveDirectiveValue(v)) as any as T; case Kind.NULL: return null; case Kind.OBJECT: diff --git a/packages/plugins/typescript/named-operations-object/src/index.ts b/packages/plugins/typescript/named-operations-object/src/index.ts index 2d015cddb64..2d57dd33a99 100644 --- a/packages/plugins/typescript/named-operations-object/src/index.ts +++ b/packages/plugins/typescript/named-operations-object/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { GraphQLSchema, concatAST, visit } from 'graphql'; +import { Types, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST } from 'graphql'; import { capitalCase } from 'change-case-all'; export interface NamedOperationsObjectPluginConfig { @@ -40,7 +40,7 @@ export const plugin: PluginFunction = fragment: new Set(), }; - visit(allAst, { + oldVisit(allAst, { enter: { OperationDefinition: node => { if (node.name?.value) { diff --git a/packages/plugins/typescript/oclif/src/index.ts b/packages/plugins/typescript/oclif/src/index.ts index 85365285d1f..4fbee63bb3d 100644 --- a/packages/plugins/typescript/oclif/src/index.ts +++ b/packages/plugins/typescript/oclif/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { RawClientSideBasePluginConfig, LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { GraphQLRequestVisitor } from './visitor'; import { extname } from 'path'; @@ -36,7 +36,7 @@ export const plugin: PluginFunction = ( ...(config.externalFragments || []), ]; const visitor = new GraphQLRequestVisitor(schema, allFragments, config, info); - visit(allAst, { leave: visitor }); + oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/operations/src/index.ts b/packages/plugins/typescript/operations/src/index.ts index 4ebaf730597..ae22ef6ee9c 100644 --- a/packages/plugins/typescript/operations/src/index.ts +++ b/packages/plugins/typescript/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 { TypeScriptDocumentsVisitor } from './visitor'; import { LoadedFragment, optimizeOperations } from '@graphql-codegen/visitor-plugin-common'; import { TypeScriptDocumentsPluginConfig } from './config'; @@ -28,7 +28,7 @@ export const plugin: PluginFunction { text @skip(if: $skip) } `, - { experimentalFragmentVariables: true } + // < v15 compatibility + { experimentalFragmentVariables: true, allowLegacyFragmentVariables: true } as any ); const config = { experimentalFragmentVariables: true }; const { content } = await plugin(schema, [{ location: 'test-file.ts', document: ast }], config, { diff --git a/packages/plugins/typescript/react-apollo-offix/src/index.ts b/packages/plugins/typescript/react-apollo-offix/src/index.ts index bad24b9bc2b..2cd8914d02b 100644 --- a/packages/plugins/typescript/react-apollo-offix/src/index.ts +++ b/packages/plugins/typescript/react-apollo-offix/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { LoadedFragment, RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-common'; import { ReactApolloVisitor } from './visitor'; import { extname } from 'path'; @@ -24,7 +24,7 @@ export const plugin: PluginFunction = ( ]; const visitor = new ReactApolloVisitor(schema, allFragments, config, documents); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/react-apollo/src/index.ts b/packages/plugins/typescript/react-apollo/src/index.ts index 70504ee819a..ffe90ce85c1 100644 --- a/packages/plugins/typescript/react-apollo/src/index.ts +++ b/packages/plugins/typescript/react-apollo/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { ReactApolloVisitor } from './visitor'; import { extname } from 'path'; @@ -25,7 +25,7 @@ export const plugin: PluginFunction `${(v.name as any) as string}${this.config.avoidOptionals ? '' : '?'}: any`) + .map(v => `${v.name as any as string}${this.config.avoidOptionals ? '' : '?'}: any`) .join(', ')} }`; this._globalDeclarations.add(ENUM_RESOLVERS_SIGNATURE); @@ -132,7 +132,7 @@ export class TypeScriptResolversVisitor extends BaseResolversVisitor< ): 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}'`}`; diff --git a/packages/plugins/typescript/rtk-query/src/index.ts b/packages/plugins/typescript/rtk-query/src/index.ts index 44b75d3c061..ce118c6d08e 100644 --- a/packages/plugins/typescript/rtk-query/src/index.ts +++ b/packages/plugins/typescript/rtk-query/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { RTKQueryVisitor } from './visitor'; import { extname } from 'path'; @@ -25,7 +25,7 @@ export const plugin: PluginFunction = ( ...(config.externalFragments || []), ]; - const visitor = new StencilApolloVisitor(schema, allFragments, config) as any; - const visitorResult = visit(allAst, { leave: visitor }); + const visitor = new StencilApolloVisitor(schema, allFragments, config); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: visitor.getImports(), diff --git a/packages/plugins/typescript/type-graphql/src/index.ts b/packages/plugins/typescript/type-graphql/src/index.ts index a9f4a3ea7c6..a3acad3e855 100644 --- a/packages/plugins/typescript/type-graphql/src/index.ts +++ b/packages/plugins/typescript/type-graphql/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 { TypeGraphQLVisitor } from './visitor'; import { TsIntrospectionVisitor, includeIntrospectionDefinitions } from '@graphql-codegen/typescript'; import { TypeGraphQLPluginConfig } from './config'; @@ -16,7 +16,7 @@ export const plugin: PluginFunction { const visitor = new TypeGraphQLVisitor(schema, config); const astNode = getCachedDocumentNodeFromSchema(schema); - const visitorResult = visit(astNode, { leave: visitor }); + const visitorResult = oldVisit(astNode, { leave: visitor }); const introspectionDefinitions = includeIntrospectionDefinitions(schema, documents, config); const scalars = visitor.scalarsDefinition; diff --git a/packages/plugins/typescript/type-graphql/src/visitor.ts b/packages/plugins/typescript/type-graphql/src/visitor.ts index 16660b50354..6be191bfde9 100644 --- a/packages/plugins/typescript/type-graphql/src/visitor.ts +++ b/packages/plugins/typescript/type-graphql/src/visitor.ts @@ -162,7 +162,7 @@ export class TypeGraphQLVisitor< if (node.description) { // Add description as TypeGraphQL description instead of comment - decoratorOptions.description = escapeString((node.description as unknown) as string); + decoratorOptions.description = escapeString(node.description as unknown as string); (node as any).description = undefined; } @@ -200,8 +200,8 @@ export class TypeGraphQLVisitor< } ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string { - const isGraphQLType = GRAPHQL_TYPES.includes((node.name as unknown) as string); - if (!isGraphQLType && !this.hasTypeDecorators((node.name as unknown) as string)) { + const isGraphQLType = GRAPHQL_TYPES.includes(node.name as unknown as string); + if (!isGraphQLType && !this.hasTypeDecorators(node.name as unknown as string)) { return this.typescriptVisitor.ObjectTypeDefinition(node, key, parent); } @@ -232,7 +232,7 @@ export class TypeGraphQLVisitor< } InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string { - if (!this.hasTypeDecorators((node.name as unknown) as string)) { + if (!this.hasTypeDecorators(node.name as unknown as string)) { return this.typescriptVisitor.InputObjectTypeDefinition(node); } @@ -279,7 +279,7 @@ export class TypeGraphQLVisitor< } InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode, key: number | string, parent: any): string { - if (!this.hasTypeDecorators((node.name as unknown) as string)) { + if (!this.hasTypeDecorators(node.name as unknown as string)) { return this.typescriptVisitor.InterfaceTypeDefinition(node, key, parent); } @@ -377,7 +377,7 @@ export class TypeGraphQLVisitor< } const fieldDecorator = this.config.decoratorName.field; - let typeString = (node.type as any) as string; + let typeString = node.type as any as string; const type = this.parseType(typeString); @@ -447,7 +447,7 @@ export class TypeGraphQLVisitor< } EnumTypeDefinition(node: EnumTypeDefinitionNode): string { - if (!this.hasTypeDecorators((node.name as unknown) as string)) { + if (!this.hasTypeDecorators(node.name as unknown as string)) { return this.typescriptVisitor.EnumTypeDefinition(node); } diff --git a/packages/plugins/typescript/typed-document-node/src/index.ts b/packages/plugins/typescript/typed-document-node/src/index.ts index 3065a704b89..b30c713d383 100644 --- a/packages/plugins/typescript/typed-document-node/src/index.ts +++ b/packages/plugins/typescript/typed-document-node/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, concatAST, GraphQLSchema, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { concatAST, GraphQLSchema, Kind, FragmentDefinitionNode } from 'graphql'; import { TypeScriptTypedDocumentNodesConfig } from './config'; import { extname } from 'path'; import { @@ -31,7 +31,7 @@ export const plugin: PluginFunction = ( ]; const visitor = new TypeScriptDocumentNodesVisitor(schema, allFragments, config, documents); - const visitorResult = visit(allAst, { leave: visitor }); + const visitorResult = oldVisit(allAst, { leave: visitor }); return { prepend: allAst.definitions.length === 0 ? [] : visitor.getImports(), diff --git a/packages/plugins/typescript/typescript/src/index.ts b/packages/plugins/typescript/typescript/src/index.ts index d78b530c4f5..5b85c9d5211 100644 --- a/packages/plugins/typescript/typescript/src/index.ts +++ b/packages/plugins/typescript/typescript/src/index.ts @@ -1,4 +1,4 @@ -import { Types, PluginFunction } from '@graphql-codegen/plugin-helpers'; +import { Types, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; import { parse, visit, @@ -31,7 +31,7 @@ export const plugin: PluginFunction; const unwrapType = (type: null | undefined | TypeNode): GraphQLFlatType | null => - isWrappingType(type) ? unwrapType(type.ofType) : type || null; + isWrappingType(type) ? unwrapType(type.ofType as any) : type || null; const getObjectTypes = (schema: GraphQLSchema): GraphQLObjectType[] => { const typeMap = schema.getTypeMap(); @@ -60,13 +61,13 @@ function constructType( allowString = false ): string { switch (typeNode.kind) { - case 'ListType': { + case Kind.LIST_TYPE: { return nullable ? `Maybe>` : `Array<${constructType(typeNode.type, schema, convertName, config, false, allowString)}>`; } - case 'NamedType': { + case Kind.NAMED_TYPE: { const type = schema.getType(typeNode.name.value); if (!type.astNode || type?.astNode?.kind === 'ScalarTypeDefinition') { return nullable @@ -76,19 +77,19 @@ function constructType( const tsTypeName = convertName(typeNode, { prefix: config.typesPrefix, suffix: config.typesSuffix }); switch (type.astNode.kind) { - case 'UnionTypeDefinition': - case 'InputObjectTypeDefinition': - case 'ObjectTypeDefinition': { + case Kind.UNION_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + case Kind.OBJECT_TYPE_DEFINITION: { const finalType = `WithTypename<${tsTypeName}>${allowString ? ' | string' : ''}`; return nullable ? `Maybe<${finalType}>` : finalType; } - case 'EnumTypeDefinition': { + case Kind.ENUM_TYPE_DEFINITION: { const finalType = `${tsTypeName}${allowString ? ' | string' : ''}`; return nullable ? `Maybe<${finalType}>` : finalType; } - case 'InterfaceTypeDefinition': { + case Kind.INTERFACE_TYPE_DEFINITION: { const possibleTypes = schema.getPossibleTypes(type as GraphQLAbstractType).map(possibleType => { const tsPossibleTypeName = convertName(possibleType.astNode, { prefix: config.typesPrefix, @@ -104,7 +105,7 @@ function constructType( break; } - case 'NonNullType': { + case Kind.NON_NULL_TYPE: { return constructType(typeNode.type, schema, convertName, config, false, allowString); } } diff --git a/packages/plugins/typescript/urql/src/index.ts b/packages/plugins/typescript/urql/src/index.ts index 7715b58ff9f..8f6f8612cc9 100644 --- a/packages/plugins/typescript/urql/src/index.ts +++ b/packages/plugins/typescript/urql/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { UrqlVisitor } from './visitor'; import { extname } from 'path'; @@ -22,8 +22,8 @@ export const plugin: PluginFunction { ((await plugin(schema, [{ location: 'test-file.ts', document: ast }], {}, { outputFile: '' })) as any).content ).toContain('VueApolloComposable.UseQueryReturn'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: ast }], - { dedupeOperationSuffix: false }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: ast }], + { dedupeOperationSuffix: false }, + { outputFile: '' } + )) as any + ).content ).toContain('VueApolloComposable.UseQueryReturn'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: ast }], - { dedupeOperationSuffix: true }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: ast }], + { dedupeOperationSuffix: true }, + { outputFile: '' } + )) as any + ).content ).toContain('VueApolloComposable.UseQueryReturn'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: ast2 }], - { dedupeOperationSuffix: true }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: ast2 }], + { dedupeOperationSuffix: true }, + { outputFile: '' } + )) as any + ).content ).toContain('VueApolloComposable.UseQueryReturn'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: ast2 }], - { dedupeOperationSuffix: false }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: ast2 }], + { dedupeOperationSuffix: false }, + { outputFile: '' } + )) as any + ).content ).toContain('VueApolloComposable.UseQueryReturn'); }); @@ -471,44 +479,54 @@ query MyFeed { `); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: documentWithHardcodedQuerySuffix }], - {}, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: documentWithHardcodedQuerySuffix }], + {}, + { outputFile: '' } + )) as any + ).content ).toContain('type NotificationsQueryQueryCompositionFunctionResult'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: documentWithHardcodedQuerySuffix }], - { omitOperationSuffix: false }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: documentWithHardcodedQuerySuffix }], + { omitOperationSuffix: false }, + { outputFile: '' } + )) as any + ).content ).toContain('type NotificationsQueryQueryCompositionFunctionResult'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: documentWithHardcodedQuerySuffix }], - { omitOperationSuffix: true }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: documentWithHardcodedQuerySuffix }], + { omitOperationSuffix: true }, + { outputFile: '' } + )) as any + ).content ).toContain('type NotificationsQueryCompositionFunctionResult'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: documentNoQuerySuffix }], - { omitOperationSuffix: true }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: documentNoQuerySuffix }], + { omitOperationSuffix: true }, + { outputFile: '' } + )) as any + ).content ).toContain('type NotificationsCompositionFunctionResult'); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: documentNoQuerySuffix }], - { omitOperationSuffix: false }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: documentNoQuerySuffix }], + { omitOperationSuffix: false }, + { outputFile: '' } + )) as any + ).content ).toContain('type NotificationsQueryCompositionFunctionResult'); }); @@ -564,12 +582,14 @@ query MyFeed { } `); expect( - ((await plugin( - schema, - [{ location: 'test-file.ts', document: documentWithQuerySuffix }], - { omitOperationSuffix: true, dedupeOperationSuffix: true }, - { outputFile: '' } - )) as any).content + ( + (await plugin( + schema, + [{ location: 'test-file.ts', document: documentWithQuerySuffix }], + { omitOperationSuffix: true, dedupeOperationSuffix: true }, + { outputFile: '' } + )) as any + ).content ).toContain('type NotificationsQueryCompositionFunctionResult'); }); diff --git a/packages/plugins/typescript/vue-urql/src/index.ts b/packages/plugins/typescript/vue-urql/src/index.ts index 910a45e411e..36c3428c6a9 100644 --- a/packages/plugins/typescript/vue-urql/src/index.ts +++ b/packages/plugins/typescript/vue-urql/src/index.ts @@ -1,5 +1,5 @@ -import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers'; -import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; +import { Types, PluginValidateFn, PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers'; +import { GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql'; import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common'; import { UrqlVisitor } from './visitor'; import { extname } from 'path'; @@ -22,8 +22,8 @@ export const plugin: PluginFunction = { throw new Error(`Preset "graphql-modules" requires to use GraphQL SDL`); } - const sourcesByModuleMap = groupSourcesByModule(options.schemaAst!.extensions.extendedSources, baseOutputDir); + const extensions: any = options.schemaAst!.extensions; + const sourcesByModuleMap = groupSourcesByModule(extensions.extendedSources, baseOutputDir); const modules = Object.keys(sourcesByModuleMap); const baseVisitor = new BaseVisitor(options.config, {}); diff --git a/packages/presets/near-operation-file/src/index.ts b/packages/presets/near-operation-file/src/index.ts index 7190ae34400..9f3181a3405 100644 --- a/packages/presets/near-operation-file/src/index.ts +++ b/packages/presets/near-operation-file/src/index.ts @@ -2,7 +2,7 @@ import { Types, CodegenPlugin } from '@graphql-codegen/plugin-helpers'; import type { Source } from '@graphql-tools/utils'; import addPlugin from '@graphql-codegen/add'; import { join } from 'path'; -import { FragmentDefinitionNode, buildASTSchema, GraphQLSchema } from 'graphql'; +import { FragmentDefinitionNode, buildASTSchema, GraphQLSchema, DocumentNode, Kind } from 'graphql'; import { appendExtensionToFilePath, defineFilepathSubfolder } from './utils'; import { resolveDocumentImports, DocumentImportResolverOptions } from './resolve-document-imports'; import { @@ -248,9 +248,10 @@ export const preset: Types.OutputPreset = { fragmentImports: fragmentImportsArr, }; + const document: DocumentNode = { kind: Kind.DOCUMENT, definitions: [] }; const combinedSource: Source = { rawSDL: '', - document: { kind: 'Document', definitions: [] }, + document: document, location: record.documents[0].location, }; diff --git a/packages/presets/near-operation-file/src/utils.ts b/packages/presets/near-operation-file/src/utils.ts index a6e16a8e8b3..31110c47ab1 100644 --- a/packages/presets/near-operation-file/src/utils.ts +++ b/packages/presets/near-operation-file/src/utils.ts @@ -1,7 +1,8 @@ import { join } from 'path'; -import { DocumentNode, visit, FragmentSpreadNode, FragmentDefinitionNode } from 'graphql'; +import { DocumentNode, FragmentSpreadNode, FragmentDefinitionNode } from 'graphql'; import { FragmentRegistry } from './fragment-resolver'; import parsePath from 'parse-filepath'; +import { oldVisit } from '@graphql-codegen/plugin-helpers'; export function defineFilepathSubfolder(baseFilePath: string, folder: string) { const parsedPath = parsePath(baseFilePath); @@ -23,7 +24,7 @@ export function extractExternalFragmentsInUse( const ignoreList: Set = new Set(); // First, take all fragments definition from the current file, and mark them as ignored - visit(documentNode, { + oldVisit(documentNode, { enter: { FragmentDefinition: (node: FragmentDefinitionNode) => { ignoreList.add(node.name.value); @@ -32,7 +33,7 @@ export function extractExternalFragmentsInUse( }); // Then, look for all used fragments in this document - visit(documentNode, { + oldVisit(documentNode, { enter: { FragmentSpread: (node: FragmentSpreadNode) => { if (!ignoreList.has(node.name.value)) { diff --git a/packages/utils/graphql-codegen-testing/src/mock-graphql-server.ts b/packages/utils/graphql-codegen-testing/src/mock-graphql-server.ts index 4130c163b59..ac56e3d0e18 100644 --- a/packages/utils/graphql-codegen-testing/src/mock-graphql-server.ts +++ b/packages/utils/graphql-codegen-testing/src/mock-graphql-server.ts @@ -1,4 +1,4 @@ -import { GraphQLSchema } from 'graphql'; +import { GraphQLSchema, execute, subscribe } from 'graphql'; import nock from 'nock'; import { getGraphQLParameters, processRequest as processGraphQLHelixRequest } from 'graphql-helix'; @@ -39,6 +39,24 @@ export function mockGraphQLServer({ variables, request, schema, + execute: (schema, document, rootValue, contextValue, variableValues, operationName) => + execute({ + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + }), + subscribe: (schema, document, rootValue, contextValue, variableValues, operationName) => + subscribe({ + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + }), }); // processRequest returns one of three types of results depending on how the server should respond // 1) RESPONSE: a regular JSON payload diff --git a/packages/utils/plugins-helpers/src/federation.ts b/packages/utils/plugins-helpers/src/federation.ts index 2d9e4c65f20..ac46cd22923 100644 --- a/packages/utils/plugins-helpers/src/federation.ts +++ b/packages/utils/plugins-helpers/src/federation.ts @@ -8,13 +8,13 @@ import { GraphQLObjectType, isObjectType, GraphQLNamedType, - visit, DefinitionNode, OperationDefinitionNode, } from 'graphql'; import merge from 'lodash/merge.js'; import { getBaseType } from './utils'; import { MapperKind, mapSchema, astFromObjectType, getRootTypeNames } from '@graphql-tools/utils'; +import { oldVisit } from '.'; /** * Federation Spec @@ -223,7 +223,7 @@ export class ApolloFederation { selection: boolean | SelectionSetField[]; }; - return visit(parse(`{${value}}`), { + return oldVisit(parse(`{${value}}`), { leave: { SelectionSet(node) { return (node.selections as any as SelectionSetField[]).reduce((accum, field) => { diff --git a/packages/utils/plugins-helpers/src/helpers.ts b/packages/utils/plugins-helpers/src/helpers.ts index 22b79709c94..a743ce65c6f 100644 --- a/packages/utils/plugins-helpers/src/helpers.ts +++ b/packages/utils/plugins-helpers/src/helpers.ts @@ -191,8 +191,8 @@ export function isUsingTypes(document: DocumentNode, externalFragments: string[] } }, }, - enter: { - VariableDefinition: (node: VariableDefinitionNode, key, parent, path, anscestors) => { + VariableDefinition: { + enter: (node: VariableDefinitionNode, key, parent, path, anscestors) => { const insideIgnoredFragment = (anscestors as any).find( (f: ASTNode) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value) ); @@ -202,7 +202,9 @@ export function isUsingTypes(document: DocumentNode, externalFragments: string[] } foundFields++; }, - InputValueDefinition: (node: InputValueDefinitionNode, key, parent, path, anscestors) => { + }, + InputValueDefinition: { + enter: (node: InputValueDefinitionNode, key, parent, path, anscestors) => { const insideIgnoredFragment = (anscestors as any).find( (f: ASTNode) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value) ); diff --git a/packages/utils/plugins-helpers/src/index.ts b/packages/utils/plugins-helpers/src/index.ts index 83a5221263a..13efe90dfdd 100644 --- a/packages/utils/plugins-helpers/src/index.ts +++ b/packages/utils/plugins-helpers/src/index.ts @@ -5,3 +5,4 @@ export * from './helpers'; export * from './federation'; export * from './errors'; export * from './getCachedDocumentNodeFromSchema'; +export * from './oldVisit'; diff --git a/packages/utils/plugins-helpers/src/oldVisit.ts b/packages/utils/plugins-helpers/src/oldVisit.ts new file mode 100644 index 00000000000..bbe63e12156 --- /dev/null +++ b/packages/utils/plugins-helpers/src/oldVisit.ts @@ -0,0 +1,27 @@ +import { ASTNode, visit } from 'graphql'; + +type VisitFn = typeof visit; +type NewVisitor = Partial[1]>; +type OldVisitor = { + enter?: Partial>; + leave?: Partial>; +} & NewVisitor; + +export function oldVisit( + root: ASTNode, + { enter: enterVisitors, leave: leaveVisitors, ...newVisitor }: OldVisitor +): any { + if (typeof enterVisitors === 'object') { + for (const key in enterVisitors) { + newVisitor[key] = newVisitor[key] || {}; + newVisitor[key].enter = enterVisitors[key]; + } + } + if (typeof leaveVisitors === 'object') { + for (const key in leaveVisitors) { + newVisitor[key] = newVisitor[key] || {}; + newVisitor[key].leave = leaveVisitors[key]; + } + } + return visit(root, newVisitor); +} diff --git a/packages/utils/plugins-helpers/src/utils.ts b/packages/utils/plugins-helpers/src/utils.ts index 20c27a654f1..d226c3d05c2 100644 --- a/packages/utils/plugins-helpers/src/utils.ts +++ b/packages/utils/plugins-helpers/src/utils.ts @@ -31,6 +31,6 @@ export function getBaseType(type: GraphQLOutputType): GraphQLNamedType { } } -export function removeNonNullWrapper(type: GraphQLOutputType): GraphQLNamedType { +export function removeNonNullWrapper(type: GraphQLOutputType): GraphQLOutputType { return isNonNullType(type) ? type.ofType : type; } diff --git a/scripts/match-graphql.js b/scripts/match-graphql.js new file mode 100644 index 00000000000..d3564058341 --- /dev/null +++ b/scripts/match-graphql.js @@ -0,0 +1,21 @@ +const { writeFileSync } = require('fs'); +const { resolve } = require('path'); +const { argv, cwd } = require('process'); + +const pkgPath = resolve(cwd(), './package.json'); + +const pkg = require(pkgPath); + +const version = argv[2]; + +pkg.resolutions = pkg.resolutions || {}; +if (pkg.resolutions.graphql.startsWith(version)) { + console.info(`GraphQL v${version} already installed! Skipping.`); +} + +pkg.devDependencies.graphql = `^${version}`; +pkg.resolutions.graphql = `^${version}`; +pkg.resolutions['**/apollo-language-server/graphql'] = `^${version}`; +pkg.resolutions['**/@types/graphql-upload/graphql'] = `^${version}`; + +writeFileSync(pkgPath, JSON.stringify(pkg, null, 2), 'utf8'); diff --git a/yarn.lock b/yarn.lock index 221bf1b5613..e9a87239b1b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8163,11 +8163,16 @@ graphql-ws@^5.4.1: resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.5.0.tgz#79f10248d23d104369eaef93acb9f887276a2c42" integrity sha512-WQepPMGQQoqS2VsrI2I3RMLCVz3CW4/6ZqGV6ABDOwH4R62DzjxwMlwZbj6vhSI/7IM3/C911yITwgs77iO/hw== -graphql@15.6.1, graphql@^15.0.0, graphql@^15.5.1: +graphql@15.6.1, graphql@^15.5.1: version "15.6.1" resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.6.1.tgz#9125bdf057553525da251e19e96dab3d3855ddfc" integrity sha512-3i5lu0z6dRvJ48QP9kFxBkJ7h4Kso7PS8eahyTFz5Jm6CvQfLtNIE8LX9N6JLnXTuwR+sIYnXzaWp6anOg0QQw== +graphql@16.0.0-rc.5: + version "16.0.0-rc.5" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.0.0-rc.5.tgz#0fe8b93890f5e5362ba24a2d743b3ddc964b4b8d" + integrity sha512-JPOT10OJL4N19Odb1IcAnE4dky9qtwXgmjdPnBu0vtWx+/qHxwM5YdEkACpJ8W/zDMKQ24xtZeMDmCZ4fvZZBg== + gray-matter@^4.0.3: version "4.0.3" resolved "https://registry.yarnpkg.com/gray-matter/-/gray-matter-4.0.3.tgz#e893c064825de73ea1f5f7d88c7a9f7274288798" @@ -13657,12 +13662,12 @@ rxjs@^7.1.0: dependencies: tslib "~2.1.0" -safe-buffer@5.1.2, safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: +safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== -safe-buffer@^5.2.0, safe-buffer@~5.2.0: +safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0: version "5.2.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==