Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/TreeToTS/functions/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,18 @@ export const PrepareScalarPaths = ({ ops, returns }: { returns: ReturnTypesType;
const keyName = root ? ops[k] : k;
return Object.entries(o)
.filter(([k]) => k !== '__directives')
.map(([k, v]) =>
ibb(k, k, v, [...p, purifyGraphQLKey(keyName || k)], [...pOriginals, purifyGraphQLKey(originalKey)], false),
)
.map(([k, v]) => {
// Inline fragments shouldn't be added to the path as they aren't a field
const isInlineFragment = originalKey.match(/^...\\s*on/) != null;
return ibb(
k,
k,
v,
isInlineFragment ? p : [...p, purifyGraphQLKey(keyName || k)],
isInlineFragment ? pOriginals : [...pOriginals, purifyGraphQLKey(originalKey)],
false,
);
})
.reduce((a, b) => ({
...a,
...b,
Expand Down
36 changes: 36 additions & 0 deletions src/TreeToTS/functions/new/decodeScalarsInResponse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,40 @@ describe('Scalars in response get decoded', () => {
});
expect(decodedResponse['drawCard']?.['info']).toEqual(cardInfo);
});

test('Inline fragments get decoded correctly', () => {
const cardInfo = {
power: 9001,
speed: 100,
};
const response = {
drawCard: {
name: 'Adanos',
info: JSON.stringify(cardInfo),
},
};

const decodedResponse = decodeScalarsInResponse({
ops: Ops,
response,
returns: ReturnTypes,
initialOp: 'query',
initialZeusQuery: {
drawCard: {
'...on Card': {
name: true,
info: true,
},
},
},
scalars: {
JSON: {
decode: (e) => {
return JSON.parse(e as string) as typeof cardInfo;
},
},
},
});
expect(decodedResponse['drawCard']?.['info']).toEqual(cardInfo);
});
});
15 changes: 14 additions & 1 deletion src/TreeToTS/functions/new/prepareScalarPaths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrepareScalarPaths } from '@/TreeToTS/functions/new/prepareScalarPaths'

const builder = PrepareScalarPaths({ returns: ReturnTypes, ops: Ops });

describe('Test generated function buildQuery', () => {
describe('Test PrepareScalarPaths function', () => {
test('Simple query', () => {
const matchExact = builder('query', 'Query', {
cards: {
Expand All @@ -18,4 +18,17 @@ describe('Test generated function buildQuery', () => {
};
expect(o).toEqual(matchExact);
});
test('Discards inline fragment from path', () => {
const matchExact = builder('query', 'Query', {
cards: {
'... on Card': {
info: true,
},
},
});
const o = {
'Query|cards|info': 'scalar.JSON',
};
expect(o).toEqual(matchExact);
});
});
15 changes: 12 additions & 3 deletions src/TreeToTS/functions/new/prepareScalarPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ export const PrepareScalarPaths = ({ ops, returns }: { returns: ReturnTypesType;
const keyName = root ? ops[k] : k;
return Object.entries(o)
.filter(([k]) => k !== '__directives')
.map(([k, v]) =>
ibb(k, k, v, [...p, purifyGraphQLKey(keyName || k)], [...pOriginals, purifyGraphQLKey(originalKey)], false),
)
.map(([k, v]) => {
// Inline fragments shouldn't be added to the path as they aren't a field
const isInlineFragment = originalKey.match(/^...\s*on/) != null;
return ibb(
k,
k,
v,
isInlineFragment ? p : [...p, purifyGraphQLKey(keyName || k)],
isInlineFragment ? pOriginals : [...pOriginals, purifyGraphQLKey(originalKey)],
false,
);
})
.reduce((a, b) => ({
...a,
...b,
Expand Down