strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Default values for scalar arguments passed as string

Open ichorid opened this issue 1 year ago • 5 comments

When declaring an optional argument with a scalar type, its default value is passed as a string in the resulting schema. This makes Strawberry-declared schemas incompatible with externally connected GraphQL consumers with strict schema checkers, such as Hasura.

The following code:

from typing import Optional

import strawberry


@strawberry.type
class Query:
    @strawberry.field
    def example(self,
                baz: int,
                foo: int | None = None ,
                bar: int = 10
                ) -> None:
        return None


schema = strawberry.Schema(query=Query)

produces the following default values in the schema:

...
                  "defaultValue": null
...
                  "defaultValue": "null"
...
                  "defaultValue": "10"
Schema inspection
{
  "data": {
    "__schema": {
      "queryType": {
        "name": "Query"
      },
      "mutationType": null,
      "types": [
        {
          "kind": "OBJECT",
          "name": "Query",
          "description": null,
          "fields": [
            {
              "name": "example",
              "description": null,
              "args": [
                {
                  "name": "baz",
                  "description": null,
                  "type": {
                    "kind": "NON_NULL",
                    "name": null,
                    "ofType": {
                      "kind": "SCALAR",
                      "name": "Int",
                      "ofType": null
                    }
                  },
                  "defaultValue": null
                },
                {
                  "name": "foo",
                  "description": null,
                  "type": {
                    "kind": "SCALAR",
                    "name": "Int",
                    "ofType": null
                  },
                  "defaultValue": "null"
                },
                {
                  "name": "bar",
                  "description": null,
                  "type": {
                    "kind": "NON_NULL",
                    "name": null,
                    "ofType": {
                      "kind": "SCALAR",
                      "name": "Int",
                      "ofType": null
                    }
                  },
                  "defaultValue": "10"
                }

System Information

Python 3.11 Ubuntu 22.10 Strawberry version: 0.209.2

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

ichorid avatar Oct 18 '23 09:10 ichorid

Would love to know if this is fixable. We just ran into this issue as well. We have a Java client that fails introspection of our Strawberry APIs because some default int values are strings instead of ints.

krispharper avatar May 15 '24 12:05 krispharper

@krispharper can you show us the error?

I've made a reproduction for this: https://play.strawberry.rocks/?gist=482103631096241ca1a08f95aeed655a

But to clarify, this behaviour is from GraphQL core and we don't have a lot of control over it 😊

Also I think the spec allows this: https://spec.graphql.org/October2021/#sec-The-__InputValue-Type (but I might have read it wrong :) )

patrick91 avatar May 15 '24 12:05 patrick91

@krispharper can you share the client implementation you're using? I'm with Patrick on this one. The type of __InputValue.defaultValue is string:

type __InputValue {
  name: String!
  description: String
  type: __Type!
  defaultValue: String
}

Source: Schema introspection schema, GraphQL Spec: https://spec.graphql.org/October2021/#sec-Schema-Introspection.Schema-Introspection-Schema

This is a bug that needs to be raised with the individual libraries not following the spec.

erikwrede avatar May 17 '24 12:05 erikwrede

It very well might be an issue with the client library. I'll try to make a reproducible example this weekend.

krispharper avatar May 17 '24 12:05 krispharper

I just ran into this as well, any possible solutions? I'm trying to connect my graphql api to hasura

x0y-gt avatar Aug 06 '24 16:08 x0y-gt