Skip to content

Commit 81671dc

Browse files
committed
Merge pull request #332 from graphql/default-resolve
Add tests and refine default resolve function.
2 parents 9761114 + d506c23 commit 81671dc

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

src/execution/__tests__/resolve.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
import { expect } from 'chai';
11+
import { describe, it } from 'mocha';
12+
13+
import {
14+
graphql,
15+
GraphQLSchema,
16+
GraphQLObjectType,
17+
GraphQLString,
18+
GraphQLInt,
19+
} from '../../';
20+
21+
describe('Execute: resolve function', () => {
22+
23+
function testSchema(testField) {
24+
return new GraphQLSchema({
25+
query: new GraphQLObjectType({
26+
name: 'Query',
27+
fields: {
28+
test: testField
29+
}
30+
})
31+
});
32+
}
33+
34+
it('default function accesses properties', async () => {
35+
const schema = testSchema({ type: GraphQLString });
36+
37+
const source = {
38+
test: 'testValue'
39+
};
40+
41+
expect(
42+
await graphql(schema, '{ test }', source)
43+
).to.deep.equal({
44+
data: {
45+
test: 'testValue'
46+
}
47+
});
48+
});
49+
50+
it('default function calls methods', async () => {
51+
const schema = testSchema({ type: GraphQLString });
52+
53+
const source = {
54+
_secret: 'secretValue',
55+
test() {
56+
return this._secret;
57+
}
58+
};
59+
60+
expect(
61+
await graphql(schema, '{ test }', source)
62+
).to.deep.equal({
63+
data: {
64+
test: 'secretValue'
65+
}
66+
});
67+
});
68+
69+
it('uses provided resolve function', async () => {
70+
const schema = testSchema({
71+
type: GraphQLString,
72+
args: {
73+
aStr: { type: GraphQLString },
74+
aInt: { type: GraphQLInt },
75+
},
76+
resolve(source, args) {
77+
return JSON.stringify([ source, args ]);
78+
}
79+
});
80+
81+
expect(
82+
await graphql(schema, '{ test }')
83+
).to.deep.equal({
84+
data: {
85+
test: '[null,{}]'
86+
}
87+
});
88+
89+
expect(
90+
await graphql(schema, '{ test }', 'Source!')
91+
).to.deep.equal({
92+
data: {
93+
test: '["Source!",{}]'
94+
}
95+
});
96+
97+
expect(
98+
await graphql(schema, '{ test(aStr: "String!") }', 'Source!')
99+
).to.deep.equal({
100+
data: {
101+
test: '["Source!",{"aStr":"String!"}]'
102+
}
103+
});
104+
105+
expect(
106+
await graphql(schema, '{ test(aInt: -123, aStr: "String!") }', 'Source!')
107+
).to.deep.equal({
108+
data: {
109+
test: '["Source!",{"aStr":"String!","aInt":-123}]'
110+
}
111+
});
112+
});
113+
114+
});

src/execution/execute.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,11 @@ function completeObjectValue(
865865
* and returns it as the result, or if it's a function, returns the result
866866
* of calling that function.
867867
*/
868-
function defaultResolveFn(source, args, { fieldName }) {
868+
function defaultResolveFn(source: any, args, { fieldName }) {
869869
// ensure source is a value for which property access is acceptable.
870-
if (typeof source !== 'number' && typeof source !== 'string' && source) {
871-
const property = (source: any)[fieldName];
872-
return typeof property === 'function' ? property.call(source) : property;
870+
if (typeof source === 'object' || typeof source === 'function') {
871+
const property = source[fieldName];
872+
return typeof property === 'function' ? source[fieldName]() : property;
873873
}
874874
}
875875

0 commit comments

Comments
 (0)