Skip to content

Commit

Permalink
Fix mergeSchemas issuee with custom scalars #1530
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed May 25, 2020
1 parent 38eca1c commit fedbb48
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 9 deletions.
12 changes: 10 additions & 2 deletions packages/graphql-tools/package.json
Expand Up @@ -24,13 +24,21 @@
"@graphql-tools/import": "6.0.0",
"@graphql-tools/links": "6.0.0",
"@graphql-tools/load": "6.0.0",
"@graphql-tools/code-file-loader": "6.0.0",
"@graphql-tools/git-loader": "6.0.0",
"@graphql-tools/github-loader": "6.0.0",
"@graphql-tools/graphql-file-loader": "6.0.0",
"@graphql-tools/json-file-loader": "6.0.0",
"@graphql-tools/module-loader": "6.0.0",
"@graphql-tools/url-loader": "6.0.0",
"@graphql-tools/load-files": "6.0.0",
"@graphql-tools/merge": "6.0.0",
"@graphql-tools/mock": "6.0.0",
"@graphql-tools/relay-operation-optimizer": "6.0.0",
"@graphql-tools/resolvers-composition": "6.0.0",
"@graphql-tools/schema": "6.0.0",
"@graphql-tools/stitch": "6.0.0",
"@graphql-tools/utils": "6.0.0"
"@graphql-tools/utils": "6.0.0",
"@graphql-tools/wrap": "6.0.0"
}
}
}
6 changes: 6 additions & 0 deletions packages/graphql-tools/src/index.ts
Expand Up @@ -3,6 +3,12 @@ export * from '@graphql-tools/graphql-tag-pluck';
export * from '@graphql-tools/import';
export * from '@graphql-tools/links';
export * from '@graphql-tools/load';
export * from '@graphql-tools/code-file-loader';
export * from '@graphql-tools/git-loader';
export * from '@graphql-tools/github-loader';
export * from '@graphql-tools/graphql-file-loader';
export * from '@graphql-tools/module-loader';
export * from '@graphql-tools/url-loader';
export * from '@graphql-tools/load-files';
export * from '@graphql-tools/merge';
export * from '@graphql-tools/mock';
Expand Down
2 changes: 1 addition & 1 deletion packages/merge/package.json
Expand Up @@ -27,4 +27,4 @@
"access": "public",
"directory": "dist"
}
}
}
72 changes: 71 additions & 1 deletion packages/merge/tests/merge-schemas.spec.ts
@@ -1,6 +1,7 @@
import { makeExecutableSchema } from '@graphql-tools/schema';
import { graphql, buildSchema, GraphQLScalarType, Kind, GraphQLSchema, ListValueNode } from 'graphql';
import { mergeSchemas, mergeSchemasAsync } from '../src/merge-schemas';
import { printSchemaWithDirectives } from '@graphql-tools/utils';

describe('Merge Schemas', () => {
it('Should include extensions in merged schemas', () => {
Expand Down Expand Up @@ -411,7 +412,7 @@ describe('Merge Schemas', () => {

expect(merged.getDirective('date')).toBeDefined();
});
it.only('should merge a lot of directives but without high memory usage', () => {
it('should merge a lot of directives but without high memory usage', () => {
let num = 100;
const base = buildSchema(/* GraphQL */`
directive @access(roles: [String]) on FIELD_DEFINITION
Expand All @@ -429,4 +430,73 @@ describe('Merge Schemas', () => {

expect((prev.getQueryType().getFields().test.astNode.directives[0].arguments[0].value as ListValueNode).values).toHaveLength(1);
});
it('should merge schemas with custom scalars', () => {
const GraphQLUUID = new GraphQLScalarType({
name: 'UUID',
serialize: val => val,
parseValue: val => val,
parseLiteral: valueNode => {
if (valueNode.kind === 'StringValue') {
return valueNode.value;
}
return null;
}
})
const countrySchema = makeExecutableSchema({
typeDefs: /* GraphQL */`
scalar UUID
type Country {
id: UUID!
name: String
}
type Query {
country: Country
}
`,
resolvers: {
UUID: GraphQLUUID,
}
})


const citySchema = makeExecutableSchema({
typeDefs: /* GraphQL */`
scalar UUID
type City {
id: ID!
name: String
}
type Query {
city: City
}
`,
})

const mergedSchema = mergeSchemas({
schemas: [
countrySchema,
citySchema,
]
});
const printedSchema = printSchemaWithDirectives(mergedSchema);

expect(printedSchema).toContain(/* GraphQL */`
type Query {
country: Country
city: City
}`.trim());
expect(printedSchema).toContain(/* GraphQL */`
scalar UUID
`.trim());
expect(printedSchema).toContain(/* GraphQL */`
type Country {
id: UUID!
name: String
}`.trim());
expect(printedSchema).toContain(/* GraphQL */`
type City {
id: ID!
name: String
}`.trim());
})
});
16 changes: 11 additions & 5 deletions packages/utils/src/mergeDeep.ts
@@ -1,10 +1,16 @@
export function mergeDeep(target: any, ...sources: any): any {
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { isScalarType } from 'graphql';

export function mergeDeep(target: any, ...sources: any[]): any {
if (isScalarType(target)) {
return target;
}
const output = {
...target,
};
sources.forEach((source: any) => {
for (const source of sources) {
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
for (const key in source) {
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] });
Expand All @@ -14,9 +20,9 @@ export function mergeDeep(target: any, ...sources: any): any {
} else {
Object.assign(output, { [key]: source[key] });
}
});
}
}
});
}
return output;
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -6322,6 +6322,11 @@ graphql-type-json@0.3.1:
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.3.1.tgz#47fca2b1fa7adc0758d165b33580d7be7a6cf548"
integrity sha512-1lPkUXQ2L8o+ERLzVAuc3rzc/E6pGF+6HnjihCVTK0VzR0jCuUd92FqNxoHdfILXqOn2L6b4y47TBxiPyieUVA==

graphql-type-uuid@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/graphql-type-uuid/-/graphql-type-uuid-0.2.0.tgz#677a363a93d1b04c4844d20b35ffab7a1d5e6b6a"
integrity sha1-Z3o2OpPRsExIRNILNf+reh1ea2o=

graphql-upload@10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/graphql-upload/-/graphql-upload-10.0.0.tgz#1020c8ac6208c8cdf9de1f6ad9000eab7467e00f"
Expand Down

0 comments on commit fedbb48

Please sign in to comment.