本文整理汇总了TypeScript中graphql.getNamedType函数的典型用法代码示例。如果您正苦于以下问题:TypeScript getNamedType函数的具体用法?TypeScript getNamedType怎么用?TypeScript getNamedType使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getNamedType函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: resolveType
const resolveType = <T extends GraphQLType>(type: T): T | GraphQLType => {
if (type instanceof GraphQLList) {
const innerType = resolveType(type.ofType);
if (innerType === null) {
return null;
} else {
return new GraphQLList(innerType) as T;
}
} else if (type instanceof GraphQLNonNull) {
const innerType = resolveType(type.ofType);
if (innerType === null) {
return null;
} else {
return new GraphQLNonNull(innerType) as T;
}
} else if (isNamedType(type)) {
const typeName = getNamedType(type).name;
switch (typeName) {
case GraphQLInt.name:
return GraphQLInt;
case GraphQLFloat.name:
return GraphQLFloat;
case GraphQLString.name:
return GraphQLString;
case GraphQLBoolean.name:
return GraphQLBoolean;
case GraphQLID.name:
return GraphQLID;
default:
return getType(typeName, type);
}
} else {
return type;
}
};
示例2: isNamedType
Object.keys(typeMap).forEach(typeName => {
const type: GraphQLType = typeMap[typeName];
if (
isNamedType(type) &&
getNamedType(type).name.slice(0, 2) !== '__' &&
type !== queryType &&
type !== mutationType
) {
let newType;
if (isCompositeType(type) || type instanceof GraphQLInputObjectType) {
newType = recreateCompositeType(schema, type, typeRegistry);
} else {
newType = getNamedType(type);
}
typeRegistry.addType(newType.name, newType, onTypeConflict);
}
});
示例3: fn
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);
});
}
});
示例4: GraphQLList
public resolveType<T extends GraphQLType>(type: T): T {
if (type instanceof GraphQLList) {
return new GraphQLList(this.resolveType(type.ofType)) as T;
} else if (type instanceof GraphQLNonNull) {
return new GraphQLNonNull(this.resolveType(type.ofType)) as T;
} else if (isNamedType(type)) {
return this.getType(getNamedType(type).name) as T;
} else {
return type;
}
}
示例5: getTypeName
/**
* Gets the standard GraphQL type string representation.
*
* @private
*/
function getTypeName (type: GraphQLType): string {
// Return an empty string for tests.
if (type == null) return ''
// If this is a named type, just return the typeâs name.
if (type === getNamedType(type)) return type.name
// If this is non-null return the nullable typeâs name.
if (type instanceof GraphQLNonNull) return getTypeName(type.ofType)
// If this is a list, wrap the name with `[]`.
if (type instanceof GraphQLList) return `[${getTypeName(type.ofType)}]`
throw new Error('Unrecognized unnamed GraphQL type.')
}
示例6: fn
Object.keys(typeMap).forEach(typeName => {
const type = typeMap[typeName];
// TODO: maybe have an option to include these?
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);
});
}
});
示例7: forEachField
forEachField(schema, (field, typeName, fieldName) => {
// requires a resolve function for *every* field.
if (requireResolversForAllFields) {
expectResolveFunction(field, typeName, fieldName);
}
// requires a resolve function on every field that has arguments
if (requireResolversForArgs && field.args.length > 0) {
expectResolveFunction(field, typeName, fieldName);
}
// requires a resolve function on every field that returns a non-scalar type
if (
requireResolversForNonScalar &&
!(getNamedType(field.type) instanceof GraphQLScalarType)
) {
expectResolveFunction(field, typeName, fieldName);
}
});
示例8: getTypeSpecifiers
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);
}
}
});
示例9: assignResolveType
function assignResolveType(type: GraphQLType) {
const fieldType = getNullableType(type);
const namedFieldType = getNamedType(fieldType);
const oldResolveType = getResolveType(namedFieldType);
if (preserveResolvers && oldResolveType && oldResolveType.length) {
return;
}
if (
namedFieldType instanceof GraphQLUnionType ||
namedFieldType instanceof GraphQLInterfaceType
) {
// the default `resolveType` always returns null. We add a fallback
// resolution that works with how unions and interface are mocked
namedFieldType.resolveType = (
data: any,
context: any,
info: GraphQLResolveInfo,
) => {
return info.schema.getType(data.__typename) as GraphQLObjectType;
};
}
}
示例10: return
return (
root: any,
args: { [key: string]: any },
context: any,
info: GraphQLResolveInfo,
): any => {
// nullability doesn't matter for the purpose of mocking.
const fieldType = getNullableType(type);
const namedFieldType = getNamedType(fieldType);
if (root && typeof root[fieldName] !== 'undefined') {
let result: any;
// if we're here, the field is already defined
if (typeof root[fieldName] === 'function') {
result = root[fieldName](root, args, context, info);
if (result instanceof MockList) {
result = result.mock(
root,
args,
context,
info,
fieldType as GraphQLList<any>,
mockType,
);
}
} else {
result = root[fieldName];
}
// Now we merge the result with the default mock for this type.
// This allows overriding defaults while writing very little code.
if (mockFunctionMap.has(namedFieldType.name)) {
result = mergeMocks(
mockFunctionMap
.get(namedFieldType.name)
.bind(null, root, args, context, info),
result,
);
}
return result;
}
if (fieldType instanceof GraphQLList) {
return [
mockType(fieldType.ofType)(root, args, context, info),
mockType(fieldType.ofType)(root, args, context, info),
];
}
if (
mockFunctionMap.has(fieldType.name) &&
!(
fieldType instanceof GraphQLUnionType ||
fieldType instanceof GraphQLInterfaceType
)
) {
// the object passed doesn't have this field, so we apply the default mock
return mockFunctionMap.get(fieldType.name)(root, args, context, info);
}
if (fieldType instanceof GraphQLObjectType) {
// objects don't return actual data, we only need to mock scalars!
return {};
}
// if a mock function is provided for unionType or interfaceType, execute it to resolve the concrete type
// otherwise randomly pick a type from all implementation types
if (
fieldType instanceof GraphQLUnionType ||
fieldType instanceof GraphQLInterfaceType
) {
let implementationType;
if (mockFunctionMap.has(fieldType.name)) {
const interfaceMockObj = mockFunctionMap.get(fieldType.name)(
root,
args,
context,
info,
);
if (!interfaceMockObj || !interfaceMockObj.__typename) {
return Error(`Please return a __typename in "${fieldType.name}"`);
}
implementationType = schema.getType(interfaceMockObj.__typename);
} else {
const possibleTypes = schema.getPossibleTypes(fieldType);
implementationType = getRandomElement(possibleTypes);
}
return Object.assign(
{ __typename: implementationType },
mockType(implementationType)(root, args, context, info),
);
}
if (fieldType instanceof GraphQLEnumType) {
return getRandomElement(fieldType.getValues()).value;
}
if (defaultMockMap.has(fieldType.name)) {
return defaultMockMap.get(fieldType.name)(root, args, context, info);
}
// if we get to here, we don't have a value, and we don't have a mock for this type,
//.........这里部分代码省略.........