Skip to content

Commit

Permalink
Another attempt at reversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
abernix committed May 21, 2019
1 parent f289555 commit 4c86710
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* @flow strict
*/

import objectValues from '../polyfills/objectValues';
import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';
import keyMap from '../jsutils/keyMap';
import keyValMap from '../jsutils/keyValMap';
import { valueFromAST } from './valueFromAST';
import { parseValue } from '../language/parser';
Expand Down Expand Up @@ -84,18 +84,25 @@ export function buildClientSchema(
// Get the schema from the introspection result.
const schemaIntrospection = introspection.__schema;

// Iterate through all types, getting the type definition for each.
const typeMap = keyValMap(
// Converts the list of types into a keyMap based on the type names.
const typeIntrospectionMap = keyMap(
schemaIntrospection.types,
typeIntrospection => typeIntrospection.name,
typeIntrospection => buildType(typeIntrospection),
type => type.name,
);

for (const stdType of [...specifiedScalarTypes, ...introspectionTypes]) {
if (typeMap[stdType.name]) {
typeMap[stdType.name] = stdType;
}
}
// A cache to use to store the actual GraphQLType definition objects by name.
// Initialize to the GraphQL built in scalars. All functions below are inline
// so that this type def cache is within the scope of the closure.
const typeDefCache = keyMap(
specifiedScalarTypes.concat(introspectionTypes),
type => type.name,
);

// Iterate through all types, getting the type definition for each, ensuring
// that any type not directly referenced by a field will get created.
const types = schemaIntrospection.types.map(typeIntrospection =>
getNamedType(typeIntrospection.name),
);

// Get the root Query, Mutation, and Subscription types.
const queryType = schemaIntrospection.queryType
Expand All @@ -121,7 +128,7 @@ export function buildClientSchema(
query: queryType,
mutation: mutationType,
subscription: subscriptionType,
types: objectValues(typeMap),
types,
directives,
assumeValid: options && options.assumeValid,
allowedLegacyNames: options && options.allowedLegacyNames,
Expand Down Expand Up @@ -152,16 +159,20 @@ export function buildClientSchema(
}

function getNamedType(typeName: string): GraphQLNamedType {
const type = typeMap[typeName];
if (!type) {
if (typeDefCache[typeName]) {
return typeDefCache[typeName];
}
const typeIntrospection = typeIntrospectionMap[typeName];
if (!typeIntrospection) {
throw new Error(
`Invalid or incomplete schema, unknown type: ${typeName}. Ensure ` +
'that a full introspection query is used in order to build a ' +
'client schema.',
);
}

return type;
const typeDef = buildType(typeIntrospection);
typeDefCache[typeName] = typeDef;
return typeDef;
}

function getInputType(typeRef: IntrospectionInputTypeRef): GraphQLInputType {
Expand Down

0 comments on commit 4c86710

Please sign in to comment.