forked from graphql/graphql-js
/
resolve-test.js
129 lines (110 loc) · 3.11 KB
/
resolve-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
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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse } from '../../language/parser';
import type { GraphQLFieldConfig } from '../../type/definition';
import { GraphQLSchema } from '../../type/schema';
import { GraphQLInt, GraphQLString } from '../../type/scalars';
import { GraphQLObjectType } from '../../type/definition';
import { executeSync } from '../execute';
describe('Execute: resolve function', () => {
function testSchema(testField: GraphQLFieldConfig<any, any>) {
return new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
test: testField,
},
}),
});
}
it('default function accesses properties', () => {
const result = executeSync({
schema: testSchema({ type: GraphQLString }),
document: parse('{ test }'),
rootValue: { test: 'testValue' },
});
expect(result).to.deep.equal({
data: {
test: 'testValue',
},
});
});
it('default function calls methods', () => {
const rootValue = {
_secret: 'secretValue',
test() {
return this._secret;
},
};
const result = executeSync({
schema: testSchema({ type: GraphQLString }),
document: parse('{ test }'),
rootValue,
});
expect(result).to.deep.equal({
data: {
test: 'secretValue',
},
});
});
it('default function passes args and context', () => {
class Adder {
_num: number;
constructor(num: number) {
this._num = num;
}
test(args: { addend1: number }, context: { addend2: number }) {
return this._num + args.addend1 + context.addend2;
}
}
const rootValue = new Adder(700);
const schema = testSchema({
type: GraphQLInt,
args: {
addend1: { type: GraphQLInt },
},
});
const contextValue = { addend2: 9 };
const document = parse('{ test(addend1: 80) }');
const result = executeSync({ schema, document, rootValue, contextValue });
expect(result).to.deep.equal({
data: { test: 789 },
});
});
it('uses provided resolve function', () => {
const schema = testSchema({
type: GraphQLString,
args: {
aStr: { type: GraphQLString },
aInt: { type: GraphQLInt },
},
resolve: (source, args) => JSON.stringify([source, args]),
});
function executeQuery(query: string, rootValue?: mixed) {
const document = parse(query);
return executeSync({ schema, document, rootValue });
}
expect(executeQuery('{ test }')).to.deep.equal({
data: {
test: '[null,{}]',
},
});
expect(executeQuery('{ test }', 'Source!')).to.deep.equal({
data: {
test: '["Source!",{}]',
},
});
expect(executeQuery('{ test(aStr: "String!") }', 'Source!')).to.deep.equal({
data: {
test: '["Source!",{"aStr":"String!"}]',
},
});
expect(
executeQuery('{ test(aInt: -123, aStr: "String!") }', 'Source!'),
).to.deep.equal({
data: {
test: '["Source!",{"aStr":"String!","aInt":-123}]',
},
});
});
});