ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.25k stars 745 forks source link

Include optional parameter in the variables field of a request when the parameter value is null #6681

Closed CalvinQuark closed 1 year ago

CalvinQuark commented 1 year ago

Product

Strawberry Shake

Is your feature request related to a problem?

There is a compatibility issue when using a ChilliCream StrawberryShake generated client to fetch data from an Azure Data API Builder CLI-generated GraphQL endpoint.

If a query is defined with an optional $filter parameter such as:

query Todos(
  $filter: TodoFilterInput
) {
  todos(
    filter: $filter
  ) {
    items {
      Id
      Title
      DueOn
    }
  }
}

When a null value is passed to filter parameter via a ChilliCream StrawberryShake-generated client query's .ExecuteAsync(filter: null) method, the request does not include the "filter": null node in the variables section:

{
    "id": "ec304833fcb193a31fdd994a6c503e21",
    "query": "query Todos($filter: TodoFilterInput) { todos(filter: $filter) { __typename items { __typename Id Title DueOn } } }",
    "operationName": "Todos",
    "variables": {}
}

Unfortunately, the Azure Data API Builder CLI's GraphQL endpoint responds to this request with:

{
  "errors": [
    {
      "message": "The variable with the name `filter` does not exist."
    }
  ]
}

unless the null-valued filter variable is specifically included in the request payload. I.e., this works:

{
    "id": "ec304833fcb193a31fdd994a6c503e21",
    "query": "query Todos($filter: TodoFilterInput) { todos(filter: $filter) { __typename items { __typename Id Title DueOn } } }",
    "operationName": "Todos",
    "variables": {
        "filter": null
    }
}

I haven't discovered a workaround for this issue either on the client side with StrawberryShake or on the server side with Azure Data API Builder. Is there a way to coax the StrawberryShake-generated Client to include the unused null filter variable?

The solution you'd like

OOB compatibility between ChilliCream StrawberryShake generated clients and Azure Data API Builder CLI-generated GraphQL endpoints.

CalvinQuark commented 1 year ago

JSON serialization stripping out null properties seems to be the root cause.

CalvinQuark commented 1 year ago

According to ChatGPT-4, in GraphQL if an argument is optional, "the client should not be required to include it in the query". If so, then this is an issue for the Azure Data API Builder team rather than for the ChilliCream/StrawberryShake team. Consequently, I have posted the issue there.