本文整理汇总了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,
);
}
});
});
}
示例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
}
示例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]),
});
}
示例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,
)
}
示例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.`,
);
}
});
}
示例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;
}
示例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');
}
};
示例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);
}
}
});
示例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,
});
});
示例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);
}
});