Skip to content

Commit 9ae4399

Browse files
Kevin Lackerleebyron
authored andcommitted
improve default fn behavior (#468)
1 parent a819fb3 commit 9ae4399

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/execution/__tests__/resolve-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ describe('Execute: resolve function', () => {
6666
});
6767
});
6868

69+
it('default function passes args and context', async () => {
70+
const schema = testSchema({
71+
type: GraphQLInt,
72+
args: {
73+
addend1: { type: GraphQLInt },
74+
},
75+
});
76+
77+
class Adder {
78+
constructor(num) {
79+
this._num = num;
80+
}
81+
82+
test({addend1}, context) {
83+
return this._num + addend1 + context.addend2;
84+
}
85+
}
86+
const source = new Adder(700);
87+
88+
expect(
89+
await graphql(schema, '{ test(addend1: 80) }', source, { addend2: 9 })
90+
).to.deep.equal({
91+
data: {
92+
test: 789
93+
}
94+
});
95+
});
96+
6997
it('uses provided resolve function', async () => {
7098
const schema = testSchema({
7199
type: GraphQLString,

src/execution/execute.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,13 +990,16 @@ function defaultResolveTypeFn(
990990
* If a resolve function is not given, then a default resolve behavior is used
991991
* which takes the property of the source object of the same name as the field
992992
* and returns it as the result, or if it's a function, returns the result
993-
* of calling that function.
993+
* of calling that function while passing along args and context.
994994
*/
995995
function defaultResolveFn(source: any, args, context, { fieldName }) {
996996
// ensure source is a value for which property access is acceptable.
997997
if (typeof source === 'object' || typeof source === 'function') {
998998
const property = source[fieldName];
999-
return typeof property === 'function' ? source[fieldName]() : property;
999+
if (typeof property === 'function') {
1000+
return source[fieldName](args, context);
1001+
}
1002+
return property;
10001003
}
10011004
}
10021005

0 commit comments

Comments
 (0)