Skip to content

Commit

Permalink
Allow IEnumResolver values to be number type (#568)
Browse files Browse the repository at this point in the history
* Allow IEnumResolver values to be number type
  • Loading branch information
tgriesser authored and freiksenet committed Jan 9, 2018
1 parent 6177034 commit 608414b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
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

* ...
* IEnumResolver value can be a `number` type

### v2.17.0

Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type ITypeDefinitions = ITypedef | ITypedef[];
export type IResolverObject = {
[key: string]: IFieldResolver<any, any> | IResolverOptions;
};
export type IEnumResolver = { [key: string]: string };
export type IEnumResolver = { [key: string]: string | number };
export interface IResolvers {
[key: string]:
| (() => any)
Expand Down
33 changes: 33 additions & 0 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,20 @@ let enumTest = `
RED
}
"""
A type that uses an Enum with a numeric constant.
"""
enum NumericEnum {
TEST
}
schema {
query: Query
}
type Query {
color: Color
numericEnum: NumericEnum
}
`;

Expand Down Expand Up @@ -157,12 +165,18 @@ if (process.env.GRAPHQL_VERSION === '^0.11') {
RED
}
# A type that uses an Enum with a numeric constant.
enum NumericEnum {
TEST
}
schema {
query: Query
}
type Query {
color: Color
numericEnum: NumericEnum
}
`;

Expand Down Expand Up @@ -232,6 +246,9 @@ testCombinations.forEach(async combination => {
parseValue: value => value,
parseLiteral: () => null,
}),
NumericEnum: {
TEST: 1
},
Color: {
RED: '#EA3232',
},
Expand Down Expand Up @@ -287,6 +304,9 @@ testCombinations.forEach(async combination => {
color() {
return '#EA3232';
},
numericEnum() {
return 1;
},
delegateInterfaceTest(parent, args, context, info) {
return info.mergeInfo.delegate(
'query',
Expand Down Expand Up @@ -449,10 +469,16 @@ testCombinations.forEach(async combination => {
Color: {
RED: '#EA3232',
},
NumericEnum: {
TEST: 1
},
Query: {
color() {
return '#EA3232';
},
numericEnum() {
return 1;
},
},
},
});
Expand All @@ -461,6 +487,7 @@ testCombinations.forEach(async combination => {
`
query {
color
numericEnum
}
`,
);
Expand All @@ -470,13 +497,15 @@ testCombinations.forEach(async combination => {
`
query {
color
numericEnum
}
`,
);

expect(enumResult).to.deep.equal({
data: {
color: 'RED',
numericEnum: 'TEST'
},
});
expect(mergedResult).to.deep.equal(enumResult);
Expand Down Expand Up @@ -1556,6 +1585,10 @@ bookingById(id: $b1) {
'A type that uses an Enum.',
);

expect(mergedSchema.getType('NumericEnum').description).to.equal(
'A type that uses an Enum with a numeric constant.',
);

expect(mergedSchema.getType('LinkType').description).to.equal(
'A new type linking the Property type.',
);
Expand Down
22 changes: 22 additions & 0 deletions src/test/testSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,19 +890,27 @@ describe('generating schema from shorthand', () => {
RED
}
enum NumericEnum {
TEST
}
schema {
query: Query
}
type Query {
color: Color
numericEnum: NumericEnum
}
`;

const resolveFunctions = {
Color: {
RED: '#EA3232',
},
NumericEnum: {
TEST: 1
}
};

const jsSchema = makeExecutableSchema({
Expand All @@ -912,6 +920,7 @@ describe('generating schema from shorthand', () => {

expect(jsSchema.getQueryType().name).to.equal('Query');
expect(jsSchema.getType('Color')).to.be.an.instanceof(GraphQLEnumType);
expect(jsSchema.getType('NumericEnum')).to.be.an.instanceof(GraphQLEnumType);
});

it('supports passing the value for a GraphQLEnumType in resolveFunctions', () => {
Expand All @@ -920,27 +929,39 @@ describe('generating schema from shorthand', () => {
RED
}
enum NumericEnum {
TEST
}
schema {
query: Query
}
type Query {
color: Color
numericEnum: NumericEnum
}
`;

const testQuery = `{
color
numericEnum
}`;

const resolveFunctions = {
Color: {
RED: '#EA3232',
},
NumericEnum: {
TEST: 1,
},
Query: {
color() {
return '#EA3232';
},
numericEnum() {
return 1;
}
},
};

Expand All @@ -952,6 +973,7 @@ describe('generating schema from shorthand', () => {
const resultPromise = graphql(jsSchema, testQuery);
return resultPromise.then(result => {
assert.equal(result.data['color'], 'RED');
assert.equal(result.data['numericEnum'], 'TEST');
assert.equal(result.errors, undefined);
});
});
Expand Down

0 comments on commit 608414b

Please sign in to comment.