graphql#defaultFieldResolver TypeScript Examples
The following examples show how to use
graphql#defaultFieldResolver.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: upper-case.directive.ts From nestjs-mercurius with MIT License | 6 votes |
visitFieldDefinition(
field: GraphQLField<any, any>,
_details: { objectType: GraphQLObjectType | GraphQLInterfaceType },
): GraphQLField<any, any> | void | null {
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
const result = await resolve.apply(this, args);
if (typeof result === 'string') {
return result.toUpperCase();
}
return result;
};
}
Example #2
Source File: bareRename.ts From graphql-mesh with MIT License | 6 votes |
defaultResolverComposer =
(resolveFn = defaultFieldResolver, originalFieldName: string, argsMap: { [key: string]: string }) =>
(root: any, args: any, context: any, info: any) =>
resolveFn(
root,
// map renamed arguments to their original value
argsMap
? Object.keys(args).reduce((acc, key: string) => ({ ...acc, [argsMap[key] || key]: args[key] }), {})
: args,
context,
// map renamed field name to its original value
originalFieldName ? { ...info, fieldName: originalFieldName } : info
)
Example #3
Source File: index.ts From graphql-mesh with MIT License | 5 votes |
transformSchema(schema: GraphQLSchema) {
const additionalTypeDefs =
this.typeDefs &&
loadTypedefsSync(this.typeDefs, {
cwd: this.baseDir,
loaders: [new CodeFileLoader(), new GraphQLFileLoader()],
});
const baseSchema = additionalTypeDefs ? extendSchema(schema, additionalTypeDefs[0].document) : schema;
const transformedSchema = mapSchema(baseSchema, {
[MapperKind.COMPOSITE_FIELD]: (
fieldConfig: GraphQLFieldConfig<any, any>,
currentFieldName: string,
typeName: string
) => {
const fieldKey = `${typeName}.${currentFieldName}`;
const newFieldConfig = this.replacementsMap.get(fieldKey);
if (!newFieldConfig) {
return undefined;
}
const fieldName = newFieldConfig.name || currentFieldName;
const targetFieldName = newFieldConfig.field;
const targetFieldConfig = selectObjectFields(
baseSchema,
newFieldConfig.type,
fieldName => fieldName === targetFieldName
)[targetFieldName];
if (newFieldConfig.scope === 'config') {
const targetResolver = targetFieldConfig.resolve;
targetFieldConfig.resolve = newFieldConfig.composer(targetResolver);
// replace the entire field config
return [fieldName, targetFieldConfig];
}
// override field type with the target type requested
fieldConfig.type = targetFieldConfig.type;
// If renaming fields that don't have a custom resolver, we need to map response to original field name
if (newFieldConfig.name && !fieldConfig.resolve) fieldConfig.resolve = source => source[currentFieldName];
if (newFieldConfig.scope === 'hoistValue') {
// implement value hoisting by wrapping a default composer that hoists the value from resolver result
fieldConfig.resolve = defaultHoistFieldComposer(fieldConfig.resolve || defaultFieldResolver, targetFieldName);
}
// wrap user-defined composer to current field resolver or, if not preset, defaultFieldResolver
fieldConfig.resolve = newFieldConfig.composer(fieldConfig.resolve || defaultFieldResolver);
// avoid re-iterating over replacements that have already been applied
this.replacementsMap.delete(fieldKey);
return [fieldName, fieldConfig];
},
});
return transformedSchema;
}