Skip to content

Commit

Permalink
fix(core): Correctly encode IDs in nested fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Oct 20, 2020
1 parent 75e3f9c commit d2333fc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
69 changes: 68 additions & 1 deletion packages/core/e2e/entity-id-strategy.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import gql from 'graphql-tag';
import path from 'path';

import { initialData } from '../../../e2e-common/e2e-initial-data';
import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';

import {
IdTest1,
Expand Down Expand Up @@ -215,4 +215,71 @@ describe('EntityIdStrategy', () => {
],
});
});

it('encodes ids in doubly-nested fragment', async () => {
const { products } = await shopClient.query<IdTest1.Query>(gql`
query IdTest10 {
products(options: { take: 1 }) {
items {
...ProdFragment1
}
}
}
fragment ProdFragment1 on Product {
...ProdFragment2
}
fragment ProdFragment2 on Product {
id
featuredAsset {
id
}
}
`);

expect(products).toEqual({
items: [
{
id: 'T_1',
featuredAsset: {
id: 'T_2',
},
},
],
});
});

it('encodes ids in triply-nested fragment', async () => {
const { products } = await shopClient.query<IdTest1.Query>(gql`
query IdTest11 {
products(options: { take: 1 }) {
items {
...ProdFragment1_1
}
}
}
fragment ProdFragment1_1 on Product {
...ProdFragment2_1
}
fragment ProdFragment2_1 on Product {
...ProdFragment3_1
}
fragment ProdFragment3_1 on Product {
id
featuredAsset {
id
}
}
`);

expect(products).toEqual({
items: [
{
id: 'T_1',
featuredAsset: {
id: 'T_2',
},
},
],
});
});
});
12 changes: 10 additions & 2 deletions packages/core/src/api/common/graphql-value-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,16 @@ export class GraphqlValueTransformer {
if (targetNode) {
let children: { [name: string]: TypeTreeNode } = targetNode.children;
if (targetNode.fragmentRefs.length) {
for (const ref of targetNode.fragmentRefs) {
children = { ...children, ...typeTree.fragments[ref].children };
const fragmentRefs = targetNode.fragmentRefs.slice();
while (fragmentRefs.length) {
const ref = fragmentRefs.pop();
if (ref) {
const fragment = typeTree.fragments[ref];
children = { ...children, ...fragment.children };
if (fragment.fragmentRefs) {
fragmentRefs.push(...fragment.fragmentRefs);
}
}
}
}
targetNode = children[segment];
Expand Down

0 comments on commit d2333fc

Please sign in to comment.