From 2a8e14fb2799909aac7885d6819c717b03f1a6c6 Mon Sep 17 00:00:00 2001 From: Kyohei Nakamori Date: Tue, 21 Feb 2023 09:00:25 +0900 Subject: [PATCH] Remove `addToSchema` in document transform --- .changeset/short-toes-relax.md | 3 +- .../graphql-codegen-cli/tests/codegen.spec.ts | 109 +++--------------- packages/graphql-codegen-core/src/codegen.ts | 22 +--- packages/utils/plugins-helpers/src/types.ts | 9 -- .../docs/advanced/document-transform.mdx | 7 +- 5 files changed, 19 insertions(+), 131 deletions(-) diff --git a/.changeset/short-toes-relax.md b/.changeset/short-toes-relax.md index d1d8d2f4853..d352658a3ed 100644 --- a/.changeset/short-toes-relax.md +++ b/.changeset/short-toes-relax.md @@ -9,7 +9,7 @@ Introduce a new feature called DocumentTransform. -DocumentTransform is a functionality that allows you to modify `documents` before they are processed by plugins. You can use functions passed to the `documentTransforms` option to make changes to GraphQL documents or extend the schema, as needed. +DocumentTransform is a functionality that allows you to modify `documents` before they are processed by plugins. You can use functions passed to the `documentTransforms` option to make changes to GraphQL documents. To use this feature, you can write `documentTransforms` as follows: @@ -28,7 +28,6 @@ const config: CodegenConfig = { // Make some changes to the documents return documents; }, - addToSchema: 'extend type Query { test: String! }', }, ], }, diff --git a/packages/graphql-codegen-cli/tests/codegen.spec.ts b/packages/graphql-codegen-cli/tests/codegen.spec.ts index 0034eec334f..e15a70bbf45 100644 --- a/packages/graphql-codegen-cli/tests/codegen.spec.ts +++ b/packages/graphql-codegen-cli/tests/codegen.spec.ts @@ -1272,33 +1272,8 @@ describe('Codegen Executor', () => { } }); - it('should be able to extend the schema.', async () => { - const documentTransform: Types.DocumentTransformObject = { - transform: ({ documents }) => { - return documents; - }, - addToSchema: `extend type Query { test: String! }`, - }; - - const output = await executeCodegen({ - schema: SIMPLE_TEST_SCHEMA, - documents: `query foo { f }`, - generates: { - 'out1.ts': { - plugins: ['typescript', 'typescript-operations'], - documentTransforms: [documentTransform], - }, - }, - }); - - expect(output.length).toBe(1); - expect(output[0].content).toContain(`test: Scalars['String']`); - }); - }); - - it('should be able to dynamically extend the schema.', async () => { - const documentTransform: Types.DocumentTransformObject = { - transform: ({ documents }) => { + it('Should transform documents with client-preset', async () => { + const transform: Types.DocumentTransformFunction = ({ documents }) => { const newDocuments = [ { document: { @@ -1306,84 +1281,28 @@ describe('Codegen Executor', () => { definitions: [ { ...documents[0].document.definitions[0], - selectionSet: { - kind: Kind.SELECTION_SET, - selections: [ - { - kind: Kind.FIELD, - name: { kind: Kind.NAME, value: 'test' }, - }, - ], - }, + name: { kind: Kind.NAME, value: 'bar' }, } as OperationDefinitionNode, ], }, }, ]; return newDocuments; - }, - addToSchema: ({ schema, schemaAst, documents }) => { - // Check that the arguments schema, schemaAST, and documents exist correctly. - // These following are only needed as a test and is of no importance for the extension of the schema in this case. - const fieldObjectTypeFromSchema = schema.definitions.find( - node => node.kind === Kind.OBJECT_TYPE_DEFINITION && node.fields.find(field => field.name.value === 'f') - ); - const fieldFromSchemaAST = schemaAst.getQueryType().getFields()['f']; - const operationDefinitionNode = documents[0].document!.definitions[0] as OperationDefinitionNode; - - if (operationDefinitionNode.name.value === 'foo' && fieldObjectTypeFromSchema && fieldFromSchemaAST) { - return `extend type Query { test: String! }`; - } - return ''; - }, - }; - - const output = await executeCodegen({ - schema: SIMPLE_TEST_SCHEMA, - documents: `query foo { f }`, - generates: { - 'out1.ts': { - plugins: ['typescript', 'typescript-operations'], - documentTransforms: [documentTransform], - }, - }, - }); - - expect(output.length).toBe(1); - expect(output[0].content).toContain(`test: Scalars['String']`); - expect(output[0].content).toContain(`export type FooQuery = { __typename?: 'Query', test: string }`); - }); + }; - it('Should transform documents with client-preset', async () => { - const transform: Types.DocumentTransformFunction = ({ documents }) => { - const newDocuments = [ - { - document: { - ...documents[0].document, - definitions: [ - { - ...documents[0].document.definitions[0], - name: { kind: Kind.NAME, value: 'bar' }, - } as OperationDefinitionNode, - ], + const output = await executeCodegen({ + schema: SIMPLE_TEST_SCHEMA, + documents: `query foo { f }`, + generates: { + './src/gql/': { + preset: 'client', + documentTransforms: [{ transform }], }, }, - ]; - return newDocuments; - }; + }); - const output = await executeCodegen({ - schema: SIMPLE_TEST_SCHEMA, - documents: `query foo { f }`, - generates: { - './src/gql/': { - preset: 'client', - documentTransforms: [{ transform }], - }, - }, + const fileOutput = output.find(file => file.filename === './src/gql/graphql.ts'); + expect(fileOutput.content).toContain('export type BarQuery'); }); - - const fileOutput = output.find(file => file.filename === './src/gql/graphql.ts'); - expect(fileOutput.content).toContain('export type BarQuery'); }); }); diff --git a/packages/graphql-codegen-core/src/codegen.ts b/packages/graphql-codegen-core/src/codegen.ts index b325109ffc0..e1da67b94ee 100644 --- a/packages/graphql-codegen-core/src/codegen.ts +++ b/packages/graphql-codegen-core/src/codegen.ts @@ -44,27 +44,6 @@ export async function codegen(options: Types.GenerateOptions): Promise { } } - const documentTransforms = Array.isArray(options.documentTransforms) ? options.documentTransforms : []; - for (const documentTransform of documentTransforms) { - const addToSchema = documentTransform.transformObject.addToSchema; - const partialSchema = - typeof addToSchema === 'function' - ? addToSchema({ - schema: options.schema, - schemaAst: options.schemaAst, - documents: options.documents, - config: { - ...options.config, - ...documentTransform.config, - }, - pluginContext: options.pluginContext, - }) - : addToSchema; - if (partialSchema) { - additionalTypeDefs.push(partialSchema); - } - } - const federationInConfig: boolean = pickFlag('federation', options.config); const isFederation = prioritize(federationInConfig, false); @@ -93,6 +72,7 @@ export async function codegen(options: Types.GenerateOptions): Promise { const schemaDocumentNode = mergeNeeded || !options.schema ? getCachedDocumentNodeFromSchema(schemaInstance) : options.schema; + const documentTransforms = Array.isArray(options.documentTransforms) ? options.documentTransforms : []; const transformedDocuments = await transformDocuments({ ...options, documentTransforms, diff --git a/packages/utils/plugins-helpers/src/types.ts b/packages/utils/plugins-helpers/src/types.ts index 3a15b347ce4..60446962171 100644 --- a/packages/utils/plugins-helpers/src/types.ts +++ b/packages/utils/plugins-helpers/src/types.ts @@ -624,17 +624,8 @@ export namespace Types { pluginContext?: { [key: string]: any }; }) => Types.Promisable; - export type DocumentTransformAddToSchemaFunction = (options: { - documents: Types.DocumentFile[]; - schema: DocumentNode; - schemaAst?: GraphQLSchema; - config: Config; - pluginContext?: { [key: string]: any }; - }) => AddToSchemaResult; - export type DocumentTransformObject = { transform: DocumentTransformFunction; - addToSchema?: AddToSchemaResult | DocumentTransformAddToSchemaFunction; }; export type DocumentTransformFileName = string; diff --git a/website/src/pages/docs/advanced/document-transform.mdx b/website/src/pages/docs/advanced/document-transform.mdx index 52ad56b0a3d..475acb99e50 100644 --- a/website/src/pages/docs/advanced/document-transform.mdx +++ b/website/src/pages/docs/advanced/document-transform.mdx @@ -3,11 +3,11 @@ import { Callout } from '@theguild/components' # Document Transform Document transform is a feature that allows you to modify `documents` before they are used by plugins. -You can use functions passed to the `documentTransforms` option to make changes to GraphQL `documents` or extend the schema, as needed. +You can use functions passed to the `documentTransforms` option to make changes to GraphQL `documents`. ## Basic Usage -Document transform has `transform` function and optional `addToSchema` function. +Document transform has `transform` function. Let's specify an object containing those functions as the `documentTransforms` option as follows: @@ -25,8 +25,7 @@ const config: CodegenConfig = { transform: ({ documents }) => { // Make some changes to the documents return documents - }, - addToSchema: 'extend type Query { test: String! }' + } } ] }