Skip to content

Commit

Permalink
Simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Dec 11, 2017
1 parent 98046f0 commit 98d1277
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-tools",
"version": "2.12.0-alpha.1",
"version": "2.12.0-alpha-2",
"description": "Useful tools to create and manipulate GraphQL schemas.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
35 changes: 5 additions & 30 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,18 @@ export interface IMockServer {
) => Promise<ExecutionResult>;
}

export enum SubSchemaKind {
Schema,
ExtensionString,
}

export type SubSchema =
| {
kind: SubSchemaKind.Schema;
name: string;
schema: GraphQLSchema;
}
| {
kind: SubSchemaKind.ExtensionString;
name: string;
typeDefs: string;
};

export type MergeTypeCandidate = {
schemaName: string;
schema?: GraphQLSchema;
type: GraphQLNamedType;
};

export enum VisitTypeResultKind {
Type,
Remove,
}
export type TypeWithResolvers = {
type: GraphQLNamedType;
resolvers?: IResolvers;
};

export type VisitTypeResult =
| {
kind: VisitTypeResultKind.Type;
type: GraphQLNamedType;
resolvers?: IResolvers;
}
| {
kind: VisitTypeResultKind.Remove;
};
export type VisitTypeResult = GraphQLNamedType | TypeWithResolvers | null;

export type VisitType = (
name: string,
Expand Down
41 changes: 24 additions & 17 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ import {
IResolvers,
MergeInfo,
IFieldResolver,
SubSchema,
VisitType,
MergeTypeCandidate,
SubSchemaKind,
TypeWithResolvers,
VisitTypeResult,
VisitTypeResultKind,
ResolveType,
} from '../Interfaces';
// import isEmptyObject from '../isEmptyObject';
Expand All @@ -49,7 +47,7 @@ export default function mergeSchemas({
visitType,
resolvers,
}: {
schemas: Array<SubSchema>;
schemas: Array<{ name: string; schema: string | GraphQLSchema }>;
visitType?: VisitType;
resolvers?: IResolvers | ((mergeInfo: MergeInfo) => IResolvers);
}): GraphQLSchema {
Expand Down Expand Up @@ -89,8 +87,8 @@ export default function mergeSchemas({
});
};

schemas.forEach((subSchema: SubSchema) => {
if (subSchema.kind === SubSchemaKind.Schema) {
schemas.forEach(subSchema => {
if (subSchema.schema instanceof GraphQLSchema) {
const schema = subSchema.schema;
allSchemas[subSchema.name] = schema;
const queryType = schema.getQueryType();
Expand Down Expand Up @@ -133,8 +131,8 @@ export default function mergeSchemas({
});
}
});
} else if (subSchema.kind === SubSchemaKind.ExtensionString) {
let parsedSchemaDocument = parse(subSchema.typeDefs);
} else if (typeof subSchema.schema === 'string') {
let parsedSchemaDocument = parse(subSchema.schema);
parsedSchemaDocument.definitions.forEach(def => {
const type = typeFromAST(def, createNamedStub);
if (type) {
Expand All @@ -151,6 +149,8 @@ export default function mergeSchemas({
if (extensionsDocument.definitions.length > 0) {
extensions.push(extensionsDocument);
}
} else {
throw new Error(`Invalid schema ${subSchema.name}`);
}
});

Expand All @@ -161,17 +161,28 @@ export default function mergeSchemas({
typeName,
typeCandidates[typeName],
);
if (resultType.kind === VisitTypeResultKind.Type) {
const type = resultType.type;
if (resultType === null) {
types[typeName] = null;
} else {
let type: GraphQLNamedType;
let typeResolvers: IResolvers;
if (isNamedType(<GraphQLNamedType>resultType)) {
type = <GraphQLNamedType>resultType;
} else if ((<TypeWithResolvers>resultType).type) {
type = (<TypeWithResolvers>resultType).type;
typeResolvers = (<TypeWithResolvers>resultType).resolvers;
} else {
throw new Error('Invalid `visitType` result for type "${typeName}"');
}
let newType: GraphQLType;
if (isCompositeType(type) || type instanceof GraphQLInputObjectType) {
newType = recreateCompositeType(type, resolveType);
} else {
newType = getNamedType(type);
}
types[typeName] = newType;
if (resultType.resolvers) {
generatedResolvers[typeName] = resultType.resolvers;
if (typeResolvers) {
generatedResolvers[typeName] = typeResolvers;
}
}
});
Expand Down Expand Up @@ -412,14 +423,10 @@ const defaultVisitType: VisitType = (
fields: fieldMapToFieldConfigMap(fields, resolveType),
});
return {
kind: VisitTypeResultKind.Type,
type,
resolvers,
};
} else {
return {
kind: VisitTypeResultKind.Type,
type: candidates[candidates.length - 1].type,
};
return candidates[candidates.length - 1].type;
}
};
13 changes: 3 additions & 10 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
parse,
ExecutionResult,
} from 'graphql';
import { SubSchemaKind } from '../Interfaces';
import mergeSchemas from '../stitching/mergeSchemas';
import {
propertySchema as localPropertySchema,
Expand Down Expand Up @@ -122,32 +121,26 @@ testCombinations.forEach(async combination => {
mergedSchema = mergeSchemas({
schemas: [
{
kind: SubSchemaKind.Schema,
name: 'Property',
schema: propertySchema,
},
{
kind: SubSchemaKind.Schema,
name: 'Booking',
schema: bookingSchema,
},
{
kind: SubSchemaKind.ExtensionString,
name: 'ScalarTest',
typeDefs: scalarTest,
schema: scalarTest,
},
{
kind: SubSchemaKind.ExtensionString,
name: 'EnumTest',
typeDefs: enumTest,
schema: enumTest,
},
{
kind: SubSchemaKind.ExtensionString,
name: 'LinkSchema',
typeDefs: linkSchema,
schema: linkSchema,
},
{
kind: SubSchemaKind.Schema,
name: 'LocalSubscription',
schema: localSubscriptionSchema,
},
Expand Down

0 comments on commit 98d1277

Please sign in to comment.