/
typeMerging.test.ts
105 lines (92 loc) · 2.2 KB
/
typeMerging.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
// The below is meant to be an alternative canonical schema stitching example
// which relies on type merging.
import { graphql } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { addMocksToSchema } from '@graphql-tools/mock';
import { stitchSchemas } from '../src/stitchSchemas';
let chirpSchema = makeExecutableSchema({
typeDefs: `
type Chirp {
id: ID!
text: String
author: User
coAuthors: [User]
authorGroups: [[User]]
}
type User {
id: ID!
chirps: [Chirp]
}
type Query {
userById(id: ID!): User
}
`,
});
chirpSchema = addMocksToSchema({ schema: chirpSchema });
let authorSchema = makeExecutableSchema({
typeDefs: `
type User {
id: ID!
email: String
}
type Query {
userById(id: ID!): User
}
`,
});
authorSchema = addMocksToSchema({ schema: authorSchema });
const stitchedSchema = stitchSchemas({
subschemas: [
{
schema: chirpSchema,
merge: {
User: {
fieldName: 'userById',
args: (originalResult) => ({ id: originalResult.id }),
selectionSet: '{ id }',
},
},
},
{
schema: authorSchema,
merge: {
User: {
fieldName: 'userById',
args: (originalResult) => ({ id: originalResult.id }),
selectionSet: '{ id }',
},
},
},
],
mergeTypes: true,
});
describe('merging using type merging', () => {
test('works', async () => {
const query = `
query {
userById(id: 5) {
__typename
chirps {
id
textAlias: text
author {
email
}
coAuthors {
email
}
authorGroups {
email
}
}
}
}
`;
const result = await graphql(stitchedSchema, query);
expect(result.errors).toBeUndefined();
expect(result.data.userById.__typename).toBe('User');
expect(result.data.userById.chirps[1].id).not.toBe(null);
expect(result.data.userById.chirps[1].text).not.toBe(null);
expect(result.data.userById.chirps[1].author.email).not.toBe(null);
});
});