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

Fixes #1617: Federation Enum not parsing value 0 #1618

Merged
merged 3 commits into from
Dec 5, 2019
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- `apollo-env`
- <First `apollo-env` related entry goes here>
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- [#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)
- `apollo-language-server`
- <First `apollo-language-server` related entry goes here>
- `apollo-tools`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,5 +503,52 @@ type MutationRoot {
expect(colorEnum.getValue("RED")!.value).toBe("#f00");
expect(mockResolver).toBeCalledWith(undefined, { borderColor: "#f00" });
});

it(`should add resolvers to enum types with 0 value`, () => {
const typeDefs = gql`
enum CustomerType {
EXISTING
NEW
}

type Query {
existingCustomer: CustomerType
newCustomer: CustomerType
}
`;

const resolvers = {
CustomerType: {
EXISTING: 0,
NEW: 1
},
Query: {
existingCustomer: () => 0,
newCustomer: () => 1
}
};

const schema = buildSchemaFromSDL([{ typeDefs, resolvers }]);
const customerTypeEnum = schema.getType(
"CustomerType"
) as GraphQLEnumType;

let result = execute(
schema,
gql`
query {
existingCustomer
newCustomer
}
`
);

expect((result as ExecutionResult).data!.existingCustomer).toBe(
"EXISTING"
);
expect(customerTypeEnum.getValue("EXISTING")!.value).toBe(0);
expect((result as ExecutionResult).data!.newCustomer).toBe("NEW");
expect(customerTypeEnum.getValue("NEW")!.value).toBe(1);
});
});
});
5 changes: 4 additions & 1 deletion packages/apollo-graphql/src/schema/buildSchemaFromSDL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ export function addResolversToSchema(
const values = type.getValues();
const newValues: { [key: string]: GraphQLEnumValue } = {};
values.forEach(value => {
const newValue = (fieldConfigs as any)[value.name] || value.name;
let newValue = (fieldConfigs as any)[value.name];
if (newValue === undefined) {
newValue = value.name;
}
newValues[value.name] = {
value: newValue,
deprecationReason: value.deprecationReason,
Expand Down