Graphql-js: Query with all parameters optional can't be called with empty `()`

Created on 13 May 2019  路  1Comment  路  Source: graphql/graphql-js

I've found that given a query with all parameters optional, at least one parameter is still required to call the query with the same queryName(...) syntax:

type Query {
  search(text: String, start: Int, end: Int): String
}

search(text: '') and search work, but search() fails with

Syntax Error: Expected Name, found )

This forces client code to specifically look for all parameters being omitted and remove the (). It would be nice to not have to do this.

question

Most helpful comment

Syntax Error: Expected Name, found )

@dandv It's expected since GraphQL specification requires argument list to be non-empty:
https://graphql.github.io/graphql-spec/June2018/#sec-Language.Arguments

This forces client code to specifically look for all parameters being omitted and remove the (). It would be nice to not have to do this.

Client code shouldn't do that instead it should use query variables:

query ($text: String, $start: Int, $end: Int) {
  search(text: $text, start: $start, end: $end)
}

>All comments

Syntax Error: Expected Name, found )

@dandv It's expected since GraphQL specification requires argument list to be non-empty:
https://graphql.github.io/graphql-spec/June2018/#sec-Language.Arguments

This forces client code to specifically look for all parameters being omitted and remove the (). It would be nice to not have to do this.

Client code shouldn't do that instead it should use query variables:

query ($text: String, $start: Int, $end: Int) {
  search(text: $text, start: $start, end: $end)
}
Was this page helpful?
0 / 5 - 0 ratings