question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for inline enum arguments

See original GitHub issue

Given that GraphQL serializes Enum values as strings, it would be great to have them supported as argument parameters. Currently they throw a Only scalar argument types currently supported. error.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

9reactions
PavanBahugunicommented, Apr 4, 2018

What if the Enum is inside a type? For example like below

mutation addSample($action: sampleInputType){ addSample(action: $action) { id } } sampleInputType has a enum field.

The above query do not work? How to handle nested enums with variables?

1reaction
LinnaViljamicommented, Oct 1, 2020

Prerequisites:

We must define < SomeEnumType > in our GraphQL schema (server side, no client configuration needed)

Let’s assume we have defined:

enum SomeEnumType {
    OPTION1,
    OPTION2,
    OPTION3
}

We also must configure our Apollo Client in appropriate way ans connect it with the GraphQL API.

Then on client side:

export const OUR_MUTATION = gql`
    mutation ourMutation($foo: SomeEnumType){
        ourMutation(foo: $foo){
            bar
        }
    }    
`

Only doing this, you can pass enum as a variable in your query or mutation. For example using useMutation hook we can now mutate as follows:

const [ourMutation] = useMutation(OUR_MUTATION, {
        variables: {
            foo: "OPTION2"
        },

Since our type definition in gql tag equals with definition in Schema, GraphQL recognizes our variable as an enum type despite of that we give it as a string.

If we want pass enum to our variables using typescript enums we can do it as follows:

enum SomeEnumType {
    OPTION1 = 0,
    OPTION2 = 1,
    OPTION3 = 2
}

const [ourMutation] = useMutation(OUR_MUTATION, {
        variables: {
            foo: SomeEnumType[SomeEnumType.OPTION1]
        },

Read more comments on GitHub >

github_iconTop Results From Across the Web

kotlin - can you help me explaining how this function work? ...
In Kotlin, inline functions get their bodies transformed in compile-time and, well, inlined at each of the call sites.
Read more >
enum — Support for enumerations
enum — Support for enumerations¶ · is a set of symbolic names (members) bound to unique values · can be iterated over to...
Read more >
TypeScript: Handbook - Enums
Numeric enums​​ An enum can be defined using the enum keyword. Above, we have a numeric enum where Up is initialized with 1...
Read more >
Enums
You can use the enum keyword to specify possible values of a request parameter or a model property. For example, the sort parameter...
Read more >
TypeScript string enums, and when and how to use them
enums are not natively supported in JavaScript, but there is a workaround ... declare them as types and pass them as arguments to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found