Skip to content

Make the default resolver use args and context #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/execution/__tests__/resolve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ describe('Execute: resolve function', () => {
});
});

it('default function passes args and context', async () => {
const schema = testSchema({
type: GraphQLInt,
args: {
addend1: { type: GraphQLInt },
},
});

class Adder {
constructor(num) {
this._num = num;
}

test({addend1}, context) {
return this._num + addend1 + context.addend2;
}
}
const source = new Adder(700);

expect(
await graphql(schema, '{ test(addend1: 80) }', source, { addend2: 9 })
).to.deep.equal({
data: {
test: 789
}
});
});

it('uses provided resolve function', async () => {
const schema = testSchema({
type: GraphQLString,
Expand Down
7 changes: 5 additions & 2 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,13 +990,16 @@ function defaultResolveTypeFn(
* If a resolve function is not given, then a default resolve behavior is used
* which takes the property of the source object of the same name as the field
* and returns it as the result, or if it's a function, returns the result
* of calling that function.
* of calling that function while passing along args and context.
*/
function defaultResolveFn(source: any, args, context, { fieldName }) {
// ensure source is a value for which property access is acceptable.
if (typeof source === 'object' || typeof source === 'function') {
const property = source[fieldName];
return typeof property === 'function' ? source[fieldName]() : property;
if (typeof property === 'function') {
return source[fieldName](args, context);
}
return property;
}
}

Expand Down