Skip to content

Commit

Permalink
Go
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Apr 4, 2023
1 parent 8ca6f60 commit f004305
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 35 deletions.
1 change: 1 addition & 0 deletions packages/transforms/transfer-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
29 changes: 22 additions & 7 deletions packages/transforms/transfer-schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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 },
};
},
}),
});
Expand Down
89 changes: 61 additions & 28 deletions packages/transforms/transfer-schema/test/transfer-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -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 */ `
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
});

Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit f004305

Please sign in to comment.