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

Small non-breaking refactors #918

Merged
merged 3 commits into from Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 7 additions & 71 deletions src/stitching/mergeSchemas.ts
@@ -1,14 +1,11 @@
import {
DocumentNode,
GraphQLField,
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLNamedType,
GraphQLObjectType,
GraphQLResolveInfo,
GraphQLScalarType,
GraphQLSchema,
GraphQLString,
extendSchema,
getNamedType,
isNamedType,
Expand All @@ -21,7 +18,6 @@ import {
MergeInfo,
MergeTypeCandidate,
TypeWithResolvers,
VisitType,
VisitTypeResult,
IResolversParameter,
} from '../Interfaces';
Expand All @@ -35,7 +31,7 @@ import {
createResolveType,
} from './schemaRecreation';
import delegateToSchema from './delegateToSchema';
import typeFromAST, { GetType } from './typeFromAST';
import typeFromAST from './typeFromAST';
import {
Transform,
ExpandAbstractTypes,
Expand Down Expand Up @@ -70,25 +66,16 @@ export default function mergeSchemas({
schemaDirectives?: { [name: string]: typeof SchemaDirectiveVisitor };
inheritResolversFromInterfaces?: boolean;
}): GraphQLSchema {
let visitType: VisitType = defaultVisitType;
if (onTypeConflict) {
console.warn(
'`onTypeConflict` is deprecated. Use schema transforms to customize merging logic.',
);
visitType = createVisitTypeFromOnTypeConflict(onTypeConflict);
}
return mergeSchemasImplementation({ schemas, visitType, resolvers, schemaDirectives, inheritResolversFromInterfaces });
return mergeSchemasImplementation({ schemas, resolvers, schemaDirectives, inheritResolversFromInterfaces });
}

function mergeSchemasImplementation({
schemas,
visitType,
resolvers,
schemaDirectives,
inheritResolversFromInterfaces
}: {
schemas: Array<string | GraphQLSchema | Array<GraphQLNamedType>>;
visitType?: VisitType;
resolvers?: IResolversParameter;
schemaDirectives?: { [name: string]: typeof SchemaDirectiveVisitor };
inheritResolversFromInterfaces?: boolean;
Expand All @@ -102,36 +89,13 @@ function mergeSchemasImplementation({
fragment: string;
}> = [];

if (!visitType) {
visitType = defaultVisitType;
}

const resolveType = createResolveType(name => {
if (types[name] === undefined) {
throw new Error(`Can't find type ${name}.`);
}
return types[name];
});

const createNamedStub: GetType = (name, type) => {
let constructor: any;
if (type === 'object') {
constructor = GraphQLObjectType;
} else if (type === 'interface') {
constructor = GraphQLInterfaceType;
} else {
constructor = GraphQLInputObjectType;
}
return new constructor({
name,
fields: {
__fake: {
type: GraphQLString,
},
},
});
};

schemas.forEach(schema => {
if (schema instanceof GraphQLSchema) {
allSchemas.push(schema);
Expand Down Expand Up @@ -176,7 +140,7 @@ function mergeSchemasImplementation({
} else if (typeof schema === 'string') {
let parsedSchemaDocument = parse(schema);
parsedSchemaDocument.definitions.forEach(def => {
const type = typeFromAST(def, createNamedStub);
const type = typeFromAST(def);
if (type) {
addTypeCandidate(typeCandidates, type.name, {
type: type,
Expand Down Expand Up @@ -225,7 +189,7 @@ function mergeSchemasImplementation({
let generatedResolvers = {};

Object.keys(typeCandidates).forEach(typeName => {
const resultType: VisitTypeResult = visitType(
const resultType: VisitTypeResult = defaultVisitType(
typeName,
typeCandidates[typeName],
);
Expand Down Expand Up @@ -443,41 +407,13 @@ function addTypeCandidate(
typeCandidates[name].push(typeCandidate);
}

function createVisitTypeFromOnTypeConflict(
onTypeConflict: OnTypeConflict,
): VisitType {
return (name, candidates) =>
defaultVisitType(name, candidates, cands =>
cands.reduce((prev, next) => {
const type = onTypeConflict(prev.type, next.type, {
left: {
schema: prev.schema,
},
right: {
schema: next.schema,
},
});
if (prev.type === type) {
return prev;
} else if (next.type === type) {
return next;
} else {
return {
schemaName: 'unknown',
type,
};
}
}),
);
}

const defaultVisitType = (
function defaultVisitType(
name: string,
candidates: Array<MergeTypeCandidate>,
candidateSelector?: (
candidates: Array<MergeTypeCandidate>,
) => MergeTypeCandidate,
) => {
) {
if (!candidateSelector) {
candidateSelector = cands => cands[cands.length - 1];
}
Expand Down Expand Up @@ -526,4 +462,4 @@ const defaultVisitType = (
const candidate = candidateSelector(candidates);
return candidate.type;
}
};
}
72 changes: 44 additions & 28 deletions src/stitching/typeFromAST.ts
Expand Up @@ -22,7 +22,8 @@ import {
TypeNode,
UnionTypeDefinitionNode,
valueFromAST,
getDescription
getDescription,
GraphQLString
} from 'graphql';
import resolveFromParentType from './resolveFromParentTypename';

Expand All @@ -36,48 +37,45 @@ export type GetType = (

export default function typeFromAST(
node: DefinitionNode,
getType: GetType,
): GraphQLNamedType | null {
switch (node.kind) {
case Kind.OBJECT_TYPE_DEFINITION:
return makeObjectType(node, getType);
return makeObjectType(node);
case Kind.INTERFACE_TYPE_DEFINITION:
return makeInterfaceType(node, getType);
return makeInterfaceType(node);
case Kind.ENUM_TYPE_DEFINITION:
return makeEnumType(node, getType);
return makeEnumType(node);
case Kind.UNION_TYPE_DEFINITION:
return makeUnionType(node, getType);
return makeUnionType(node);
case Kind.SCALAR_TYPE_DEFINITION:
return makeScalarType(node, getType);
return makeScalarType(node);
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
return makeInputObjectType(node, getType);
return makeInputObjectType(node);
default:
return null;
}
}

function makeObjectType(
node: ObjectTypeDefinitionNode,
getType: GetType,
): GraphQLObjectType {
return new GraphQLObjectType({
name: node.name.value,
fields: () => makeFields(node.fields, getType),
fields: () => makeFields(node.fields),
interfaces: () =>
node.interfaces.map(
iface => getType(iface.name.value, 'interface') as GraphQLInterfaceType,
iface => createNamedStub(iface.name.value, 'interface') as GraphQLInterfaceType,
),
description: getDescription(node, backcompatOptions),
});
}

function makeInterfaceType(
node: InterfaceTypeDefinitionNode,
getType: GetType,
): GraphQLInterfaceType {
return new GraphQLInterfaceType({
name: node.name.value,
fields: () => makeFields(node.fields, getType),
fields: () => makeFields(node.fields),
description: getDescription(node, backcompatOptions),
resolveType: (parent, context, info) =>
resolveFromParentType(parent, info.schema),
Expand All @@ -86,7 +84,6 @@ function makeInterfaceType(

function makeEnumType(
node: EnumTypeDefinitionNode,
getType: GetType,
): GraphQLEnumType {
const values = {};
node.values.forEach(value => {
Expand All @@ -103,13 +100,12 @@ function makeEnumType(

function makeUnionType(
node: UnionTypeDefinitionNode,
getType: GetType,
): GraphQLUnionType {
return new GraphQLUnionType({
name: node.name.value,
types: () =>
node.types.map(
type => resolveType(type, getType, 'object') as GraphQLObjectType,
type => resolveType(type, 'object') as GraphQLObjectType,
),
description: getDescription(node, backcompatOptions),
resolveType: (parent, context, info) =>
Expand All @@ -119,7 +115,6 @@ function makeUnionType(

function makeScalarType(
node: ScalarTypeDefinitionNode,
getType: GetType,
): GraphQLScalarType {
return new GraphQLScalarType({
name: node.name.value,
Expand All @@ -136,31 +131,30 @@ function makeScalarType(

function makeInputObjectType(
node: InputObjectTypeDefinitionNode,
getType: GetType,
): GraphQLInputObjectType {
return new GraphQLInputObjectType({
name: node.name.value,
fields: () => makeValues(node.fields, getType),
fields: () => makeValues(node.fields),
description: getDescription(node, backcompatOptions),
});
}

function makeFields(nodes: Array<FieldDefinitionNode>, getType: GetType) {
function makeFields(nodes: Array<FieldDefinitionNode>) {
const result = {};
nodes.forEach(node => {
result[node.name.value] = {
type: resolveType(node.type, getType, 'object'),
args: makeValues(node.arguments, getType),
type: resolveType(node.type, 'object'),
args: makeValues(node.arguments),
description: getDescription(node, backcompatOptions),
};
});
return result;
}

function makeValues(nodes: Array<InputValueDefinitionNode>, getType: GetType) {
function makeValues(nodes: Array<InputValueDefinitionNode>) {
const result = {};
nodes.forEach(node => {
const type = resolveType(node.type, getType, 'input') as GraphQLInputType;
const type = resolveType(node.type, 'input') as GraphQLInputType;
result[node.name.value] = {
type,
defaultValue: valueFromAST(node.defaultValue, type),
Expand All @@ -172,15 +166,37 @@ function makeValues(nodes: Array<InputValueDefinitionNode>, getType: GetType) {

function resolveType(
node: TypeNode,
getType: GetType,
type: 'object' | 'interface' | 'input',
): GraphQLType {
switch (node.kind) {
case Kind.LIST_TYPE:
return new GraphQLList(resolveType(node.type, getType, type));
return new GraphQLList(resolveType(node.type, type));
case Kind.NON_NULL_TYPE:
return new GraphQLNonNull(resolveType(node.type, getType, type));
return new GraphQLNonNull(resolveType(node.type, type));
default:
return getType(node.name.value, type);
return createNamedStub(node.name.value, type);
}
}

function createNamedStub(
name: string,
type: 'object' | 'interface' | 'input'
): GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType {
let constructor: any;
if (type === 'object') {
constructor = GraphQLObjectType;
} else if (type === 'interface') {
constructor = GraphQLInterfaceType;
} else {
constructor = GraphQLInputObjectType;
}

return new constructor({
name,
fields: {
__fake: {
type: GraphQLString,
},
},
});
}