From f004305a199f2c6922d6642c13fec9376025747d Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Tue, 4 Apr 2023 13:32:07 +0300 Subject: [PATCH] Go --- .../transforms/transfer-schema/package.json | 1 + .../transforms/transfer-schema/src/index.ts | 29 ++++-- .../test/transfer-schema.spec.ts | 89 +++++++++++++------ 3 files changed, 84 insertions(+), 35 deletions(-) diff --git a/packages/transforms/transfer-schema/package.json b/packages/transforms/transfer-schema/package.json index be86eb38ff803..faff2139f4363 100644 --- a/packages/transforms/transfer-schema/package.json +++ b/packages/transforms/transfer-schema/package.json @@ -25,6 +25,7 @@ "peerDependencies": { "@graphql-mesh/types": "^0.91.12", "@graphql-mesh/utils": "^0.43.20", + "@graphql-tools/utils": "^9.2.1", "graphql": "*" }, "dependencies": { diff --git a/packages/transforms/transfer-schema/src/index.ts b/packages/transforms/transfer-schema/src/index.ts index b68e7c8900d95..b954ac661b379 100644 --- a/packages/transforms/transfer-schema/src/index.ts +++ b/packages/transforms/transfer-schema/src/index.ts @@ -1,7 +1,12 @@ -import { GraphQLArgumentConfig, GraphQLFieldConfig, GraphQLObjectType, GraphQLSchema } from 'graphql'; -import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types'; -import { mapSchema, pruneSchema, MapperKind } from '@graphql-tools/utils'; +import { + GraphQLArgumentConfig, + GraphQLFieldConfig, + GraphQLObjectType, + GraphQLSchema, +} from 'graphql'; import { matcher } from 'micromatch'; +import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types'; +import { MapperKind, mapSchema, pruneSchema } from '@graphql-tools/utils'; export default class TransferSchemaTransform implements MeshTransform { noWrap = true; @@ -26,7 +31,9 @@ export default class TransferSchemaTransform implements MeshTransform { const rawGlob = fromArgsGlob || fromFieldNameOrGlob; const fixedGlob = - rawGlob.includes('{') && !rawGlob.includes(',') ? rawGlob.replace('{', '').replace('}', '') : rawGlob; + rawGlob.includes('{') && !rawGlob.includes(',') + ? rawGlob.replace('{', '').replace('}', '') + : rawGlob; const polishedGlob = fixedGlob.split(', ').join(',').trim(); const mapName = fromArgsGlob ? 'ArgsMap' : 'FieldsMap'; @@ -54,7 +61,10 @@ export default class TransferSchemaTransform implements MeshTransform { const isMatch = matcher(usePattern); if (isMatch(value)) { const currentAdditionalConfigs = this[`additional${mapIdentifier}`].get(mapKey) || {}; - this[`additional${mapIdentifier}`].set(mapKey, { ...currentAdditionalConfigs, [value]: config }); + this[`additional${mapIdentifier}`].set(mapKey, { + ...currentAdditionalConfigs, + [value]: config, + }); break; } } @@ -99,7 +109,9 @@ export default class TransferSchemaTransform implements MeshTransform { // handle args removal if (hasRemovalArgRules) { const newArgs = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => { - return this.matchInSet(removalArgRules, argName) ? args : { ...args, [argName]: argConfig }; + return this.matchInSet(removalArgRules, argName) + ? args + : { ...args, [argName]: argConfig }; }, {}); return { ...fieldConfig, args: newArgs }; @@ -128,7 +140,10 @@ export default class TransferSchemaTransform implements MeshTransform { if (!additionalArgsConfigMap) return undefined; - return { ...fieldConfig, args: { ...(fieldConfig.args || {}), ...additionalArgsConfigMap } }; + return { + ...fieldConfig, + args: { ...(fieldConfig.args || {}), ...additionalArgsConfigMap }, + }; }, }), }); diff --git a/packages/transforms/transfer-schema/test/transfer-schema.spec.ts b/packages/transforms/transfer-schema/test/transfer-schema.spec.ts index 920d91edb3951..6cd15024a8153 100644 --- a/packages/transforms/transfer-schema/test/transfer-schema.spec.ts +++ b/packages/transforms/transfer-schema/test/transfer-schema.spec.ts @@ -1,11 +1,10 @@ /* eslint-disable import/no-extraneous-dependencies */ -import { printSchema, GraphQLObjectType } from 'graphql'; +import { GraphQLObjectType, printSchema } from 'graphql'; import InMemoryLRUCache from '@graphql-mesh/cache-localforage'; import { MeshPubSub } from '@graphql-mesh/types'; +import { defaultImportFn, DefaultLogger, PubSub } from '@graphql-mesh/utils'; import { makeExecutableSchema } from '@graphql-tools/schema'; - import TransferFieldTransform from '../src'; -import { DefaultLogger, PubSub, defaultImportFn } from '@graphql-mesh/utils'; describe('transfer-schema transform', () => { const schemaDefs = /* GraphQL */ ` @@ -64,18 +63,26 @@ describe('transfer-schema transform', () => { const transformedSchema = transform.transformSchema(schema); // test copy - expect((transformedSchema.getType('Mutation') as GraphQLObjectType).getFields().ourBooks.type.toString()).toBe( - '[Book]' - ); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().ourBooks.type.toString()).toBe( - '[Book]' - ); + expect( + (transformedSchema.getType('Mutation') as GraphQLObjectType) + .getFields() + .ourBooks.type.toString(), + ).toBe('[Book]'); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType) + .getFields() + .ourBooks.type.toString(), + ).toBe('[Book]'); // test move - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook).toBeUndefined(); - expect((transformedSchema.getType('Mutation') as GraphQLObjectType).getFields().addBook.type.toString()).toBe( - 'Book' - ); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook, + ).toBeUndefined(); + expect( + (transformedSchema.getType('Mutation') as GraphQLObjectType) + .getFields() + .addBook.type.toString(), + ).toBe('Book'); expect(printSchema(transformedSchema)).toMatchSnapshot(); }); @@ -103,11 +110,17 @@ describe('transfer-schema transform', () => { const transformedSchema = transform.transformSchema(schema); expect(transformedSchema.getType('Mutation') as GraphQLObjectType).toBeUndefined(); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.type.toString()).toBe('[Book]'); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.type.toString()).toBe('Book'); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().ourBooks.type.toString()).toBe( - '[Book]' - ); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.type.toString(), + ).toBe('[Book]'); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.type.toString(), + ).toBe('Book'); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType) + .getFields() + .ourBooks.type.toString(), + ).toBe('[Book]'); expect(printSchema(transformedSchema)).toMatchSnapshot(); }); @@ -134,8 +147,12 @@ describe('transfer-schema transform', () => { }); const transformedSchema = transform.transformSchema(schema); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args).toHaveLength(0); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args).toHaveLength(2); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args, + ).toHaveLength(0); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args, + ).toHaveLength(2); const titleArg = (transformedSchema.getType('Query') as GraphQLObjectType) .getFields() .books.args.find(({ name }) => name === 'title'); @@ -171,8 +188,12 @@ describe('transfer-schema transform', () => { }); const transformedSchema = transform.transformSchema(schema); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args).toHaveLength(2); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args).toHaveLength(2); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args, + ).toHaveLength(2); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args, + ).toHaveLength(2); const titleArg = (transformedSchema.getType('Query') as GraphQLObjectType) .getFields() .books.args.find(({ name }) => name === 'title'); @@ -209,8 +230,12 @@ describe('transfer-schema transform', () => { }); const transformedSchema = transform.transformSchema(schema); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args).toHaveLength(0); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args).toHaveLength(2); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args, + ).toHaveLength(0); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args, + ).toHaveLength(2); const titleArg = (transformedSchema.getType('Query') as GraphQLObjectType) .getFields() .books.args.find(({ name }) => name === 'title'); @@ -245,8 +270,12 @@ describe('transfer-schema transform', () => { }); const transformedSchema = transform.transformSchema(schema); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args).toHaveLength(2); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args).toHaveLength(2); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args, + ).toHaveLength(2); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args, + ).toHaveLength(2); const titleArg = (transformedSchema.getType('Query') as GraphQLObjectType) .getFields() .books.args.find(({ name }) => name === 'title'); @@ -281,8 +310,12 @@ describe('transfer-schema transform', () => { }); const transformedSchema = transform.transformSchema(schema); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args).toHaveLength(2); - expect((transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args).toHaveLength(1); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().addBook.args, + ).toHaveLength(2); + expect( + (transformedSchema.getType('Query') as GraphQLObjectType).getFields().books.args, + ).toHaveLength(1); const titleArg = (transformedSchema.getType('Query') as GraphQLObjectType) .getFields() .books.args.find(({ name }) => name === 'title');