本文整理汇总了TypeScript中graphql.GraphQLFieldResolver类的典型用法代码示例。如果您正苦于以下问题:TypeScript GraphQLFieldResolver类的具体用法?TypeScript GraphQLFieldResolver怎么用?TypeScript GraphQLFieldResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GraphQLFieldResolver类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: assignResolveType
(field: GraphQLField<any, any>, typeName: string, fieldName: string) => {
assignResolveType(field.type);
let mockResolver: GraphQLFieldResolver<any, any>;
// we have to handle the root mutation and root query types differently,
// because no resolver is called at the root.
/* istanbul ignore next: Must provide schema DefinitionNode with query type or a type named Query. */
const isOnQueryType: boolean = schema.getQueryType()
? schema.getQueryType().name === typeName
: false;
const isOnMutationType: boolean = schema.getMutationType()
? schema.getMutationType().name === typeName
: false;
if (isOnQueryType || isOnMutationType) {
if (mockFunctionMap.has(typeName)) {
const rootMock = mockFunctionMap.get(typeName);
// XXX: BUG in here, need to provide proper signature for rootMock.
if (rootMock(undefined, {}, {}, {} as any)[fieldName]) {
// TODO: assert that it's a function
mockResolver = (
root: any,
args: { [key: string]: any },
context: any,
info: GraphQLResolveInfo,
) => {
const updatedRoot = root || {}; // TODO: should we clone instead?
updatedRoot[fieldName] = rootMock(root, args, context, info)[
fieldName
];
// XXX this is a bit of a hack to still use mockType, which
// lets you mock lists etc. as well
// otherwise we could just set field.resolve to rootMock()[fieldName]
// it's like pretending there was a resolve function that ran before
// the root resolve function.
return mockType(field.type, typeName, fieldName)(
updatedRoot,
args,
context,
info,
);
};
}
}
}
if (!mockResolver) {
mockResolver = mockType(field.type, typeName, fieldName);
}
if (!preserveResolvers || !field.resolve) {
field.resolve = mockResolver;
} else {
const oldResolver = field.resolve;
field.resolve = (
rootObject?: any,
args?: { [key: string]: any },
context?: any,
info?: GraphQLResolveInfo,
) =>
Promise.all([
mockResolver(rootObject, args, context, info),
oldResolver(rootObject, args, context, info),
]).then(values => {
const [mockedValue, resolvedValue] = values;
// In case we couldn't mock
if (mockedValue instanceof Error) {
// only if value was not resolved, populate the error.
if (undefined === resolvedValue) {
throw mockedValue;
}
return resolvedValue;
}
if (resolvedValue instanceof Date && mockedValue instanceof Date) {
return undefined !== resolvedValue ? resolvedValue : mockedValue;
}
if (isObject(mockedValue) && isObject(resolvedValue)) {
// Object.assign() won't do here, as we need to all properties, including
// the non-enumerable ones and defined using Object.defineProperty
const emptyObject = Object.create(
Object.getPrototypeOf(resolvedValue),
);
return copyOwnProps(emptyObject, resolvedValue, mockedValue);
}
return undefined !== resolvedValue ? resolvedValue : mockedValue;
});
}
},
示例2: copyOwnProps
field.resolve = (
rootObject?: any,
args?: { [key: string]: any },
context?: any,
info?: GraphQLResolveInfo,
) =>
Promise.all([
mockResolver(rootObject, args, context, info),
oldResolver(rootObject, args, context, info),
]).then(values => {
const [mockedValue, resolvedValue] = values;
// In case we couldn't mock
if (mockedValue instanceof Error) {
// only if value was not resolved, populate the error.
if (undefined === resolvedValue) {
throw mockedValue;
}
return resolvedValue;
}
if (resolvedValue instanceof Date && mockedValue instanceof Date) {
return undefined !== resolvedValue ? resolvedValue : mockedValue;
}
if (isObject(mockedValue) && isObject(resolvedValue)) {
// Object.assign() won't do here, as we need to all properties, including
// the non-enumerable ones and defined using Object.defineProperty
const emptyObject = Object.create(
Object.getPrototypeOf(resolvedValue),
);
return copyOwnProps(emptyObject, resolvedValue, mockedValue);
}
return undefined !== resolvedValue ? resolvedValue : mockedValue;
});
示例3: return
return (root, args, ctx, info) => {
try {
const result = fn(root, args, ctx, info);
// If the resolve function returns a Promise log any Promise rejects.
if (
result &&
typeof result.then === 'function' &&
typeof result.catch === 'function'
) {
result.catch((reason: Error | string) => {
// make sure that it's an error we're logging.
const error = reason instanceof Error ? reason : new Error(reason);
logError(error);
// We don't want to leave an unhandled exception so pass on error.
return reason;
});
}
return result;
} catch (e) {
logError(e);
// we want to pass on the error, just in case.
throw e;
}
};
示例4: Error
return (root, args, ctx, info) => {
const result = fn(root, args, ctx, info);
if (typeof result === 'undefined') {
throw new Error(`Resolve function for "${hint}" returned undefined`);
}
return result;
};
示例5: innerResolver
return (obj, args, ctx, info) => {
return Promise.resolve(outerResolver(obj, args, ctx, info)).then(root => {
if (innerResolver) {
return innerResolver(root, args, ctx, info);
}
return defaultFieldResolver(root, args, ctx, info);
});
};
示例6: return
return (root, args, ctx, info) => {
if (!info.operation['__runAtMostOnce']) {
info.operation['__runAtMostOnce'] = {};
}
if (!info.operation['__runAtMostOnce'][randomNumber]) {
info.operation['__runAtMostOnce'][randomNumber] = true;
value = fn(root, args, ctx, info);
}
return value;
};