Skip to content

Commit

Permalink
fix: wrap fragments while inlining, or unions will break
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Jan 12, 2024
1 parent 3a2bad7 commit cae1f60
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
42 changes: 30 additions & 12 deletions packages/npm/@amazeelabs/codegen-operation-ids/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ describe('mode: map', () => {
{
"HomeQuery:37d40553a898c4026ba372c8f42af3df9c3451953b65695b823a8e1e7b5fd90d": "query Home {
loadPage(path: "/") {
title
... on Page {
title
}
}
}",
}
Expand Down Expand Up @@ -166,16 +168,22 @@ describe('mode: map', () => {
{
"HomeQuery:e8b5953fe0f339244ebb14102eddc5d0e23259606de6f697574f69bfe468ac53": "query Home {
loadPage(path: "/") {
title
related {
path
}
related {
... on Page {
title
related {
path
}
path
}
related {
... on Page {
title
related {
path
}
}
... on Page {
path
}
}
}
}",
Expand All @@ -201,9 +209,13 @@ describe('mode: map', () => {
{
"HomeQuery:37d40553a898c4026ba372c8f42af3df9c3451953b65695b823a8e1e7b5fd90d": "query Home {
loadPage(path: "/") {
title
related {
... on Page {
title
related {
... on Page {
title
}
}
}
}
}",
Expand Down Expand Up @@ -234,7 +246,9 @@ describe('mode: map', () => {
{
"HomeQuery:37d40553a898c4026ba372c8f42af3df9c3451953b65695b823a8e1e7b5fd90d": "query Home {
loadPage(path: "/") {
title
... on Page {
title
}
}
}",
}
Expand Down Expand Up @@ -270,9 +284,13 @@ describe('mode: map', () => {
{
"HomeQuery:37d40553a898c4026ba372c8f42af3df9c3451953b65695b823a8e1e7b5fd90d": "query Home {
loadPage(path: "/") {
title
related {
... on Page {
title
related {
... on Page {
title
}
}
}
}
}",
Expand Down
18 changes: 13 additions & 5 deletions packages/npm/@amazeelabs/codegen-operation-ids/src/inline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ describe('inlineFragments', () => {
const [A] = doc.definitions.filter(isFragmentDefinitionNode);
const inlined = inlineFragments(query, new Map(Object.entries({ A })));
expect(print(inlined)).toEqual(`{
myprop
... on Query {
myprop
}
}`);
});

Expand All @@ -56,7 +58,9 @@ describe('inlineFragments', () => {
const inlined = inlineFragments(query, new Map(Object.entries({ A })));
expect(print(inlined)).toEqual(`{
a {
myprop
... on A {
myprop
}
}
}`);
});
Expand All @@ -83,9 +87,13 @@ fragment B on B {
expect(print(inlined)).toEqual(`{
a {
propA
propA
propB {
propC
... on A {
propA
propB {
... on B {
propC
}
}
}
}
}`);
Expand Down
13 changes: 11 additions & 2 deletions packages/npm/@amazeelabs/codegen-operation-ids/src/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ import {
export function inlineFragments<
TNode extends ExecutableDefinitionNode | FieldNode,
>(node: TNode, fragments: Map<string, FragmentDefinitionNode>): TNode {
const selections = [] as Array<SelectionNode>;
const selections: Array<SelectionNode> = [];
const target = structuredClone(node);
target.selectionSet?.selections.forEach((sel) => {
if (sel.kind === Kind.FRAGMENT_SPREAD) {
const fragment = fragments.get(sel.name.value);
if (fragment) {
const fragmentSelections: Array<SelectionNode> = [];
inlineFragments(fragment, fragments).selectionSet.selections.forEach(
(sel) => {
selections.push(sel);
fragmentSelections.push(sel);
},
);
selections.push({
kind: Kind.INLINE_FRAGMENT,
typeCondition: fragment.typeCondition,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: fragmentSelections,
},
});
}
} else if (sel.kind === Kind.FIELD && sel.selectionSet) {
selections.push(inlineFragments(sel, fragments));
Expand Down

0 comments on commit cae1f60

Please sign in to comment.