Enum
You need to use $enum
if you want to define an enum in GQFn. Learn about enum
Specification
To use enum, you need to warp the value with $enum
function.
const query = gqfn([
{
toBytes: $ => $({ unit: $enum('MB'), value: 1000 }, true),
},
])
{
toBytes(unit: MB, value: 1000)
}
Why we need $enum
?
If we have a toBytes
field to get the number of bytes in a certain unit and we defined a enum to limit the input values.
enum UnitEnum {
KB
MB
GB
TB
}
type Query {
toBytes(unit: UnitEnum, value: Int!): Int!
}
And make a query with query builder:
const query = gqfn([
{
toBytes: $ => $({ unit: 'MB', value: 1000 }, true),
},
])
In GraphQL, enums have a different syntax than strings, and they are not compatible with each other. But query builder is designed to be schema-independent, in this example the builder only know MB
is a string, and it is impossible to determine whether the value is an enum or a string.
We need to make some different. As input object has used object, we can only use function or class, we choose function to wrap the enum value.
const query = gqfn([
{
toBytes: $ => $({ unit: () => 'MB', value: 1000 }, true),
},
])
Passing a function directly may lose type hints, so we provide a $enum
wrapper.
const query = gqfn([
{
toBytes: $ => $({ unit: $enum('MB'), value: 1000 }, true),
},
])
You can use enum in variables without $enum
wrapper.
const query = gqfn('query', {
unit: 'UnitEnum! = MB',
value: 'Int!',
}, [
{
toBytes: $ => $({ unit: $.unit, value: $.value }, true),
},
])
const data = client.query(query, {
unit: 'MB',
value: 1000,
})
query ($unit: UnitEnum! = MB, $value: Int!) {
toBytes(unit: $unit, value: $value)
}
Type definition
export type EnumPackage<T extends string> = () => T
export function $enum<T extends string>(content: T): EnumPackage<T>