-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathschemaBuilder.spec.ts
146 lines (142 loc) · 4.24 KB
/
schemaBuilder.spec.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
145
146
import { graphql } from 'graphql';
import { expect, test } from 'vitest';
import schemaBuilder from './schemaBuilder';
test('plugs resolvers with schema', () => {
const schema = schemaBuilder({
posts: [{ id: 0, title: 'hello', foo: 'bar' }],
});
return graphql({
schema,
source: 'query { Post(id: 0) { id title } }',
}).then((result) =>
expect(result).toEqual({
data: { Post: { id: '0', title: 'hello' } },
}),
);
});
const data = {
posts: [
{
id: 1,
title: 'Lorem Ipsum',
views: 254,
user_id: 123,
},
{
id: 2,
title: 'Ut enim ad minim veniam',
views: 65,
user_id: 456,
},
{
id: 3,
title: 'Sic Dolor amet',
views: 76,
user_id: 123,
},
],
users: [
{ id: 123, name: 'John Doe' },
{ id: 456, name: 'Jane Doe' },
],
comments: [
{ id: 987, post_id: 1, body: 'Consectetur adipiscing elit' },
{ id: 995, post_id: 1, body: 'Nam molestie pellentesque dui' },
{ id: 998, post_id: 2, body: 'Sunt in culpa qui officia' },
],
};
const schema = schemaBuilder(data);
test('all* route returns all entities by default', () =>
graphql({ schema, source: '{ allPosts { id } }' }).then((result) =>
expect(result).toEqual({
data: {
allPosts: [{ id: '1' }, { id: '2' }, { id: '3' }],
},
}),
));
test('all* route supports pagination', () =>
graphql({
schema,
source: '{ allPosts(page: 0, perPage: 2) { id } }',
}).then((result) =>
expect(result).toEqual({
data: {
allPosts: [{ id: '1' }, { id: '2' }],
},
}),
));
test('all* route supports sorting', () =>
graphql({
schema,
source: '{ allPosts(sortField: "views", sortOrder: "desc") { id } }',
}).then((result) =>
expect(result).toEqual({
data: {
allPosts: [{ id: '1' }, { id: '3' }, { id: '2' }],
},
}),
));
test('all* route supports filtering', () =>
graphql({
schema,
source: '{ allPosts(filter: { q: "lorem"}) { id } }',
}).then((result) =>
expect(result).toEqual({
data: {
allPosts: [{ id: '1' }],
},
}),
));
test('entity route returns a single entity', () =>
graphql({ schema, source: '{ Post(id: 2) { id } }' }).then((result) =>
expect(result).toEqual({
data: {
Post: { id: '2' },
},
}),
));
test('entity route gets all the entity fields', () =>
graphql({
schema,
source: '{ Post(id: 1) { id title views user_id } }',
}).then((result) =>
expect(result).toEqual({
data: {
Post: {
id: '1',
title: 'Lorem Ipsum',
user_id: '123',
views: 254,
},
},
}),
));
test('entity route get many to one relationships fields', () =>
graphql({ schema, source: '{ Post(id: 1) { User { name } } }' }).then(
(result) =>
expect(result).toEqual({
data: { Post: { User: { name: 'John Doe' } } },
}),
));
test('entity route get one to many relationships fields', () =>
graphql({ schema, source: '{ Post(id: 1) { Comments { body } } }' }).then(
(result) =>
expect(result).toEqual({
data: {
Post: {
Comments: [
{ body: 'Consectetur adipiscing elit' },
{ body: 'Nam molestie pellentesque dui' },
],
},
},
}),
));
test('returns an error when asked for a non existent field', () =>
graphql({ schema, source: '{ Post(id: 1) { foo } }' }).then((result) => {
expect(result.errors).toHaveLength(1);
const errors = result.errors as any[];
expect(errors[0].message).toEqual(
'Cannot query field "foo" on type "Post".',
);
}));