Skip to content

Commit

Permalink
fix: problem "schema contains multiple types named XXX" when merging …
Browse files Browse the repository at this point in the history
…several schemas

related #287
  • Loading branch information
nodkz committed Sep 22, 2020
1 parent bf28343 commit ea179e3
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/EnumTypeComposer.js
Expand Up @@ -146,6 +146,7 @@ export class EnumTypeComposer<TContext> {
// add itself to TypeStorage on create
// it avoids recursive type use errors
this.schemaComposer.set(graphqlType, this);
this.schemaComposer.set(graphqlType.name, this);

this._gqcFields = convertEnumValuesToConfig(this._gqType.getValues(), this.schemaComposer);

Expand Down
1 change: 1 addition & 0 deletions src/InputTypeComposer.js
Expand Up @@ -178,6 +178,7 @@ export class InputTypeComposer<TContext> {
// add itself to TypeStorage on create
// it avoids recursive type use errors
this.schemaComposer.set(graphqlType, this);
this.schemaComposer.set(graphqlType.name, this);

if (graphqlVersion >= 14) {
this._gqcFields = convertInputFieldMapToConfig(this._gqType._fields, this.schemaComposer);
Expand Down
1 change: 1 addition & 0 deletions src/InterfaceTypeComposer.js
Expand Up @@ -211,6 +211,7 @@ export class InterfaceTypeComposer<TSource, TContext> {
// add itself to TypeStorage on create
// it avoids recursive type use errors
this.schemaComposer.set(graphqlType, this);
this.schemaComposer.set(graphqlType.name, this);

if (graphqlVersion >= 15) {
this._gqcFields = convertObjectFieldMapToConfig(this._gqType._fields, this.schemaComposer);
Expand Down
4 changes: 4 additions & 0 deletions src/ObjectTypeComposer.js
Expand Up @@ -324,6 +324,10 @@ export class ObjectTypeComposer<TSource, TContext> {
// add itself to TypeStorage on create
// it avoids recursive type use errors
this.schemaComposer.set(graphqlType, this);
const typename = graphqlType.name;
if (typename !== 'Query' && typename !== 'Mutation' && typename !== 'Subscription') {
this.schemaComposer.set(typename, this);
}

if (graphqlVersion >= 14) {
this._gqcFields = convertObjectFieldMapToConfig(this._gqType._fields, this.schemaComposer);
Expand Down
1 change: 1 addition & 0 deletions src/ScalarTypeComposer.js
Expand Up @@ -120,6 +120,7 @@ export class ScalarTypeComposer<TContext> {
// add itself to TypeStorage on create
// it avoids recursive type use errors
this.schemaComposer.set(graphqlType, this);
this.schemaComposer.set(graphqlType.name, this);

let serialize;
let parseValue;
Expand Down
1 change: 1 addition & 0 deletions src/UnionTypeComposer.js
Expand Up @@ -181,6 +181,7 @@ export class UnionTypeComposer<TSource, TContext> {
// add itself to TypeStorage on create
// it avoids recursive type use errors
this.schemaComposer.set(graphqlType, this);
this.schemaComposer.set(graphqlType.name, this);

let types = [];
if (graphqlVersion >= 14) {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/EnumTypeComposer-test.js
Expand Up @@ -198,7 +198,7 @@ describe('EnumTypeComposer', () => {
});

it('should create TC without values from string', () => {
const myTC = EnumTypeComposer.create('MyEnum', schemaComposer);
const myTC = EnumTypeComposer.create('MyEnum123', schemaComposer);
expect(myTC.getFieldNames()).toEqual([]);
});

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/ObjectTypeComposer-test.js
Expand Up @@ -784,7 +784,7 @@ describe('ObjectTypeComposer', () => {
expect(schemaComposer.getOTC('SomeUser')).toBe(SomeUserTC);
});

it('should create type and NOTE store root types in schemaComposer', () => {
it('should create type and NOT to store root types in schemaComposer', () => {
ObjectTypeComposer.create('Query', schemaComposer);
expect(schemaComposer.has('Query')).toBeFalsy();

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/github_issues/107-test.js
Expand Up @@ -70,7 +70,7 @@ describe('github issue #107 merge Schema types on GQL', () => {
expect(RemoteAccessTC.getTypeName()).toEqual('Access');
});

it('schema stiching on Query', async () => {
it('schema stitching on Query', async () => {
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);

Expand Down Expand Up @@ -105,7 +105,7 @@ describe('github issue #107 merge Schema types on GQL', () => {
});
});

it('schema stiching on Query.remote', async () => {
it('schema stitching on Query.remote', async () => {
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);

Expand Down
92 changes: 92 additions & 0 deletions src/__tests__/github_issues/287-test.js
Expand Up @@ -118,4 +118,96 @@ describe('github issue #287: Can we merge schemas with overriding types in field
type Subscription
`);
});

it('should not throw contains multiple types named "Node"', () => {
if (graphqlVersion < 15) return;

const typeDefsA = `
type Mutation {
createPost(data: String!): Post!
}
interface Node {
id: ID!
}
type Post implements Node {
id: ID!
title: String!
}
type PostConnection {
edges: [PostEdge]!
}
type PostEdge {
node: Post!
}
type PostSubscriptionPayload {
node: Post
}
input PostUpdateManyMutationInput {
title: String
content: String
}
type Query {
posts(first: Int, last: Int): [Post]!
postsConnection(first: Int, last: Int): PostConnection!
node(id: ID!): Node
}
type Subscription {
post(where: String): PostSubscriptionPayload
}
`;

const typeDefsB = `
type Mutation {
createPost(data: String!): Post!
}
interface Node {
id: ID!
}
type Post implements Node {
id: ID!
content: String
}
type PostConnection {
edges: [PostEdge]!
}
type PostEdge {
node: Post!
}
type PostSubscriptionPayload {
node: Post
}
type Query {
posts: [Post]!
postsConnection: PostConnection!
node(id: ID!): Node
}
type Subscription {
post(where: String): PostSubscriptionPayload
}
`;

const schemaA = buildSchema(typeDefsA);
const schemaB = buildSchema(typeDefsB);

const sc = new SchemaComposer(schemaA);
sc.merge(schemaB);
expect(() => {
sc.buildSchema();
}).not.toThrow(/contains multiple types named "Node"/);
});
});

0 comments on commit ea179e3

Please sign in to comment.