• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript graphql.GraphQLFieldResolver类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;
          });
      }
    },
开发者ID:sventschui,项目名称:graphql-tools,代码行数:89,代码来源:mock.ts


示例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;
          });
开发者ID:sventschui,项目名称:graphql-tools,代码行数:35,代码来源:mock.ts


示例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;
    }
  };
开发者ID:apollostack,项目名称:graphql-tools,代码行数:25,代码来源:decorateWithLogger.ts


示例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;
 };
开发者ID:apollostack,项目名称:graphql-tools,代码行数:7,代码来源:makeExecutableSchema.ts


示例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);
   });
 };
开发者ID:apollostack,项目名称:graphql-tools,代码行数:8,代码来源:addSchemaLevelResolveFunction.ts


示例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;
 };
开发者ID:apollostack,项目名称:graphql-tools,代码行数:10,代码来源:addSchemaLevelResolveFunction.ts



注:本文中的graphql.GraphQLFieldResolver类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript graphql.GraphQLInputObjectType类代码示例发布时间:2022-05-25
下一篇:
TypeScript graphql.visit函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap