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


TypeScript GraphQLSchema.getMutationType方法代码示例

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


在下文中一共展示了GraphQLSchema.getMutationType方法的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: 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

示例4: switch

 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

示例5: 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

示例6: addSchema

  public addSchema(schema: GraphQLSchema) {
    const query = schema.getQueryType();
    if (query) {
      const fieldNames = Object.keys(query.getFields());
      fieldNames.forEach(field => {
        this.schemaByField.query[field] = schema;
      });
    }

    const mutation = schema.getMutationType();
    if (mutation) {
      const fieldNames = Object.keys(mutation.getFields());
      fieldNames.forEach(field => {
        this.schemaByField.mutation[field] = schema;
      });
    }
  }
开发者ID:sventschui,项目名称:graphql-tools,代码行数:17,代码来源:TypeRegistry.ts

示例7: getTypeSpecifiers

function getTypeSpecifiers(
  type: GraphQLType,
  schema: GraphQLSchema,
): Array<VisitSchemaKind> {
  const specifiers = [VisitSchemaKind.TYPE];
  if (type instanceof GraphQLObjectType) {
    specifiers.unshift(
      VisitSchemaKind.COMPOSITE_TYPE,
      VisitSchemaKind.OBJECT_TYPE,
    );
    const query = schema.getQueryType();
    const mutation = schema.getMutationType();
    const subscription = schema.getSubscriptionType();
    if (type === query) {
      specifiers.push(VisitSchemaKind.ROOT_OBJECT, VisitSchemaKind.QUERY);
    } else if (type === mutation) {
      specifiers.push(VisitSchemaKind.ROOT_OBJECT, VisitSchemaKind.MUTATION);
    } else if (type === subscription) {
      specifiers.push(
        VisitSchemaKind.ROOT_OBJECT,
        VisitSchemaKind.SUBSCRIPTION,
      );
    }
  } else if (type instanceof GraphQLInputObjectType) {
    specifiers.push(VisitSchemaKind.INPUT_OBJECT_TYPE);
  } else if (type instanceof GraphQLInterfaceType) {
    specifiers.push(
      VisitSchemaKind.COMPOSITE_TYPE,
      VisitSchemaKind.ABSTRACT_TYPE,
      VisitSchemaKind.INTERFACE_TYPE,
    );
  } else if (type instanceof GraphQLUnionType) {
    specifiers.push(
      VisitSchemaKind.COMPOSITE_TYPE,
      VisitSchemaKind.ABSTRACT_TYPE,
      VisitSchemaKind.UNION_TYPE,
    );
  } else if (type instanceof GraphQLEnumType) {
    specifiers.push(VisitSchemaKind.ENUM_TYPE);
  } else if (type instanceof GraphQLScalarType) {
    specifiers.push(VisitSchemaKind.SCALAR_TYPE);
  }

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

示例8: generateSimpleMapping

export function generateSimpleMapping(targetSchema: GraphQLSchema): Mapping {
  const query = targetSchema.getQueryType();
  const mutation = targetSchema.getMutationType();
  const subscription = targetSchema.getSubscriptionType();

  const result: Mapping = {};
  if (query) {
    result[query.name] = generateMappingFromObjectType(query, 'query');
  }
  if (mutation) {
    result[mutation.name] = generateMappingFromObjectType(mutation, 'mutation');
  }
  if (subscription) {
    result[subscription.name] = generateMappingFromObjectType(
      subscription,
      'subscription',
    );
  }

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

示例9: delegateToSchema

async function delegateToSchema(
  schema: GraphQLSchema,
  fragmentReplacements: {
    [typeName: string]: { [fieldName: string]: InlineFragmentNode };
  },
  operation: 'query' | 'mutation',
  fieldName: string,
  args: { [key: string]: any },
  context: { [key: string]: any },
  info: GraphQLResolveInfo,
): Promise<any> {
  let type;
  if (operation === 'mutation') {
    type = schema.getMutationType();
  } else {
    type = schema.getQueryType();
  }
  if (type) {
    const graphqlDoc: DocumentNode = createDocument(
      schema,
      fragmentReplacements,
      type,
      fieldName,
      operation,
      info.fieldNodes,
      info.fragments,
      info.operation.variableDefinitions,
    );

    const operationDefinition = graphqlDoc.definitions.find(
      ({ kind }) => kind === Kind.OPERATION_DEFINITION,
    );
    let variableValues = {};
    if (
      operationDefinition &&
      operationDefinition.kind === Kind.OPERATION_DEFINITION &&
      operationDefinition.variableDefinitions
    ) {
      operationDefinition.variableDefinitions.forEach(definition => {
        const key = definition.variable.name.value;
        // (XXX) This is kinda hacky
        let actualKey = key;
        if (actualKey.startsWith('_')) {
          actualKey = actualKey.slice(1);
        }
        const value = args[actualKey] || args[key] || info.variableValues[key];
        variableValues[key] = value;
      });
    }

    const result = await execute(
      schema,
      graphqlDoc,
      info.rootValue,
      context,
      variableValues,
    );

    return checkResultAndHandleErrors(result, info, fieldName);
  }

  throw new Error('Could not forward to merged schema');
}
开发者ID:sventschui,项目名称:graphql-tools,代码行数:63,代码来源:mergeSchemas.ts

示例10:

typeName =>
  ast.getMutationType()
    ? typeName !== (ast.getMutationType()! as any).name
    : true,
开发者ID:dhruvcodeword,项目名称:prisma,代码行数:4,代码来源:getTypeNames.ts


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