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

Fix the arguments of GraphQL enum types resolves to merged schema #604

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
*.tgz
.DS_Store
package-lock.json
.idea
4 changes: 1 addition & 3 deletions src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,10 @@ export function createDocument(
selectionSet,
};

const newDoc: DocumentNode = {
return {
kind: Kind.DOCUMENT,
definitions: [operationDefinition, ...processedFragments],
};

return newDoc;
}

function processRootField(
Expand Down
19 changes: 18 additions & 1 deletion src/stitching/makeRemoteExecutableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GraphQLBoolean,
GraphQLInt,
GraphQLScalarType,
GraphQLEnumType,
ExecutionResult,
print,
buildSchema,
Expand Down Expand Up @@ -213,7 +214,7 @@ function createSubscriptionResolver(
};
}

function createPassThroughScalar({
export function createPassThroughScalar({
name,
description,
}: {
Expand All @@ -235,6 +236,22 @@ function createPassThroughScalar({
});
}

export function createPassThroughEnum(type: GraphQLEnumType) {
const values = type.getValues();
const newValues = {};

Object.keys(values).forEach(key => {
newValues[key] = values[key];
});

return new GraphQLEnumType({
name: type.name,
description: type.description,
astNode: type.astNode,
values: newValues,
});
}

function parseLiteral(ast: ValueNode): any {
switch (ast.kind) {
case Kind.STRING:
Expand Down
8 changes: 8 additions & 0 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GraphQLObjectType,
GraphQLResolveInfo,
GraphQLScalarType,
GraphQLEnumType,
GraphQLSchema,
GraphQLType,
buildASTSchema,
Expand All @@ -16,6 +17,7 @@ import {
isNamedType,
parse,
} from 'graphql';

import TypeRegistry from './TypeRegistry';
import { IResolvers, MergeInfo, IFieldResolver } from '../Interfaces';
import isEmptyObject from '../isEmptyObject';
Expand All @@ -29,6 +31,7 @@ import {
} from './schemaRecreation';
import delegateToSchema from './delegateToSchema';
import typeFromAST from './typeFromAST';
import { createPassThroughScalar, createPassThroughEnum } from './makeRemoteExecutableSchema';

const backcompatOptions = { commentDescriptions: true };

Expand Down Expand Up @@ -103,9 +106,14 @@ export default function mergeSchemas({
let newType;
if (isCompositeType(type) || type instanceof GraphQLInputObjectType) {
newType = recreateCompositeType(schema, type, typeRegistry);
} else if (type instanceof GraphQLEnumType) {
newType = createPassThroughEnum(type);
} else if (type instanceof GraphQLScalarType) {
newType = createPassThroughScalar(type);
} else {
newType = getNamedType(type);
}

if (newType instanceof GraphQLObjectType) {
delete newType.isTypeOf;
}
Expand Down
40 changes: 37 additions & 3 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@ import {
parse,
ExecutionResult,
} from 'graphql';

import { forAwaitEach } from 'iterall';

import mergeSchemas from '../stitching/mergeSchemas';

import {
propertySchema as localPropertySchema,
productSchema as localProductSchema,
bookingSchema as localBookingSchema,
subscriptionSchema as localSubscriptionSchema,
enumSchema as localEnumSchema,
resolveEnumType,
ENUM,
remoteBookingSchema,
remotePropertySchema,
remoteProductSchema,
subscriptionPubSub,
subscriptionPubSubTrigger,
} from './testingSchemas';
import { forAwaitEach } from 'iterall';

import { makeExecutableSchema } from '../schemaGenerator';

const testCombinations = [
Expand Down Expand Up @@ -227,7 +234,7 @@ if (process.env.GRAPHQL_VERSION === '^0.11') {
}

testCombinations.forEach(async combination => {
describe('merging ' + combination.name, () => {
describe.only('merging ' + combination.name, () => {
let mergedSchema: GraphQLSchema,
propertySchema: GraphQLSchema,
productSchema: GraphQLSchema,
Expand All @@ -248,6 +255,7 @@ testCombinations.forEach(async combination => {
linkSchema,
loneExtend,
localSubscriptionSchema,
localEnumSchema,
],
resolvers: {
TestScalar: new GraphQLScalarType({
Expand Down Expand Up @@ -1140,7 +1148,7 @@ bookingById(id: "b1") {
});
});

describe('variables', () => {
describe.only('variables', () => {
it('basic', async () => {
const propertyFragment = `
propertyById(id: $p1) {
Expand Down Expand Up @@ -1248,6 +1256,32 @@ bookingById(id: "b1") {
},
});
});

it.only('with enum args', async () => {
const enumArg = 'VALUE_2';
const mergedResult = await graphql(
mergedSchema,
// language=GraphQL
`
query ($enumArg: EnumArgument!) {
enumType (enumArg: $enumArg) {
fieldA
}
}
`,
{},
{},
{
enumArg,
},
);

expect(mergedResult).to.deep.equal({
data: {
enumType: resolveEnumType(ENUM[enumArg]),
},
});
});
});

describe('aliases', () => {
Expand Down
61 changes: 58 additions & 3 deletions src/test/testingSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
GraphQLScalarType,
ValueNode,
ExecutionResult,
GraphQLObjectType,
GraphQLEnumType,
GraphQLString,
GraphQLNonNull,
} from 'graphql';

import { ApolloLink, Observable } from 'apollo-link';
import { makeExecutableSchema } from '../schemaGenerator';
import { IResolvers } from '../Interfaces';
import makeRemoteExecutableSchema, {
Fetcher,
} from '../stitching/makeRemoteExecutableSchema';
import makeRemoteExecutableSchema, { Fetcher } from '../stitching/makeRemoteExecutableSchema';
import introspectSchema from '../stitching/introspectSchema';
import { PubSub } from 'graphql-subscriptions';

Expand Down Expand Up @@ -613,6 +616,58 @@ const subscriptionResolvers: IResolvers = {
},
};

export const ENUM = {
VALUE_1: 1,
VALUE_2: 2,
};

const enumSchemaEnumType = new GraphQLEnumType({
name: 'EnumArgument',
values: {
VALUE_1: {
value: ENUM.VALUE_1,
},
VALUE_2: {
value: ENUM.VALUE_2,
},
},
});

const enumSchemaEnum = new GraphQLObjectType({
name: 'EnumType',
fields: () => ({
fieldA: {
type: GraphQLString,
resolve: parent => parent.fieldA,
},
}),
});

export const resolveEnumType = (enumArg: number) => {
return {
fieldA: `The enum arg was ${enumArg}`,
};
};

const enumSchemaQuery = new GraphQLObjectType({
name: 'Query',
fields: () => ({
enumType: {
type: enumSchemaEnum,
args: {
enumArg: {
type: new GraphQLNonNull(enumSchemaEnumType),
},
},
resolve: (root, args) => resolveEnumType(args.enumArg),
},
}),
});

export const enumSchema: GraphQLSchema = new GraphQLSchema({
query: enumSchemaQuery,
});

export const propertySchema: GraphQLSchema = makeExecutableSchema({
typeDefs: propertyAddressTypeDefs,
resolvers: propertyResolvers,
Expand Down