本文整理汇总了TypeScript中graphql.GraphQLSchema.getTypeMap方法的典型用法代码示例。如果您正苦于以下问题:TypeScript GraphQLSchema.getTypeMap方法的具体用法?TypeScript GraphQLSchema.getTypeMap怎么用?TypeScript GraphQLSchema.getTypeMap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphql.GraphQLSchema
的用法示例。
在下文中一共展示了GraphQLSchema.getTypeMap方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: 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.`,
);
}
});
}
示例2: 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,
)
}
示例3: 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;
}
示例4: forEachField
function forEachField(schema: GraphQLSchema, fn: FieldIteratorFn): void {
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(typeName => {
const type = typeMap[typeName];
if (!getNamedType(type).name.startsWith('__') && type instanceof GraphQLObjectType) {
const fields = type.getFields();
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];
fn(field, typeName, fieldName);
});
}
});
}
示例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]),
});
}
示例6: extractPossibleTypes
function extractPossibleTypes(
transformedSchema: GraphQLSchema,
targetSchema: GraphQLSchema,
) {
const typeMap = transformedSchema.getTypeMap();
const mapping: TypeMapping = {};
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);
}
}
});
return mapping;
}