Skip to content

Commit

Permalink
Fix alias problems by unaliasing queries we are sending to sub schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Oct 4, 2017
1 parent 640142e commit bdf691a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ function filterSelectionSet(
const typeStack: Array<GraphQLType> = [type];
const filteredSelectionSet = visit(selectionSet, {
[Kind.FIELD]: {
enter(node: FieldNode): null | undefined {
enter(node: FieldNode): null | undefined | FieldNode {
let parentType: GraphQLType = resolveType(
typeStack[typeStack.length - 1],
);
Expand All @@ -735,6 +735,13 @@ function filterSelectionSet(
typeStack.push(field.type);
}
}

if (node.alias) {
return {
...node,
alias: null,
};
}
},
leave() {
typeStack.pop();
Expand Down
71 changes: 71 additions & 0 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,77 @@ bookingById(id: $b1) {
},
});
});

it('aliases', async () => {
const result = await graphql(
mergedSchema,
`
query {
property: propertyById(id: "p1") {
id
propertyId: id
secondAlias: id
firstReservation: bookings(limit: 1) {
id
}
reservations: bookings {
bookingId: id
user: customer {
customerId: id
}
hotel: property {
propertyId: id
}
}
}
}
`,
);

expect(result).to.deep.equal({
data: {
property: {
id: 'p1',
propertyId: 'p1',
secondAlias: 'p1',
firstReservation: [
{
id: 'b1',
},
],
reservations: [
{
bookingId: 'b1',
user: {
customerId: 'c1',
},
hotel: {
propertyId: 'p1',
},
},
{
bookingId: 'b2',
hotel: {
propertyId: 'p1',
},
user: {
customerId: 'c2',
},
},
{
bookingId: 'b3',
hotel: {
propertyId: 'p1',
},
user: {
customerId: 'c3',
},
},
],
},
},
});
});
});
});
});

0 comments on commit bdf691a

Please sign in to comment.