/
withTransforms.test.ts
144 lines (132 loc) · 3.74 KB
/
withTransforms.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { graphql, GraphQLList, Kind } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
import { stitchSchemas } from '@graphql-tools/stitch';
import { TransformQuery } from '@graphql-tools/wrap'
describe('works with complex transforms', () => {
test('using TransformQuery instead of valuesFromResults', async () => {
const bookSchema = makeExecutableSchema({
typeDefs: `
type Book {
id: ID!
title: String!
}
type UserBooks {
userId: ID!
books: [Book!]!
}
type Query {
booksByUserIds(userIds: [ID!]!): [UserBooks!]!
}
`,
resolvers: {
Query: {
booksByUserIds: (_root, { userIds }) =>
userIds.map((userId: any) => ({
userId,
books: [
{id: 'b1', title: 'Harry Potter 1'},
{id: 'b2', title: 'Harry Potter 2'},
]
}))
}
}
});
const userSchema = makeExecutableSchema({
typeDefs: `
type User {
id: String!
email: String!
}
type Query {
usersByIds(ids: [ID!]): [User]
}
`,
resolvers: {
Query: {
usersByIds: (_root, args) => {
return args.ids.map((id: string) => ({ id, email: `${id}@test.com` }));
}
}
}
});
const linkTypeDefs = `
extend type User {
books: [Book!]!
}
`;
const queryTransform = new TransformQuery({
path: ['booksByUserIds'],
queryTransformer: (selectionSet) => ({
kind: Kind.SELECTION_SET,
selections: [
{ kind: Kind.FIELD, name: { kind: Kind.NAME, value: 'userId' } },
{ kind: Kind.FIELD, name: { kind: Kind.NAME, value: 'books' }, selectionSet }
]
}),
resultTransformer: (results, delegationContext) => {
const userIds = delegationContext.args?.['userIds'];
const booksByUserIds = results.reduce(
(acc: any, { userId, books }: { userId: string, books: any[] }) => {
acc[userId] = books
return acc
}, {});
const orderedAndUnwrapped = userIds.map((id: any) => booksByUserIds[id]);
return orderedAndUnwrapped;
}
});
const stitchedSchema = stitchSchemas({
subschemas: [bookSchema, userSchema],
typeDefs: linkTypeDefs,
resolvers: {
User: {
books: {
selectionSet: `{ id }`,
resolve: (user, _args, context, info) => batchDelegateToSchema({
schema: bookSchema,
operation: 'query',
fieldName: 'booksByUserIds',
key: user.id,
argsFromKeys: (userIds) => ({ userIds }),
context,
info,
transforms: [queryTransform],
returnType: new GraphQLList(new GraphQLList(info.schema.getType('Book')!))
}),
},
},
},
});
const query = `
query {
usersByIds(ids: ["u1", "u2"]) {
id
books {
id
name: title
}
}
}
`;
const result = await graphql(stitchedSchema, query);
expect(result.errors).toBeUndefined();
expect(result.data).toEqual({
usersByIds: [
{
id: 'u1',
books: [
{id: 'b1', name: 'Harry Potter 1'},
{id: 'b2', name: 'Harry Potter 2'},
]
},
{
id: 'u2',
books: [
{id: 'b1', name: 'Harry Potter 1'},
{id: 'b2', name: 'Harry Potter 2'},
]
}
]
});
});
});