folkloreinc/laravel-graphql

GraphQL Error: Variable of type "String" used in position expecting type "String!"

filipenatanael opened this issue · 2 comments

Hello Everyone!

So I'm getting the error below when I try to retrieve values from the endpoint.

GraphQL error: Variable "$id" of type "String" used in position expecting type "String!".

The same is happeinig when I try to use Int! as well.

Below is the code:

UserQuery.php

  public function args()
  {
    return [
      'id' => [
        'type' => Type::nonNull(Type::string())
      ]
    ];
  }

User.gql

query User($id: String) {
  user(id: $id){
    name
    email
  }
}
mfn commented

This Type::nonNull(Type::string()) is a required field, i.e. it can't contain null.

That GraphQL DSL idiom is to write this as String! (the ! is the same as Type::nonNull())

But in your query, you simple wrong $id: String when it should have been $id: String!; the types must match.

Probably the same for your "int" issue.

@mfn Thanks for your help

I was able to resolve adding the code below:

// laravel-graphql/config/graphql.php
'params_key' => 'variables',

I also had to adjust some things in the lib(apolloClient) that I was using in frontend application.

Thank you again :)