Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function executeField(field, rootValue, execContext) {
const { variables, contextValue, resolver } = execContext
const fieldName = field.name.value
const args = argumentsObjectFromField(field, variables)
const info = {
isLeaf: !field.selectionSet,
resultKey: resultKeyNameFromField(field),
directives: getDirectivesFromField(field, variables)
}
const result = await resolver(fieldName, rootValue, args, contextValue, info)
// Handle all scalar types here
if (!field.selectionSet) {
return result
}
// From here down, the field has a selection set, which means it's trying to
// query a GraphQLObjectType
return executeObjectSelection(field, result, execContext)
}
selectionSet.selections.forEach(selection => {
if (!shouldInclude(selection, variables)) {
// Skip this entirely
return;
}
if (isField(selection)) {
const fieldResult = handleMissing(executeField(selection, rootValue, execContext));
if (typeof fieldResult !== 'undefined') {
merge(finalResult.result, {
[resultKeyNameFromField(selection)]: fieldResult,
});
}
} else {
let fragment: InlineFragmentNode | FragmentDefinitionNode;
if (isInlineFragment(selection)) {
fragment = selection;
} else {
// This is a named fragment
fragment = fragmentMap[selection.name.value];
if (!fragment) {
throw new Error(`No fragment named ${selection.name.value}`);
}
}
const execute = async selection => {
if (!shouldInclude(selection, variables)) {
// Skip this entirely
return;
}
if (isField(selection)) {
const fieldResult = await executeField(selection, rootValue, execContext);
const resultFieldKey = resultKeyNameFromField(selection);
if (fieldResult !== undefined) {
if (result[resultFieldKey] === undefined) {
result[resultFieldKey] = fieldResult;
} else {
merge(result[resultFieldKey], fieldResult);
}
}
return;
}
let fragment: InlineFragmentNode | FragmentDefinitionNode;
if (isInlineFragment(selection)) {
fragment = selection;
selectionSet.selections.forEach(selection => {
if (!shouldInclude(selection, variables)) {
// Skip this entirely
return;
}
if (isField(selection)) {
const fieldResult = executeField(selection, rootValue, execContext);
const resultFieldKey = resultKeyNameFromField(selection);
if (fieldResult !== undefined) {
if (result[resultFieldKey] === undefined) {
result[resultFieldKey] = fieldResult;
} else {
merge(result[resultFieldKey], fieldResult);
}
}
} else {
let fragment: InlineFragmentNode | FragmentDefinitionNode;
if (isInlineFragment(selection)) {
fragment = selection;
} else {
// This is a named fragment
fragment = fragmentMap[selection.name.value];
private async resolveField(
field: FieldNode,
rootValue: any,
execContext: ExecContext,
): Promise {
const { variables } = execContext;
const fieldName = field.name.value;
const aliasedFieldName = resultKeyNameFromField(field);
const aliasUsed = fieldName !== aliasedFieldName;
const defaultResult = rootValue[aliasedFieldName] || rootValue[fieldName];
let resultPromise = Promise.resolve(defaultResult);
// Usually all local resolvers are run when passing through here, but
// if we've specifically identified that we only want to run forced
// resolvers (that is, resolvers for fields marked with
// `@client(always: true)`), then we'll skip running non-forced resolvers.
if (
!execContext.onlyRunForcedResolvers ||
this.shouldForceResolvers(field)
) {
const resolverType =
rootValue.__typename || execContext.defaultOperationType;
const resolverMap = this.resolvers && this.resolvers[resolverType];
if (resolverMap) {
function executeField(
field: FieldNode,
rootValue: any,
execContext: ExecContext,
): any {
const { variableValues: variables, contextValue, resolver } = execContext;
const fieldName = field.name.value;
const args = argumentsObjectFromField(field, variables);
const info: ExecInfo = {
isLeaf: !field.selectionSet,
resultKey: resultKeyNameFromField(field),
directives: getDirectiveInfoFromField(field, variables),
};
const result = resolver(fieldName, rootValue, args, contextValue, info);
// Handle all scalar types here
if (!field.selectionSet) {
return result;
}
// From here down, the field has a selection set, which means it's trying to
// query a GraphQLObjectType
if (result == null) {
// Basically any field in a GraphQL response can be null, or missing
return result;
}
function executeField(
field: FieldNode,
rootValue: any,
execContext: ExecContext,
): ExecResult {
const { variableValues: variables, contextValue } = execContext;
const fieldName = field.name.value;
const args = argumentsObjectFromField(field, variables);
const info: ExecInfo = {
resultKey: resultKeyNameFromField(field),
directives: getDirectiveInfoFromField(field, variables),
};
const readStoreResult = readStoreResolver(
fieldName,
rootValue,
args,
contextValue,
info,
);
// Handle all scalar types here
if (!field.selectionSet) {
return readStoreResult;
}
private executeField(
object: StoreObject,
typename: string | void,
field: FieldNode,
execContext: ExecContext,
): ExecResult {
const { variableValues: variables, contextValue } = execContext;
const fieldName = field.name.value;
const args = argumentsObjectFromField(field, variables);
const info: ExecInfo = {
resultKey: resultKeyNameFromField(field),
directives: getDirectiveInfoFromField(field, variables),
};
const readStoreResult = readStoreResolver(
object,
typename,
fieldName,
args,
contextValue,
info,
);
if (Array.isArray(readStoreResult.result)) {
return this.combineExecResults(
readStoreResult,
this.executeSubSelectedArray({
props: (props) => {
const { ownProps: { client } } = props;
const mutationName = resultKeyNameFromField((mutation.definitions[0] as OperationDefinitionNode).selectionSet.selections[0] as FieldNode);
return {
[mutationName]: (variables) => {
return props.mutate({
variables,
...(buildMutation(client, mutation, variables, cacheUpdateQuery, typename, idField, operationType) as MutationOpts),
});
}
}
},
});
fieldResult => {
if (typeof fieldResult !== 'undefined') {
resultsToMerge.push({
[resultKeyNameFromField(selection)]: fieldResult,
} as TData);
}
},
);