Skip to content

Commit 40ed19b

Browse files
ShadyKutzJakeDawkins
authored andcommitted
Fixes #1617: Federation Enum not parsing value 0 (#1618)
* Fixes #1617 Updated buildSchemaFromSDL to handle Enums with a value of 0. Previously, when an enum had a value of 0, the name would be used as the enum value.
1 parent 29d28e4 commit 40ed19b

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- `apollo-env`
1616
- <First `apollo-env` related entry goes here>
1717
- `apollo-graphql`
18-
- <First `apollo-graphql` related entry goes here>
18+
- [#1618] Fixes an issue when enums with a value of 0 fail to resolve when using a Federated Schema (https://github.com/apollographql/apollo-tooling/pull/1618)
1919
- `apollo-language-server`
2020
- <First `apollo-language-server` related entry goes here>
2121
- `apollo-tools`

packages/apollo-graphql/src/schema/__tests__/buildSchemaFromSDL.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,5 +503,52 @@ type MutationRoot {
503503
expect(colorEnum.getValue("RED")!.value).toBe("#f00");
504504
expect(mockResolver).toBeCalledWith(undefined, { borderColor: "#f00" });
505505
});
506+
507+
it(`should add resolvers to enum types with 0 value`, () => {
508+
const typeDefs = gql`
509+
enum CustomerType {
510+
EXISTING
511+
NEW
512+
}
513+
514+
type Query {
515+
existingCustomer: CustomerType
516+
newCustomer: CustomerType
517+
}
518+
`;
519+
520+
const resolvers = {
521+
CustomerType: {
522+
EXISTING: 0,
523+
NEW: 1
524+
},
525+
Query: {
526+
existingCustomer: () => 0,
527+
newCustomer: () => 1
528+
}
529+
};
530+
531+
const schema = buildSchemaFromSDL([{ typeDefs, resolvers }]);
532+
const customerTypeEnum = schema.getType(
533+
"CustomerType"
534+
) as GraphQLEnumType;
535+
536+
let result = execute(
537+
schema,
538+
gql`
539+
query {
540+
existingCustomer
541+
newCustomer
542+
}
543+
`
544+
);
545+
546+
expect((result as ExecutionResult).data!.existingCustomer).toBe(
547+
"EXISTING"
548+
);
549+
expect(customerTypeEnum.getValue("EXISTING")!.value).toBe(0);
550+
expect((result as ExecutionResult).data!.newCustomer).toBe("NEW");
551+
expect(customerTypeEnum.getValue("NEW")!.value).toBe(1);
552+
});
506553
});
507554
});

packages/apollo-graphql/src/schema/buildSchemaFromSDL.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ export function addResolversToSchema(
237237
const values = type.getValues();
238238
const newValues: { [key: string]: GraphQLEnumValue } = {};
239239
values.forEach(value => {
240-
const newValue = (fieldConfigs as any)[value.name] || value.name;
240+
let newValue = (fieldConfigs as any)[value.name];
241+
if (newValue === undefined) {
242+
newValue = value.name;
243+
}
241244
newValues[value.name] = {
242245
value: newValue,
243246
deprecationReason: value.deprecationReason,

0 commit comments

Comments
 (0)