當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript graphql.TypeInfo類代碼示例

本文整理匯總了TypeScript中graphql.TypeInfo的典型用法代碼示例。如果您正苦於以下問題:TypeScript TypeInfo類的具體用法?TypeScript TypeInfo怎麽用?TypeScript TypeInfo使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了TypeInfo類的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類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。