-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmutations-test.js
68 lines (64 loc) · 1.82 KB
/
mutations-test.js
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
/* @flow strict */
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
graphql,
} from 'graphql';
describe('mutation rules', () => {
it('test nested mutations via aliases', async () => {
const serialResults = [];
const ArticleMutations = new GraphQLObjectType({
name: 'ArticleMutations',
fields: () => ({
like: {
type: GraphQLBoolean,
args: { id: { type: GraphQLInt } },
resolve: async (_, { id }) => {
await new Promise(resolve => setTimeout(resolve, 100));
serialResults.push(`like ${id} executed with timeout 100ms`);
return true;
},
},
unlike: {
type: GraphQLBoolean,
args: { id: { type: GraphQLInt } },
resolve: async (_, { id }) => {
await new Promise(resolve => setTimeout(resolve, 5));
serialResults.push(`unlike ${id} executed with timeout 5ms`);
return true;
},
},
}),
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: { hello: { type: GraphQLString, resolve: () => 'world' } },
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: { article: { type: ArticleMutations, resolve: () => ({}) } },
}),
});
await graphql({
schema,
source: `
mutation {
op1: article { like(id: 1) }
op2: article { like(id: 2) }
op3: article { unlike(id: 3) }
op4: article { like(id: 4) }
}
`,
});
expect(serialResults).toEqual([
'like 1 executed with timeout 100ms',
'like 2 executed with timeout 100ms',
'unlike 3 executed with timeout 5ms',
'like 4 executed with timeout 100ms',
]);
});
});