strawberry
strawberry copied to clipboard
Default values for scalar arguments passed as string
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.
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 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 :) )
@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.
It very well might be an issue with the client library. I'll try to make a reproducible example this weekend.
I just ran into this as well, any possible solutions? I'm trying to connect my graphql api to hasura