Skip to content

Commit 343e697

Browse files
st3veVst3vev-spire
andauthored
Fix for supporting longer paths in TransformQuery (#3333)
Co-authored-by: Stepan Vyterna <stepan.vyterna@spire.com>
1 parent 3588f6a commit 343e697

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

.changeset/metal-files-worry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/wrap': patch
3+
---
4+
5+
Fix TransformQuery for path longer than 1

packages/wrap/src/transforms/TransformQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export default class TransformQuery implements Transform {
122122
}
123123
newData[next] = this.resultTransformer(newData[next], delegationContext, transformationContext);
124124
}
125-
return newData;
125+
return data;
126126
}
127127

128128
private transformErrors(errors: ReadonlyArray<GraphQLError>): ReadonlyArray<GraphQLError> {

packages/wrap/tests/transforms.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,16 @@ describe('transforms', () => {
444444
errorTest: String
445445
}
446446
447+
scalar PageInfo
448+
449+
type UserConnection {
450+
pageInfo: PageInfo!
451+
nodes: [User!]
452+
}
453+
447454
type Query {
448455
userById(id: ID!): User
456+
usersByIds(ids: [ID!]): UserConnection!
449457
}
450458
`,
451459
resolvers: {
@@ -463,6 +471,14 @@ describe('transforms', () => {
463471
userById(_parent, { id }) {
464472
return data[id];
465473
},
474+
usersByIds(_parent, { ids }) {
475+
return {
476+
nodes: Object.entries(data)
477+
.filter(([id, _value]) => ids.includes(id))
478+
.map(([_id, value]) => value),
479+
pageInfo: "1"
480+
}
481+
}
466482
},
467483
},
468484
});
@@ -474,9 +490,17 @@ describe('transforms', () => {
474490
errorTest: String
475491
}
476492
493+
scalar PageInfo
494+
495+
type AddressConnection {
496+
pageInfo: PageInfo!
497+
nodes: [Address!]
498+
}
499+
477500
type Query {
478501
addressByUser(id: ID!): Address
479502
errorTest(id: ID!): Address
503+
addressesByUsers(ids:[ID!]): AddressConnection!
480504
}
481505
`,
482506
resolvers: {
@@ -517,6 +541,40 @@ describe('transforms', () => {
517541
],
518542
});
519543
},
544+
addressesByUsers(_parent, { ids }, context, info) {
545+
return delegateToSchema({
546+
schema: subschema,
547+
operation: 'query',
548+
fieldName: 'usersByIds',
549+
args: { ids },
550+
context,
551+
info,
552+
transforms: [
553+
// Wrap document takes a subtree as an AST node
554+
new TransformQuery({
555+
// longer path, useful when transforming paginated results
556+
path: ['usersByIds', 'nodes'],
557+
queryTransformer: (subtree: SelectionSetNode) => ({
558+
// same query transformation as above
559+
kind: Kind.SELECTION_SET,
560+
selections: [
561+
{
562+
kind: Kind.FIELD,
563+
name: {
564+
kind: Kind.NAME,
565+
value: 'address',
566+
},
567+
selectionSet: subtree,
568+
},
569+
],
570+
}),
571+
// how to process the data result at path
572+
resultTransformer: (result) => result.map((u: any) => u.address),
573+
errorPathTransformer: (path) => path.slice(1),
574+
}),
575+
],
576+
})
577+
},
520578
errorTest(_parent, { id }, context, info) {
521579
return delegateToSchema({
522580
schema: subschema,
@@ -637,5 +695,40 @@ describe('transforms', () => {
637695
errors: [new Error('Test Error!')],
638696
});
639697
});
698+
699+
test('nested path produces nested result, other fields get preserved', async () => {
700+
const result = await graphql(
701+
schema,
702+
`
703+
query {
704+
addressesByUsers(ids: ["u1", "u2"]) {
705+
nodes {
706+
streetAddress
707+
zip
708+
}
709+
pageInfo
710+
}
711+
}
712+
`
713+
);
714+
715+
expect(result).toEqual({
716+
data: {
717+
addressesByUsers: {
718+
nodes: [
719+
{
720+
streetAddress: 'Windy Shore 21 A 7',
721+
zip: '12345',
722+
},
723+
{
724+
streetAddress: 'Snowy Mountain 5 B 77',
725+
zip: '54321',
726+
},
727+
],
728+
pageInfo: "1"
729+
},
730+
},
731+
});
732+
});
640733
});
641734
});

0 commit comments

Comments
 (0)