Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Unable to pass enum value as argument in GraphQL query #325

Closed
kouma opened this issue Jan 24, 2019 · 2 comments
Closed

Unable to pass enum value as argument in GraphQL query #325

kouma opened this issue Jan 24, 2019 · 2 comments
Labels
bug Something isn't working Component: GraphQl

Comments

@kouma
Copy link

kouma commented Jan 24, 2019

Hello there,

When you pass enum values as argument in your GraphQL queries, the validation fails. To reproduce this bug, let's use the super simple SortEnum that contains only 2 attributes, ASC and DESC :

enum SortEnum @doc(description: "This enumeration indicates whether to return results in ascending or descending order") {
    ASC
    DESC
}

We'll use it on the products query to sort the paginated result.

Preconditions (*)

  1. Magento 2.3
  2. graphql-php 0.13.0

Steps to reproduce (*)

  1. Install GraphiQL extension for Chrome here
  2. Set the endpoint with your site uri : https://yoursite.com/graphql
  3. In the left column, type this query :
query Products(
  $price: String,
  $sort: SortEnum = DESC
) {
    products (
        filter: {
          price: {
            gt: $price
          }
        },
        sort: {
          name: $sort
        }
    ) {
      items {
        id
        name
      }
    }
  }

Type these variables in the "Query Variables" block on the bottom left :

{
  "sort": "ASC",
  "price": "1"
}

...and hit the big upper button to perform the request.

Expected result (*)

  1. Normally in GraphQL, you are able to pass enum types as variables. The result should be a paginated list of products sorted by name ASC.

Actual result (*)

  1. The request doesn't perform well :

enum_issue

{
  "errors": [
    {
      "message": "Variable \"$sort\" of type \"SortEnum\" used in position expecting type \"SortEnum\".",
      "category": "graphql",
      "locations": [
        {
          "line": 3,
          "column": 3
        },
        {
          "line": 12,
          "column": 17
        }
      ]
    }
  ]
}

I don't know if it's a Magento or a graphql-php related error. The error seems to be caused by the graphql-php validation process, the class GraphQL\Validator\Rules\VariablesInAllowedPosition is causing the error.

I tested with custom enums and the issue is the same. The only way to succeed is to change the type SortEnum to String and pass value as a string, but it's not a proper solution at all.
Actually this is very annoying because you can't use enum types as arguments for queries.

@XxXgeoXxX XxXgeoXxX assigned XxXgeoXxX and unassigned XxXgeoXxX Jan 25, 2019
@naydav
Copy link
Contributor

naydav commented Jan 25, 2019

Fixed and merged in
#259

@naydav naydav closed this as completed Jan 25, 2019
@kouma
Copy link
Author

kouma commented Jan 25, 2019

Hey thank you for your time !

@TomashKhamlai TomashKhamlai added bug Something isn't working Component: GraphQl labels Mar 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working Component: GraphQl
Projects
None yet
Development

No branches or pull requests

4 participants