You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I got some problems with graphql-compose-elasticsearch (GraphQL wrapper for Elastic API) where some arguments are defined as GraphQLEnums and can contain booleans. So the problem was resolved by providing for boolean values specific keys boolean_true and boolean_false:
function generateEnum(typeName, vals) {
const values = vals.reduce(
(result, val) => {
if (val === '') {
result.EMPTY_STRING = { value: '' };
+ } else if (val === 'true' || val === true) {+ result.BOOLEAN_TRUE = { value: true };+ } else if (val === 'false' || val === false) {+ result.BOOLEAN_FALSE = { value: false };+ } else if (val === 'null' || val === null) {+ result.NULL_VALUE = { value: null };
} else if (Number.isFinite(val)) {
result[`NUMBER_${val}`] = { value: val };
} else {
result[val] = { value: val };
}
return result;
},
{}
);
return new GraphQLEnumType({
name: typeName,
values,
});
}
It works something like this:
> generateEnum('ComplexEnum', [1, 2, 3, true, false]); < enum ComplexEnum {< NUMBER_1< NUMBER_2< NUMBER_3< BOOLEAN_TRUE< BOOLEAN_FALSE< }
But this solution does not work for null. Also, it will not work for undefined and NaN as values for enum, cause internally used isNullish method type/definition.js#L979:
{BOOLEAN_FALSE: {value: false},// will return `false` in resolve methodNULL_VALUE: {value: null},// will return `NULL_VALUE` in resolve method instead of `null`}
I suppose that there should be another check in the helper method defineEnumValues, which should use valueName as value only if property/key value.value is not defined in object.
I did not found any restriction in spec that Enum values (not its keys) can not be false, null, undefined or NaN.
Or maybe I something missed?
@leebyron if i'm right, then will be nice to allow nullish values in 0.9.5 ASAP (cause v0.9.4 brings for me some breaking changes with ENUMs, and my suggested changes can also break someone's code). Better to introduce breaking changes for ENUM in one pass, cause not so many devs have yet updated to 0.9.4.
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
According to last changes in #812 Forbid 'true', 'false' and 'null' as key names for Enum due spec:
I got some problems with graphql-compose-elasticsearch (GraphQL wrapper for Elastic API) where some arguments are defined as GraphQLEnums and can contain booleans. So the problem was resolved by providing for boolean values specific keys
boolean_true
andboolean_false
:But this solution does not work for
null
. Also, it will not work forundefined
andNaN
as values for enum, cause internally usedisNullish
method type/definition.js#L979:So if I provide for GraphQLEnum such values
I suppose that there should be another check in the helper method
defineEnumValues
, which should usevalueName
as value only if property/keyvalue.value
is not defined in object.I did not found any restriction in spec that Enum values (not its keys) can not be
false
,null
,undefined
orNaN
.Or maybe I something missed?
@leebyron if i'm right, then will be nice to allow nullish values in 0.9.5 ASAP (cause v0.9.4 brings for me some breaking changes with ENUMs, and my suggested changes can also break someone's code). Better to introduce breaking changes for ENUM in one pass, cause not so many devs have yet updated to 0.9.4.
The text was updated successfully, but these errors were encountered: