当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript graphql.GraphQLNamedType类代码示例

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


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

示例1: filterSelectionSet

function filterSelectionSet(
  schema: GraphQLSchema,
  type: GraphQLType,
  validFragments: { [name: string]: GraphQLType },
  selectionSet: SelectionSetNode,
) {
  const usedFragments: Array<string> = [];
  const usedVariables: Array<string> = [];
  const typeStack: Array<GraphQLType> = [type];

  // Should be rewritten using visitWithSchema
  const filteredSelectionSet = visit(selectionSet, {
    [Kind.FIELD]: {
      enter(node: FieldNode): null | undefined | FieldNode {
        let parentType: GraphQLNamedType = resolveType(
          typeStack[typeStack.length - 1],
        );
        if (
          parentType instanceof GraphQLObjectType ||
          parentType instanceof GraphQLInterfaceType
        ) {
          const fields = parentType.getFields();
          const field =
            node.name.value === '__typename'
              ? TypeNameMetaFieldDef
              : fields[node.name.value];
          if (!field) {
            return null;
          } else {
            typeStack.push(field.type);
          }

          const argNames = (field.args || []).map(arg => arg.name);
          if (node.arguments) {
            let args = node.arguments.filter((arg: ArgumentNode) => {
              return argNames.indexOf(arg.name.value) !== -1;
            });
            if (args.length !== node.arguments.length) {
              return {
                ...node,
                arguments: args,
              };
            }
          }
        } else if (
          parentType instanceof GraphQLUnionType &&
          node.name.value === '__typename'
        ) {
          typeStack.push(TypeNameMetaFieldDef.type);
        }
      },
      leave(node: FieldNode): null | undefined | FieldNode {
        const currentType = typeStack.pop();
        const resolvedType = resolveType(currentType);
        if (
          resolvedType instanceof GraphQLObjectType ||
          resolvedType instanceof GraphQLInterfaceType
        ) {
          const selections = node.selectionSet && node.selectionSet.selections || null;
          if (!selections || selections.length === 0) {
            // need to remove any added variables. Is there a better way to do this?
            visit(node, {
              [Kind.VARIABLE](variableNode: VariableNode) {
                const index = usedVariables.indexOf(variableNode.name.value);
                if (index !== -1) {
                  usedVariables.splice(index, 1);
                }
              }
            }
            );
            return null;
          }
        }
      },
    },
    [Kind.FRAGMENT_SPREAD](node: FragmentSpreadNode): null | undefined {
      if (node.name.value in validFragments) {
        const parentType: GraphQLNamedType = resolveType(
          typeStack[typeStack.length - 1],
        );
        const innerType = validFragments[node.name.value];
        if (!implementsAbstractType(schema, parentType, innerType)) {
          return null;
        } else {
          usedFragments.push(node.name.value);
          return;
        }
      } else {
        return null;
      }
    },
    [Kind.INLINE_FRAGMENT]: {
      enter(node: InlineFragmentNode): null | undefined {
        if (node.typeCondition) {
          const innerType = schema.getType(node.typeCondition.name.value);
          const parentType: GraphQLNamedType = resolveType(
            typeStack[typeStack.length - 1],
          );
          if (implementsAbstractType(schema, parentType, innerType)) {
            typeStack.push(innerType);
//.........这里部分代码省略.........
开发者ID:apollostack,项目名称:graphql-tools,代码行数:101,代码来源:FilterToSchema.ts

示例2: filterSelectionSet

function filterSelectionSet(
  schema: GraphQLSchema,
  fragmentReplacements: {
    [typeName: string]: { [fieldName: string]: InlineFragmentNode };
  },
  type: GraphQLType,
  selectionSet: SelectionSetNode,
  validFragments: Array<FragmentDefinitionNode>,
): {
  selectionSet: SelectionSetNode;
  usedFragments: Array<string>;
  usedVariables: Array<string>;
} {
  const usedFragments: Array<string> = [];
  const usedVariables: Array<string> = [];
  const typeStack: Array<GraphQLType> = [type];
  const filteredSelectionSet = visit(selectionSet, {
    [Kind.FIELD]: {
      enter(node: FieldNode): null | undefined | FieldNode {
        let parentType: GraphQLNamedType = resolveType(
          typeStack[typeStack.length - 1],
        );
        if (
          parentType instanceof GraphQLObjectType ||
          parentType instanceof GraphQLInterfaceType
        ) {
          const fields = parentType.getFields();
          const field =
            node.name.value === '__typename'
              ? TypeNameMetaFieldDef
              : fields[node.name.value];
          if (!field) {
            return null;
          } else {
            typeStack.push(field.type);
          }
        }
      },
      leave() {
        typeStack.pop();
      },
    },
    [Kind.SELECTION_SET](
      node: SelectionSetNode,
    ): SelectionSetNode | null | undefined {
      const parentType: GraphQLType = resolveType(
        typeStack[typeStack.length - 1],
      );
      const parentTypeName = parentType.name;
      let selections = node.selections;
      if (
        parentType instanceof GraphQLInterfaceType ||
        parentType instanceof GraphQLUnionType
      ) {
        selections = selections.concat({
          kind: Kind.FIELD,
          name: {
            kind: Kind.NAME,
            value: '__typename',
          },
        });
      }

      if (fragmentReplacements[parentTypeName]) {
        selections.forEach(selection => {
          if (selection.kind === Kind.FIELD) {
            const name = selection.name.value;
            const fragment = fragmentReplacements[parentTypeName][name];
            if (fragment) {
              selections = selections.concat(fragment);
            }
          }
        });
      }

      if (selections !== node.selections) {
        return {
          ...node,
          selections,
        };
      }
    },
    [Kind.FRAGMENT_SPREAD](node: FragmentSpreadNode): null | undefined {
      const fragmentFiltered = validFragments.filter(
        frg => frg.name.value === node.name.value,
      );
      const fragment = fragmentFiltered[0];
      if (fragment) {
        if (fragment.typeCondition) {
          const innerType = schema.getType(fragment.typeCondition.name.value);
          const parentType: GraphQLNamedType = resolveType(
            typeStack[typeStack.length - 1],
          );
          if (!implementsAbstractType(parentType, innerType)) {
            return null;
          }
        }
        usedFragments.push(node.name.value);
        return;
      } else {
//.........这里部分代码省略.........
开发者ID:sventschui,项目名称:graphql-tools,代码行数:101,代码来源:mergeSchemas.ts

示例3: recreateType

export function recreateType(
  type: GraphQLNamedType,
  resolveType: ResolveType<any>,
  keepResolvers: boolean,
): GraphQLNamedType {
  if (type instanceof GraphQLObjectType) {
    const fields = type.getFields();
    const interfaces = type.getInterfaces();

    return new GraphQLObjectType({
      name: type.name,
      description: type.description,
      astNode: type.astNode,
      isTypeOf: keepResolvers ? type.isTypeOf : undefined,
      fields: () =>
        fieldMapToFieldConfigMap(fields, resolveType, keepResolvers),
      interfaces: () => interfaces.map(iface => resolveType(iface)),
    });
  } else if (type instanceof GraphQLInterfaceType) {
    const fields = type.getFields();

    return new GraphQLInterfaceType({
      name: type.name,
      description: type.description,
      astNode: type.astNode,
      fields: () =>
        fieldMapToFieldConfigMap(fields, resolveType, keepResolvers),
      resolveType: keepResolvers
        ? type.resolveType
        : (parent, context, info) =>
            resolveFromParentTypename(parent, info.schema),
    });
  } else if (type instanceof GraphQLUnionType) {
    return new GraphQLUnionType({
      name: type.name,
      description: type.description,
      astNode: type.astNode,

      types: () => type.getTypes().map(unionMember => resolveType(unionMember)),
      resolveType: keepResolvers
        ? type.resolveType
        : (parent, context, info) =>
            resolveFromParentTypename(parent, info.schema),
    });
  } else if (type instanceof GraphQLInputObjectType) {
    return new GraphQLInputObjectType({
      name: type.name,
      description: type.description,
      astNode: type.astNode,

      fields: () =>
        inputFieldMapToFieldConfigMap(type.getFields(), resolveType),
    });
  } else if (type instanceof GraphQLEnumType) {
    const values = type.getValues();
    const newValues = {};
    values.forEach(value => {
      newValues[value.name] = {
        value: value.value,
        deprecationReason: value.deprecationReason,
        description: value.description,
        astNode: value.astNode,
      };
    });
    return new GraphQLEnumType({
      name: type.name,
      description: type.description,
      astNode: type.astNode,
      values: newValues,
    });
  } else if (type instanceof GraphQLScalarType) {
    if (keepResolvers || isSpecifiedScalarType(type)) {
      return type;
    } else {
      return new GraphQLScalarType({
        name: type.name,
        description: type.description,
        astNode: type.astNode,
        serialize(value: any) {
          return value;
        },
        parseValue(value: any) {
          return value;
        },
        parseLiteral(ast: ValueNode) {
          return parseLiteral(ast);
        },
      });
    }
  } else {
    throw new Error(`Invalid type ${type}`);
  }
}
开发者ID:apollostack,项目名称:graphql-tools,代码行数:93,代码来源:schemaRecreation.ts

示例4: resolveType

types: () => type.getTypes().map(unionMember => resolveType(unionMember)),
开发者ID:apollostack,项目名称:graphql-tools,代码行数:1,代码来源:schemaRecreation.ts

示例5: inputFieldMapToFieldConfigMap

 fields: () =>
   inputFieldMapToFieldConfigMap(type.getFields(), resolveType),
开发者ID:apollostack,项目名称:graphql-tools,代码行数:2,代码来源:schemaRecreation.ts


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