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 #962, Enum internal value mapping #973

Merged
merged 1 commit into from
Oct 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

### vNEXT

* Fix [regression](https://github.com/apollographql/graphql-tools/issues/962) in enum internal value mapping.

### 4.0.0

* Support `graphql` and `@types/graphql` 14.x. <br />
Expand Down
15 changes: 6 additions & 9 deletions src/generate/addResolveFunctionsToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function addResolveFunctionsToSchema(
if (resolverType !== 'object' && resolverType !== 'function') {
throw new SchemaError(
`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". A resolver's value ` +
`must be of type object or function.`,
`must be of type object or function.`,
);
}

Expand Down Expand Up @@ -109,10 +109,8 @@ function addResolveFunctionsToSchema(
// its resolver provided internal value. This map is used to transform
// the current schema to a new schema that includes enums with the new
// internal value.
enumValueMap[type.name] = {
[fieldName]: resolverValue[fieldName],
};

enumValueMap[type.name] = enumValueMap[type.name] || {};
enumValueMap[type.name][fieldName] = resolverValue[fieldName];
return;
}

Expand Down Expand Up @@ -158,10 +156,9 @@ function addResolveFunctionsToSchema(
// If there are any enum resolver functions (that are used to return
// internal enum values), create a new schema that includes enums with the
// new internal facing values.
const updatedSchema = applySchemaTransforms(
schema,
[new ConvertEnumValues(enumValueMap)],
);
const updatedSchema = applySchemaTransforms(schema, [
new ConvertEnumValues(enumValueMap),
]);

return updatedSchema;
}
Expand Down
58 changes: 37 additions & 21 deletions src/test/testSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ describe('generating schema from shorthand', () => {
const shorthand = `
enum Color {
RED
BLUE
}

enum NumericEnum {
Expand All @@ -997,27 +998,33 @@ describe('generating schema from shorthand', () => {
}

type Query {
color: Color
redColor: Color
blueColor: Color
numericEnum: NumericEnum
}
`;

const testQuery = `{
color
redColor
blueColor
numericEnum
}`;

const resolveFunctions = {
Color: {
RED: '#EA3232',
BLUE: '#0000FF',
},
NumericEnum: {
TEST: 1,
},
Query: {
color() {
redColor() {
return '#EA3232';
},
blueColor() {
return '#0000FF';
},
numericEnum() {
return 1;
},
Expand All @@ -1031,7 +1038,8 @@ describe('generating schema from shorthand', () => {

const resultPromise = graphql(jsSchema, testQuery);
return resultPromise.then(result => {
assert.equal(result.data['color'], 'RED');
assert.equal(result.data['redColor'], 'RED');
assert.equal(result.data['blueColor'], 'BLUE');
assert.equal(result.data['numericEnum'], 'TEST');
assert.equal(result.errors, undefined);
});
Expand Down Expand Up @@ -1306,14 +1314,14 @@ describe('generating schema from shorthand', () => {
`;

const rf = {
Searchable: undefined
Searchable: undefined,
} as any;

expect(() =>
makeExecutableSchema({ typeDefs: short, resolvers: rf }),
).to.throw(
`"Searchable" defined in resolvers, but has invalid value "undefined". A resolver's value ` +
`must be of type object or function.`
`must be of type object or function.`,
);
});

Expand Down Expand Up @@ -1384,7 +1392,9 @@ describe('generating schema from shorthand', () => {

expect(() =>
makeExecutableSchema({ typeDefs: short, resolvers: rf }),
).to.throw(`Color.NO_RESOLVER was defined in resolvers, but enum is not in schema`);
).to.throw(
`Color.NO_RESOLVER was defined in resolvers, but enum is not in schema`,
);

expect(() =>
makeExecutableSchema({
Expand Down Expand Up @@ -2585,27 +2595,30 @@ describe('interface resolver inheritance', () => {
id: ({ id }: { id: number }) => `Node:${id}`,
},
User: {
name: ({ name }: { name: string}) => `User:${name}`
name: ({ name }: { name: string }) => `User:${name}`,
},
Query: {
user: () => user
}
user: () => user,
},
};
const schema = makeExecutableSchema({
typeDefs: testSchemaWithInterfaceResolvers,
resolvers,
inheritResolversFromInterfaces: true,
resolverValidationOptions: { requireResolversForAllFields: true, requireResolversForResolveType: true }
resolverValidationOptions: {
requireResolversForAllFields: true,
requireResolversForResolveType: true,
},
});
const query = `{ user { id name } }`;
const response = await graphql(schema, query);
assert.deepEqual(response, {
data: {
user: {
id: `Node:1`,
name: `User:Ada`
}
}
name: `User:Ada`,
},
},
});
});

Expand Down Expand Up @@ -2644,33 +2657,36 @@ describe('interface resolver inheritance', () => {
Person: {
__resolveType: ({ type }: { type: string }) => type,
id: ({ id }: { id: number }) => `Person:${id}`,
name: ({ name }: { name: string}) => `Person:${name}`
name: ({ name }: { name: string }) => `Person:${name}`,
},
Query: {
cyborg: () => cyborg,
replicant: () => replicant,
}
},
};
const schema = makeExecutableSchema({
parseOptions: { allowLegacySDLImplementsInterfaces: true },
typeDefs: testSchemaWithInterfaceResolvers,
resolvers,
inheritResolversFromInterfaces: true,
resolverValidationOptions: { requireResolversForAllFields: true, requireResolversForResolveType: true }
resolverValidationOptions: {
requireResolversForAllFields: true,
requireResolversForResolveType: true,
},
});
const query = `{ cyborg { id name } replicant { id name }}`;
const response = await graphql(schema, query);
assert.deepEqual(response, {
data: {
cyborg: {
id: `Node:1`,
name: `Person:Alex Murphy`
name: `Person:Alex Murphy`,
},
replicant: {
id: `Person:2`,
name: `Person:Rachael Tyrell`
}
}
name: `Person:Rachael Tyrell`,
},
},
});
});
});
Expand Down