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

TypeScript graphql.GraphQLSchema类代码示例

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

本文整理汇总了TypeScript中graphql.GraphQLSchema的典型用法代码示例。如果您正苦于以下问题:TypeScript GraphQLSchema类的具体用法?TypeScript GraphQLSchema怎么用?TypeScript GraphQLSchema使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了GraphQLSchema类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: addSchemaLevelResolveFunction

// wraps all resolve functions of query, mutation or subscription fields
// with the provided function to simulate a root schema level resolve funciton
function addSchemaLevelResolveFunction(
  schema: GraphQLSchema,
  fn: GraphQLFieldResolver<any, any>,
): void {
  // TODO test that schema is a schema, fn is a function
  const rootTypes = [
    schema.getQueryType(),
    schema.getMutationType(),
    schema.getSubscriptionType(),
  ].filter(x => !!x);
  rootTypes.forEach(type => {
    // XXX this should run at most once per request to simulate a true root resolver
    // for graphql-js this is an approximation that works with queries but not mutations
    const rootResolveFn = runAtMostOncePerRequest(fn);
    const fields = type.getFields();
    Object.keys(fields).forEach(fieldName => {
      // XXX if the type is a subscription, a same query AST will be ran multiple times so we
      // deactivate here the runOnce if it's a subscription. This may not be optimal though...
      if (type === schema.getSubscriptionType()) {
        fields[fieldName].resolve = wrapResolver(fields[fieldName].resolve, fn);
      } else {
        fields[fieldName].resolve = wrapResolver(
          fields[fieldName].resolve,
          rootResolveFn,
        );
      }
    });
  });
}
开发者ID:apollostack,项目名称:graphql-tools,代码行数:31,代码来源:addSchemaLevelResolveFunction.ts


示例2: getTypeForRootFieldName

export function getTypeForRootFieldName(
  rootFieldName: string,
  operation: Operation,
  schema: GraphQLSchema,
): GraphQLOutputType {
  if (operation === 'mutation' && !schema.getMutationType()) {
    throw new Error(`Schema doesn't have mutation type`)
  }

  if (operation === 'subscription' && !schema.getSubscriptionType()) {
    throw new Error(`Schema doesn't have subscription type`)
  }

  const rootType =
    {
      query: () => schema.getQueryType(),
      mutation: () => schema.getMutationType()!,
      subscription: () => schema.getSubscriptionType()!,
    }[operation]() || undefined!

  const rootField = rootType.getFields()[rootFieldName]

  if (!rootField) {
    throw new Error(`No such root field found: ${rootFieldName}`)
  }

  return rootField.type
}
开发者ID:dhruvcodeword,项目名称:prisma,代码行数:28,代码来源:index.ts


示例3: visitSchema

export function visitSchema(
  schema: GraphQLSchema,
  visitor: SchemaVisitor,
  stripResolvers?: boolean,
) {
  const types = {};
  const resolveType = createResolveType(name => {
    if (typeof types[name] === 'undefined') {
      throw new Error(`Can't find type ${name}.`);
    }
    return types[name];
  });
  const queryType = schema.getQueryType();
  const mutationType = schema.getMutationType();
  const subscriptionType = schema.getSubscriptionType();
  const typeMap = schema.getTypeMap();
  Object.keys(typeMap).map((typeName: string) => {
    const type = typeMap[typeName];
    if (isNamedType(type) && getNamedType(type).name.slice(0, 2) !== '__') {
      const specifiers = getTypeSpecifiers(type, schema);
      const typeVisitor = getVisitor(visitor, specifiers);
      if (typeVisitor) {
        const result: GraphQLNamedType | null | undefined = typeVisitor(
          type,
          schema,
        );
        if (typeof result === 'undefined') {
          types[typeName] = recreateType(type, resolveType, !stripResolvers);
        } else if (result === null) {
          types[typeName] = null;
        } else {
          types[typeName] = recreateType(result, resolveType, !stripResolvers);
        }
      } else {
        types[typeName] = recreateType(type, resolveType, !stripResolvers);
      }
    }
  });
  return new GraphQLSchema({
    query: queryType ? (types[queryType.name] as GraphQLObjectType) : null,
    mutation: mutationType
      ? (types[mutationType.name] as GraphQLObjectType)
      : null,
    subscription: subscriptionType
      ? (types[subscriptionType.name] as GraphQLObjectType)
      : null,
    types: Object.keys(types).map(name => types[name]),
  });
}
开发者ID:apollostack,项目名称:graphql-tools,代码行数:49,代码来源:visitSchema.ts


示例4: getTypeNames

export function getTypeNames(ast: GraphQLSchema) {
  // Create types
  return Object.keys(ast.getTypeMap())
    .filter(typeName => !typeName.startsWith('__'))
    .filter(typeName => typeName !== (ast.getQueryType() as any).name)
    .filter(
      typeName =>
        ast.getMutationType()
          ? typeName !== (ast.getMutationType()! as any).name
          : true,
    )
    .filter(
      typeName =>
        ast.getSubscriptionType()
          ? typeName !== (ast.getSubscriptionType()! as any).name
          : true,
    )
    .sort(
      (a, b) =>
        (ast.getType(a) as any).constructor.name <
        (ast.getType(b) as any).constructor.name
          ? -1
          : 1,
    )
}
开发者ID:dhruvcodeword,项目名称:prisma,代码行数:25,代码来源:getTypeNames.ts


示例5: checkForResolveTypeResolver

// If we have any union or interface types throw if no there is no resolveType or isTypeOf resolvers
function checkForResolveTypeResolver(
  schema: GraphQLSchema,
  requireResolversForResolveType?: boolean,
) {
  Object.keys(schema.getTypeMap())
    .map(typeName => schema.getType(typeName))
    .forEach((type: GraphQLUnionType | GraphQLInterfaceType) => {
      if (
        !(
          type instanceof GraphQLUnionType ||
          type instanceof GraphQLInterfaceType
        )
      ) {
        return;
      }
      if (!type.resolveType) {
        if (requireResolversForResolveType === false) {
          return;
        }
        if (requireResolversForResolveType === true) {
          throw new SchemaError(
            `Type "${type.name}" is missing a "resolveType" resolver`,
          );
        }
        // tslint:disable-next-line:max-line-length
        console.warn(
          `Type "${
            type.name
          }" is missing a "__resolveType" resolver. Pass false into `  +
          `"resolverValidationOptions.requireResolversForResolveType" to disable this warning.`,
        );
      }
    });
}
开发者ID:apollostack,项目名称:graphql-tools,代码行数:35,代码来源:checkForResolveTypeResolver.ts


示例6: extendResolversFromInterfaces

function extendResolversFromInterfaces(
  schema: GraphQLSchema,
  resolvers: IResolvers,
) {
  const typeNames = Object.keys({
    ...schema.getTypeMap(),
    ...resolvers,
  });

  const extendedResolvers: IResolvers = {};
  typeNames.forEach(typeName => {
    const typeResolvers = resolvers[typeName];
    const type = schema.getType(typeName);
    if (type instanceof GraphQLObjectType) {
      const interfaceResolvers = type
        .getInterfaces()
        .map(iFace => resolvers[iFace.name]);
      extendedResolvers[typeName] = Object.assign(
        {},
        ...interfaceResolvers,
        typeResolvers,
      );
    } else {
      if (typeResolvers) {
        extendedResolvers[typeName] = typeResolvers;
      }
    }
  });

  return extendedResolvers;
}
开发者ID:apollostack,项目名称:graphql-tools,代码行数:31,代码来源:extendResolversFromInterfaces.ts


示例7: Error

 const getOperationFields: (operation: OperationTypeNode) => GraphQLObjectType | null | undefined = operation => {
   switch (operation) {
     case 'query':
       return parsedSchema.getQueryType();
     case 'mutation':
       return parsedSchema.getMutationType();
     case 'subscription':
       return parsedSchema.getSubscriptionType();
     default:
       throw new Error('Unsupported Operation');
   }
 };
开发者ID:avantcredit,项目名称:gql2ts,代码行数:12,代码来源:index.ts


示例8:

 Object.keys(typeMap).forEach(typeName => {
   const type = typeMap[typeName];
   if (isAbstractType(type)) {
     const targetType = targetSchema.getType(typeName);
     if (!isAbstractType(targetType)) {
       const implementations = transformedSchema.getPossibleTypes(type) || [];
       mapping[typeName] = implementations
         .filter(impl => targetSchema.getType(impl.name))
         .map(impl => impl.name);
     }
   }
 });
开发者ID:apollostack,项目名称:graphql-tools,代码行数:12,代码来源:ExpandAbstractTypes.ts


示例9: if

  operations.forEach((operation: OperationDefinitionNode) => {
    let type;
    if (operation.operation === 'subscription') {
      type = targetSchema.getSubscriptionType();
    } else if (operation.operation === 'mutation') {
      type = targetSchema.getMutationType();
    } else {
      type = targetSchema.getQueryType();
    }

    const {
      selectionSet,
      usedFragments: operationUsedFragments,
      usedVariables: operationUsedVariables,
    } = filterSelectionSet(
      targetSchema,
      type,
      validFragmentsWithType,
      operation.selectionSet
    );

    usedFragments = union(usedFragments, operationUsedFragments);

    const {
      usedVariables: collectedUsedVariables,
      newFragments: collectedNewFragments,
      fragmentSet: collectedFragmentSet,
    } = collectFragmentVariables(
      targetSchema,
      fragmentSet,
      validFragments,
      validFragmentsWithType,
      usedFragments,
    );
    const fullUsedVariables =
      union(operationUsedVariables, collectedUsedVariables);
    newFragments = collectedNewFragments;
    fragmentSet = collectedFragmentSet;

    const variableDefinitions = operation.variableDefinitions.filter(
      (variable: VariableDefinitionNode) =>
        fullUsedVariables.indexOf(variable.variable.name.value) !== -1,
    );

    newOperations.push({
      kind: Kind.OPERATION_DEFINITION,
      operation: operation.operation,
      name: operation.name,
      directives: operation.directives,
      variableDefinitions,
      selectionSet,
    });
  });
开发者ID:apollostack,项目名称:graphql-tools,代码行数:53,代码来源:FilterToSchema.ts


示例10:

 Object.keys(fragments).forEach(fragmentName => {
   const fragment = fragments[fragmentName];
   const typeName = fragment.typeCondition.name.value;
   const innerType = schema.getType(typeName);
   if (innerType) {
     validFragments.push(fragment);
   }
 });
开发者ID:sventschui,项目名称:graphql-tools,代码行数:8,代码来源:mergeSchemas.ts



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
TypeScript graphql.TypeInfo类代码示例发布时间:2022-05-25
下一篇:
TypeScript graphql.GraphQLResult类代码示例发布时间: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