Skip to content

Fix the GraphQL enum resolving type on schema merged #601

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

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### vNEXT

* ...
* Fix `delegateToSchema.js` to accept and move forward args with zero or false values [PR #586](https://github.com/apollographql/graphql-tools/pull/586)

### v2.18.0

Expand Down
19 changes: 7 additions & 12 deletions src/stitching/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,15 @@ export default async function delegateToSchema(
if (
operationDefinition &&
operationDefinition.kind === Kind.OPERATION_DEFINITION &&
operationDefinition.variableDefinitions
operationDefinition.variableDefinitions &&
Array.isArray(operationDefinition.variableDefinitions)
) {
operationDefinition.variableDefinitions.forEach(definition => {
for (const definition of operationDefinition.variableDefinitions) {
const key = definition.variable.name.value;
// (XXX) This is kinda hacky
let actualKey = key;
if (actualKey.startsWith('_')) {
actualKey = actualKey.slice(1);
}
const value = args[actualKey] || args[key] || info.variableValues[key];
variableValues[key] = value;
});
const actualKey = key.startsWith('_') ? key.slice(1) : key;
variableValues[key] = args[actualKey] != null ? args[actualKey] : info.variableValues[key];
}
}

if (operation === 'query' || operation === 'mutation') {
Expand Down Expand Up @@ -187,12 +184,10 @@ export function createDocument(
selectionSet,
};

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

return newDoc;
}

function processRootField(
Expand Down
66 changes: 51 additions & 15 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 @@ -248,6 +255,7 @@ testCombinations.forEach(async combination => {
linkSchema,
loneExtend,
localSubscriptionSchema,
localEnumSchema,
],
resolvers: {
TestScalar: new GraphQLScalarType({
Expand Down Expand Up @@ -1143,21 +1151,21 @@ bookingById(id: "b1") {
describe('variables', () => {
it('basic', async () => {
const propertyFragment = `
propertyById(id: $p1) {
id
name
}
`;
propertyById(id: $p1) {
id
name
}
`;
const bookingFragment = `
bookingById(id: $b1) {
id
customer {
name
}
startTime
endTime
}
`;
bookingById(id: $b1) {
id
customer {
name
}
startTime
endTime
}
`;

const propertyResult = await graphql(
propertySchema,
Expand Down Expand Up @@ -1248,6 +1256,34 @@ bookingById(id: $b1) {
},
});
});

it('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: {
fieldA: 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
Loading