Skip to content

Commit 82ea9ca

Browse files
buildClientSchema: small code refactoring (#2391)
1 parent 23b427c commit 82ea9ca

File tree

1 file changed

+45
-52
lines changed

1 file changed

+45
-52
lines changed

src/utilities/buildClientSchema.js

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import {
1818
} from '../type/schema';
1919
import {
2020
type GraphQLType,
21-
type GraphQLInputType,
22-
type GraphQLOutputType,
2321
type GraphQLNamedType,
2422
isInputType,
2523
isOutputType,
@@ -47,8 +45,6 @@ import {
4745
type IntrospectionEnumType,
4846
type IntrospectionInputObjectType,
4947
type IntrospectionTypeRef,
50-
type IntrospectionInputTypeRef,
51-
type IntrospectionOutputTypeRef,
5248
type IntrospectionNamedTypeRef,
5349
} from './getIntrospectionQuery';
5450

@@ -139,13 +135,17 @@ export function buildClientSchema(
139135
const nullableType = getType(nullableRef);
140136
return GraphQLNonNull(assertNullableType(nullableType));
141137
}
142-
if (!typeRef.name) {
138+
return getNamedType(typeRef);
139+
}
140+
141+
function getNamedType(
142+
typeRef: IntrospectionNamedTypeRef<>,
143+
): GraphQLNamedType {
144+
const typeName = typeRef.name;
145+
if (!typeName) {
143146
throw new Error(`Unknown type reference: ${inspect(typeRef)}.`);
144147
}
145-
return getNamedType(typeRef.name);
146-
}
147148

148-
function getNamedType(typeName: string): GraphQLNamedType {
149149
const type = typeMap[typeName];
150150
if (!type) {
151151
throw new Error(
@@ -156,42 +156,16 @@ export function buildClientSchema(
156156
return type;
157157
}
158158

159-
function getInputType(typeRef: IntrospectionInputTypeRef): GraphQLInputType {
160-
const type = getType(typeRef);
161-
if (isInputType(type)) {
162-
return type;
163-
}
164-
const typeStr = inspect(type);
165-
throw new Error(
166-
`Introspection must provide input type for arguments, but received: ${typeStr}.`,
167-
);
168-
}
169-
170-
function getOutputType(
171-
typeRef: IntrospectionOutputTypeRef,
172-
): GraphQLOutputType {
173-
const type = getType(typeRef);
174-
if (isOutputType(type)) {
175-
return type;
176-
}
177-
const typeStr = inspect(type);
178-
throw new Error(
179-
`Introspection must provide output type for fields, but received: ${typeStr}.`,
180-
);
181-
}
182-
183159
function getObjectType(
184160
typeRef: IntrospectionNamedTypeRef<IntrospectionObjectType>,
185161
): GraphQLObjectType {
186-
const type = getType(typeRef);
187-
return assertObjectType(type);
162+
return assertObjectType(getNamedType(typeRef));
188163
}
189164

190165
function getInterfaceType(
191-
typeRef: IntrospectionTypeRef,
166+
typeRef: IntrospectionNamedTypeRef<IntrospectionInterfaceType>,
192167
): GraphQLInterfaceType {
193-
const type = getType(typeRef);
194-
return assertInterfaceType(type);
168+
return assertInterfaceType(getNamedType(typeRef));
195169
}
196170

197171
// Given a type's introspection result, construct the correct
@@ -335,26 +309,38 @@ export function buildClientSchema(
335309
`Introspection result missing fields: ${inspect(typeIntrospection)}.`,
336310
);
337311
}
312+
338313
return keyValMap(
339314
typeIntrospection.fields,
340315
fieldIntrospection => fieldIntrospection.name,
341-
fieldIntrospection => {
342-
if (!fieldIntrospection.args) {
343-
const fieldIntrospectionStr = inspect(fieldIntrospection);
344-
throw new Error(
345-
`Introspection result missing field args: ${fieldIntrospectionStr}.`,
346-
);
347-
}
348-
return {
349-
description: fieldIntrospection.description,
350-
deprecationReason: fieldIntrospection.deprecationReason,
351-
type: getOutputType(fieldIntrospection.type),
352-
args: buildInputValueDefMap(fieldIntrospection.args),
353-
};
354-
},
316+
buildField,
355317
);
356318
}
357319

320+
function buildField(fieldIntrospection) {
321+
const type = getType(fieldIntrospection.type);
322+
if (!isOutputType(type)) {
323+
const typeStr = inspect(type);
324+
throw new Error(
325+
`Introspection must provide output type for fields, but received: ${typeStr}.`,
326+
);
327+
}
328+
329+
if (!fieldIntrospection.args) {
330+
const fieldIntrospectionStr = inspect(fieldIntrospection);
331+
throw new Error(
332+
`Introspection result missing field args: ${fieldIntrospectionStr}.`,
333+
);
334+
}
335+
336+
return {
337+
description: fieldIntrospection.description,
338+
deprecationReason: fieldIntrospection.deprecationReason,
339+
type,
340+
args: buildInputValueDefMap(fieldIntrospection.args),
341+
};
342+
}
343+
358344
function buildInputValueDefMap(inputValueIntrospections) {
359345
return keyValMap(
360346
inputValueIntrospections,
@@ -364,7 +350,14 @@ export function buildClientSchema(
364350
}
365351

366352
function buildInputValue(inputValueIntrospection) {
367-
const type = getInputType(inputValueIntrospection.type);
353+
const type = getType(inputValueIntrospection.type);
354+
if (!isInputType(type)) {
355+
const typeStr = inspect(type);
356+
throw new Error(
357+
`Introspection must provide input type for arguments, but received: ${typeStr}.`,
358+
);
359+
}
360+
368361
const defaultValue =
369362
inputValueIntrospection.defaultValue != null
370363
? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type)

0 commit comments

Comments
 (0)