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


TypeScript TypeInfo.getParentType方法代码示例

本文整理汇总了TypeScript中graphql.TypeInfo.getParentType方法的典型用法代码示例。如果您正苦于以下问题:TypeScript TypeInfo.getParentType方法的具体用法?TypeScript TypeInfo.getParentType怎么用?TypeScript TypeInfo.getParentType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在graphql.TypeInfo的用法示例。


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

示例1: replaceFieldsWithFragments

function replaceFieldsWithFragments(
  targetSchema: GraphQLSchema,
  document: DocumentNode,
  mapping: FieldToFragmentMapping,
): DocumentNode {
  const typeInfo = new TypeInfo(targetSchema);
  return visit(
    document,
    visitWithTypeInfo(typeInfo, {
      [Kind.SELECTION_SET](
        node: SelectionSetNode,
      ): SelectionSetNode | null | undefined {
        const parentType: GraphQLType = typeInfo.getParentType();
        if (parentType) {
          const parentTypeName = parentType.name;
          let selections = node.selections;

          if (mapping[parentTypeName]) {
            node.selections.forEach(selection => {
              if (selection.kind === Kind.FIELD) {
                const name = selection.name.value;
                const fragments = mapping[parentTypeName][name];
                if (fragments && fragments.length > 0) {
                  const fragment = concatInlineFragments(
                    parentTypeName,
                    fragments,
                  );
                  selections = selections.concat(fragment);
                }
              }
            });
          }

          if (selections !== node.selections) {
            return {
              ...node,
              selections,
            };
          }
        }
      },
    }),
  );
}
开发者ID:apollostack,项目名称:graphql-tools,代码行数:44,代码来源:ReplaceFieldWithFragment.ts

示例2: addTypenameToAbstract

function addTypenameToAbstract(
  targetSchema: GraphQLSchema,
  document: DocumentNode,
): DocumentNode {
  const typeInfo = new TypeInfo(targetSchema);
  return visit(
    document,
    visitWithTypeInfo(typeInfo, {
      [Kind.SELECTION_SET](
        node: SelectionSetNode,
      ): SelectionSetNode | null | undefined {
        const parentType: GraphQLType = typeInfo.getParentType();
        let selections = node.selections;
        if (
          parentType &&
          (parentType instanceof GraphQLInterfaceType ||
            parentType instanceof GraphQLUnionType) &&
          !selections.find(
            _ =>
              (_ as FieldNode).kind === Kind.FIELD &&
              (_ as FieldNode).name.value === '__typename',
          )
        ) {
          selections = selections.concat({
            kind: Kind.FIELD,
            name: {
              kind: Kind.NAME,
              value: '__typename',
            },
          });
        }

        if (selections !== node.selections) {
          return {
            ...node,
            selections,
          };
        }
      },
    }),
  );
}
开发者ID:apollostack,项目名称:graphql-tools,代码行数:42,代码来源:AddTypenameToAbstract.ts

示例3: stripQuery

function stripQuery(schema, queryAST, operationName, extensionFields) {
  const typeInfo = new TypeInfo(schema);

  const changedAST = visit(queryAST, visitWithTypeInfo(typeInfo, {
    [Kind.FIELD]: () => {
      const typeName = typeInfo.getParentType().name;
      const fieldName = typeInfo.getFieldDef().name;

      if (fieldName.startsWith('__'))
        return null;
      if ((extensionFields[typeName] || []).includes(fieldName))
        return null;
    },
    [Kind.SELECTION_SET]: {
      leave(node) {
        const type = typeInfo.getParentType()
        if (isAbstractType(type) || node.selections.length === 0)
          return injectTypename(node);
      }
    },
  }), null);

  return print(extractOperation(changedAST, operationName));
}
开发者ID:codeaudit,项目名称:graphql-faker,代码行数:24,代码来源:proxy.ts

示例4: expandAbstractTypes

function expandAbstractTypes(
  targetSchema: GraphQLSchema,
  mapping: TypeMapping,
  reverseMapping: TypeMapping,
  document: DocumentNode,
): DocumentNode {
  const operations: Array<
    OperationDefinitionNode
  > = document.definitions.filter(
    def => def.kind === Kind.OPERATION_DEFINITION,
  ) as Array<OperationDefinitionNode>;
  const fragments: Array<FragmentDefinitionNode> = document.definitions.filter(
    def => def.kind === Kind.FRAGMENT_DEFINITION,
  ) as Array<FragmentDefinitionNode>;

  const existingFragmentNames = fragments.map(fragment => fragment.name.value);
  let fragmentCounter = 0;
  const generateFragmentName = (typeName: string) => {
    let fragmentName;
    do {
      fragmentName = `_${typeName}_Fragment${fragmentCounter}`;
      fragmentCounter++;
    } while (existingFragmentNames.indexOf(fragmentName) !== -1);
    return fragmentName;
  };

  const newFragments: Array<FragmentDefinitionNode> = [];
  const fragmentReplacements: {
    [fragmentName: string]: Array<{ fragmentName: string; typeName: string }>;
  } = {};

  fragments.forEach((fragment: FragmentDefinitionNode) => {
    newFragments.push(fragment);
    const possibleTypes = mapping[fragment.typeCondition.name.value];
    if (possibleTypes) {
      fragmentReplacements[fragment.name.value] = [];
      possibleTypes.forEach(possibleTypeName => {
        const name = generateFragmentName(possibleTypeName);
        existingFragmentNames.push(name);
        const newFragment: FragmentDefinitionNode = {
          kind: Kind.FRAGMENT_DEFINITION,
          name: {
            kind: Kind.NAME,
            value: name,
          },
          typeCondition: {
            kind: Kind.NAMED_TYPE,
            name: {
              kind: Kind.NAME,
              value: possibleTypeName,
            },
          },
          selectionSet: fragment.selectionSet,
        };
        newFragments.push(newFragment);

        fragmentReplacements[fragment.name.value].push({
          fragmentName: name,
          typeName: possibleTypeName,
        });
      });
    }
  });

  const newDocument = {
    ...document,
    definitions: [...operations, ...newFragments],
  };
  const typeInfo = new TypeInfo(targetSchema);
  return visit(
    newDocument,
    visitWithTypeInfo(typeInfo, {
      [Kind.SELECTION_SET](node: SelectionSetNode) {
        const newSelections = [...node.selections];
        const parentType: GraphQLNamedType = getNamedType(
          typeInfo.getParentType(),
        );
        node.selections.forEach((selection: SelectionNode) => {
          if (selection.kind === Kind.INLINE_FRAGMENT) {
            const possibleTypes = mapping[selection.typeCondition.name.value];
            if (possibleTypes) {
              possibleTypes.forEach(possibleType => {
                if (
                  implementsAbstractType(
                    targetSchema,
                    parentType,
                    targetSchema.getType(possibleType),
                  )
                ) {
                  newSelections.push({
                    kind: Kind.INLINE_FRAGMENT,
                    typeCondition: {
                      kind: Kind.NAMED_TYPE,
                      name: {
                        kind: Kind.NAME,
                        value: possibleType,
                      },
                    },
                    selectionSet: selection.selectionSet,
                  });
//.........这里部分代码省略.........
开发者ID:apollostack,项目名称:graphql-tools,代码行数:101,代码来源:ExpandAbstractTypes.ts


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