Skip to content

Commit

Permalink
Add ability to provide default field resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Jul 24, 2017
1 parent fa38e4d commit 94f69d2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/apollo-server-core/src/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ const queryType = new GraphQLObjectType({
return 'it works';
},
},
testObject: {
type: new GraphQLObjectType({
name: 'TestObject',
fields: {
testString: {
type: GraphQLString,
},
},
}),
resolve() {
return {
testString: 'a very test string',
};
},
},
testRootValue: {
type: GraphQLString,
resolve(root) {
Expand Down Expand Up @@ -265,4 +280,39 @@ describe('runQuery', () => {
expect(logs[10]).to.deep.equals({action: LogAction.request, step: LogStep.end});
});
});

it('uses custom field resolver', async () => {
const query = `
query Q1 {
testObject {
testString
}
}
`;

const result1 = await runQuery({
schema,
query: query,
operationName: 'Q1',
})

expect(result1.data).to.deep.equal({
testObject: {
testString: 'a very test string',
},
});

const result2 = await runQuery({
schema,
query: query,
operationName: 'Q1',
fieldResolver: () => 'a very testful field resolver string',
});

expect(result2.data).to.deep.equal({
testObject: {
testString: 'a very testful field resolver string',
}
});
});
});
3 changes: 3 additions & 0 deletions packages/apollo-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
GraphQLSchema,
GraphQLFieldResolver,
ExecutionResult,
DocumentNode,
parse,
Expand Down Expand Up @@ -44,6 +45,7 @@ export interface QueryOptions {
operationName?: string;
logFunction?: LogFunction;
validationRules?: Array<(context: ValidationContext) => any>;
fieldResolver?: GraphQLFieldResolver<any, any>,
// WARNING: these extra validation rules are only applied to queries
// submitted as string, not those submitted as Document!

Expand Down Expand Up @@ -128,6 +130,7 @@ function doRunQuery(options: QueryOptions): Promise<ExecutionResult> {
options.context,
options.variables,
options.operationName,
options.fieldResolver,
).then(gqlResponse => {
logFunction({action: LogAction.execute, step: LogStep.end});
logFunction({action: LogAction.request, step: LogStep.end});
Expand Down

0 comments on commit 94f69d2

Please sign in to comment.