Reputation: 10928
I have a short list of string literals which I would like to have represented in my GraphQl definition. I tried using enum, but it's not for string literals.
Say I had some list of string literals:
export const DanceTypeList = [
"truffle-shuffle",
"stanky-leg",
"ghost-ride-the-whip",
] as const;
// equivalent to ("truffle-shuffle" | "stanky-leg" | "ghost-ride-the-whip")
export type DanceType = typeof DanceTypeList[number];
How could I take this and make a GraphQl type which was more descriptive than GraphQlString? Ideally it would be able to auto-suggest in the interactive UI.
Upvotes: 5
Views: 5849
Reputation: 84687
There is no equivalent data structure in GraphQL. You can utilize an enum for what you're trying to do, but you'll need to modify the enum values' names to meet the specification's naming requirements.
enum DanceType {
truffle_shuffle
stanky_leg
ghost_ride_the_whip
}
In most GraphQL implementations, each enum value can be mapped to a separate "internal" value to make it easier to map them to an internal representation, for example a Postgres enum. See here for additional details.
You could also use a custom scalar that would only allow strings of certain values -- however, you would not get the same autocompletion you would if you used an enum.
Upvotes: 5