Skip to content

Feature/custom resolve field #2870

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

Closed
Closed
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
34 changes: 34 additions & 0 deletions src/execution/__tests__/resolve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,38 @@ describe('Execute: resolve function', () => {
},
});
});

it('context value custom resolve field is called', () => {
const rootValue = {
test() {
return 'default';
},
};

const result = executeSync({
schema: testSchema({ type: GraphQLString }),
document: parse('{ test }'),
rootValue,
contextValue: {
customResolveField: () => 'custom',
},
});
expect(result).to.deep.equal({
data: {
test: 'custom',
},
});

const defaultResult = executeSync({
schema: testSchema({ type: GraphQLString }),
document: parse('{ test }'),
rootValue,
contextValue: {},
});
expect(defaultResult).to.deep.equal({
data: {
test: 'default',
},
});
});
});
8 changes: 8 additions & 0 deletions src/execution/execute.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,11 @@ export function getFieldDef(
parentType: GraphQLObjectType,
fieldName: string,
): Maybe<GraphQLField<any, any>>;

export function resolveField(
exeContext: ExecutionContext,
parentType: GraphQLObjectType,
source: any,
fieldNodes: Array<FieldNode>,
path: Path,
): Promise<any> | any;
35 changes: 27 additions & 8 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,32 @@ function executeFields(
for (const responseName of Object.keys(fields)) {
const fieldNodes = fields[responseName];
const fieldPath = addPath(path, responseName, parentType.name);
const result = resolveField(
exeContext,
parentType,
sourceValue,
fieldNodes,
fieldPath,
);

let result;

// If a custom resolve field function is provided by the execution context, use it
// This allows for more application specific resolve logic such as caching by specific
// document types and execution context values
const contextValue = exeContext && (exeContext.contextValue: any);
if (typeof contextValue?.customResolveField === 'function') {
result = contextValue.customResolveField(
exeContext,
parentType,
sourceValue,
fieldNodes,
fieldPath,
);
}
// otherwise fall back on resolveField
else {
result = resolveField(
exeContext,
parentType,
sourceValue,
fieldNodes,
fieldPath,
);
}

if (result !== undefined) {
results[responseName] = result;
Expand Down Expand Up @@ -627,7 +646,7 @@ function getFieldEntryKey(node: FieldNode): string {
* then calls completeValue to complete promises, serialize scalars, or execute
* the sub-selection-set for objects.
*/
function resolveField(
export function resolveField(
exeContext: ExecutionContext,
parentType: GraphQLObjectType,
source: mixed,
Expand Down