Skip to content

Commit

Permalink
Change merge system to avoid ref bugs (#6791)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moumouls authored Jul 13, 2020
1 parent 34614e0 commit c855017
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/GraphQL/ParseGraphQLSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,41 @@ class ParseGraphQLSchema {

if (typeof this.graphQLCustomTypeDefs.getTypeMap === 'function') {
const customGraphQLSchemaTypeMap = this.graphQLCustomTypeDefs.getTypeMap();
const findAndReplaceLastType = (parent, key) => {
if (parent[key].name) {
if (
this.graphQLAutoSchema.getType(parent[key].name) &&
this.graphQLAutoSchema.getType(parent[key].name) !== parent[key]
) {
// To avoid unresolved field on overloaded schema
// replace the final type with the auto schema one
parent[key] = this.graphQLAutoSchema.getType(parent[key].name);
}
} else {
if (parent[key].ofType) {
findAndReplaceLastType(parent[key], 'ofType');
}
}
};
Object.values(customGraphQLSchemaTypeMap).forEach(
(customGraphQLSchemaType) => {
if (
!customGraphQLSchemaType ||
!customGraphQLSchemaType.name ||
customGraphQLSchemaType.name.startsWith('__')
) {
return;
}
const autoGraphQLSchemaType = this.graphQLAutoSchema.getType(
customGraphQLSchemaType.name
);
if (!autoGraphQLSchemaType) {
this.graphQLAutoSchema._typeMap[
customGraphQLSchemaType.name
] = customGraphQLSchemaType;
}
}
);
Object.values(customGraphQLSchemaTypeMap).forEach(
(customGraphQLSchemaType) => {
if (
Expand All @@ -212,30 +247,11 @@ class ParseGraphQLSchema {
const autoGraphQLSchemaType = this.graphQLAutoSchema.getType(
customGraphQLSchemaType.name
);

if (
autoGraphQLSchemaType &&
typeof customGraphQLSchemaType.getFields === 'function'
) {
const findAndReplaceLastType = (parent, key) => {
if (parent[key].name) {
if (
this.graphQLAutoSchema.getType(parent[key].name) &&
this.graphQLAutoSchema.getType(parent[key].name) !==
parent[key]
) {
// To avoid unresolved field on overloaded schema
// replace the final type with the auto schema one
parent[key] = this.graphQLAutoSchema.getType(
parent[key].name
);
}
} else {
if (parent[key].ofType) {
findAndReplaceLastType(parent[key], 'ofType');
}
}
};

Object.values(customGraphQLSchemaType.getFields()).forEach(
(field) => {
findAndReplaceLastType(field, 'type');
Expand All @@ -245,10 +261,6 @@ class ParseGraphQLSchema {
...autoGraphQLSchemaType.getFields(),
...customGraphQLSchemaType.getFields(),
};
} else {
this.graphQLAutoSchema._typeMap[
customGraphQLSchemaType.name
] = customGraphQLSchemaType;
}
}
);
Expand Down

0 comments on commit c855017

Please sign in to comment.