Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildClientSchema: small code refactoring #2391

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 45 additions & 52 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
} from '../type/schema';
import {
type GraphQLType,
type GraphQLInputType,
type GraphQLOutputType,
type GraphQLNamedType,
isInputType,
isOutputType,
Expand Down Expand Up @@ -47,8 +45,6 @@ import {
type IntrospectionEnumType,
type IntrospectionInputObjectType,
type IntrospectionTypeRef,
type IntrospectionInputTypeRef,
type IntrospectionOutputTypeRef,
type IntrospectionNamedTypeRef,
} from './getIntrospectionQuery';

Expand Down Expand Up @@ -139,13 +135,17 @@ export function buildClientSchema(
const nullableType = getType(nullableRef);
return GraphQLNonNull(assertNullableType(nullableType));
}
if (!typeRef.name) {
return getNamedType(typeRef);
}

function getNamedType(
typeRef: IntrospectionNamedTypeRef<>,
): GraphQLNamedType {
const typeName = typeRef.name;
if (!typeName) {
throw new Error(`Unknown type reference: ${inspect(typeRef)}.`);
}
return getNamedType(typeRef.name);
}

function getNamedType(typeName: string): GraphQLNamedType {
const type = typeMap[typeName];
if (!type) {
throw new Error(
Expand All @@ -156,42 +156,16 @@ export function buildClientSchema(
return type;
}

function getInputType(typeRef: IntrospectionInputTypeRef): GraphQLInputType {
const type = getType(typeRef);
if (isInputType(type)) {
return type;
}
const typeStr = inspect(type);
throw new Error(
`Introspection must provide input type for arguments, but received: ${typeStr}.`,
);
}

function getOutputType(
typeRef: IntrospectionOutputTypeRef,
): GraphQLOutputType {
const type = getType(typeRef);
if (isOutputType(type)) {
return type;
}
const typeStr = inspect(type);
throw new Error(
`Introspection must provide output type for fields, but received: ${typeStr}.`,
);
}

function getObjectType(
typeRef: IntrospectionNamedTypeRef<IntrospectionObjectType>,
): GraphQLObjectType {
const type = getType(typeRef);
return assertObjectType(type);
return assertObjectType(getNamedType(typeRef));
}

function getInterfaceType(
typeRef: IntrospectionTypeRef,
typeRef: IntrospectionNamedTypeRef<IntrospectionInterfaceType>,
): GraphQLInterfaceType {
const type = getType(typeRef);
return assertInterfaceType(type);
return assertInterfaceType(getNamedType(typeRef));
}

// Given a type's introspection result, construct the correct
Expand Down Expand Up @@ -335,26 +309,38 @@ export function buildClientSchema(
`Introspection result missing fields: ${inspect(typeIntrospection)}.`,
);
}

return keyValMap(
typeIntrospection.fields,
fieldIntrospection => fieldIntrospection.name,
fieldIntrospection => {
if (!fieldIntrospection.args) {
const fieldIntrospectionStr = inspect(fieldIntrospection);
throw new Error(
`Introspection result missing field args: ${fieldIntrospectionStr}.`,
);
}
return {
description: fieldIntrospection.description,
deprecationReason: fieldIntrospection.deprecationReason,
type: getOutputType(fieldIntrospection.type),
args: buildInputValueDefMap(fieldIntrospection.args),
};
},
buildField,
);
}

function buildField(fieldIntrospection) {
const type = getType(fieldIntrospection.type);
if (!isOutputType(type)) {
const typeStr = inspect(type);
throw new Error(
`Introspection must provide output type for fields, but received: ${typeStr}.`,
);
}

if (!fieldIntrospection.args) {
const fieldIntrospectionStr = inspect(fieldIntrospection);
throw new Error(
`Introspection result missing field args: ${fieldIntrospectionStr}.`,
);
}

return {
description: fieldIntrospection.description,
deprecationReason: fieldIntrospection.deprecationReason,
type,
args: buildInputValueDefMap(fieldIntrospection.args),
};
}

function buildInputValueDefMap(inputValueIntrospections) {
return keyValMap(
inputValueIntrospections,
Expand All @@ -364,7 +350,14 @@ export function buildClientSchema(
}

function buildInputValue(inputValueIntrospection) {
const type = getInputType(inputValueIntrospection.type);
const type = getType(inputValueIntrospection.type);
if (!isInputType(type)) {
const typeStr = inspect(type);
throw new Error(
`Introspection must provide input type for arguments, but received: ${typeStr}.`,
);
}

const defaultValue =
inputValueIntrospection.defaultValue != null
? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type)
Expand Down