import { isType, GraphQLType, GraphQLList, GraphQLNonNull } from 'graphql';
import { Thunk } from 'services/types';
import {
objectTypeRegistry,
compileObjectType,
inputObjectTypeRegistry,
compileInputObjectType,
} from 'domains';
import { enumsRegistry, unionRegistry } from 'domains';
import { parseNativeTypeToGraphQL, isParsableScalar } from './parseNative';
export function resolveType(input: any, allowThunk = true): GraphQLType {
if (isType(input)) {
return input;
}
if (isParsableScalar(input)) {
return parseNativeTypeToGraphQL(input);
}
if (Array.isArray(input)) {
return resolveListType(input);
}
if (enumsRegistry.has(input)) {
return enumsRegistry.get(input);
}
if (unionRegistry.has(input)) {
return unionRegistry.get(input)();
}
if (objectTypeRegistry.has(input)) {
return compileObjectType(input);
}
I
if (inputObjectTypeRegistry.has(input)) {
return compileInputObjectType(input);
}
ifI (!allowThunk || typeof input !== 'function') {
return;
}
return resolveType(input(), false);
}
E
function resolveListType(input: any[]): GraphQLType {
if (input.length !== 1) {
return;
}
const [itemType] = input;
const resolvedItemType = resolveType(itemType);
if (!resolvedItemType) {
return;
}
return new GraphQLList(new GraphQLNonNull(resolvedItemType));
}
export function resolveTypesList(types: Thunk<any[]>): GraphQLType[] {
if (Array.isArray(types)) {
return types.map(type => {
return resolveType(type);
});
}
return types().map(type => {
return resolveType(type);
});
}
|