question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default value for custom input object type

See original GitHub issue

I have a custom input object type

class Filters(graphene.InputObjectType):                                        
    name = graphene.String()
    type = graphene.List(graphene.String)

and the query definition

class Query(graphene.ObjectType):                                               
    search = graphene.Field(
        SearchResult, query=graphene.String(required=True),
        filters=graphene.Argument(Filters, default_value=None)
    )

I want to be able to provide an empty default value for the filters argument. With the above definition, the argument is not set as optional and I’m getting this error message:

{
  "errors": [
    {
      "message": "resolve_search() missing 1 required positional argument: 'filters'",
      "locations": [
        {
          "line": 15,
          "column": 3
        }
      ],
      "path": [
        "search"
      ]
    }
  ],
  "data": {
    "search": null
  }
}

What is the correct way to provide an empty default value for the argument of custom object type? I’m at these package versions:

graphene==2.1.3 graphql-core==2.1 graphql-server-core==1.1.1

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
artemnesterenkocommented, Dec 6, 2018

For now, I’ve ended up with the following solution:

import graphene

class InputObjectType(graphene.InputObjectType):

    @classmethod
    def default(cls):
        meta = cls._meta
        fields = meta.fields
        default_fields = {name: field.default_value for name, field in fields.items()}
        container = meta.container
        return container(**default_fields)


class SizeInput(InputObjectType):
    width = graphene.Int(default_value=100)
    height = graphene.Int(default_value=100)

    def square(self):
        return self.width * self.height

Since now, it’s possible to do:


class Query(graphene.ObjectType):
    square = graphene.Field(
        graphene.NonNull(graphene.Int),
        size=graphene.Argument(SizeInput, default_value=SizeInput.default())
    )
    
    @staticmethod
    def resolve_square(root, info, size, **kwargs):
        return size.square()

It produces the correct schema and passes the default value I was expecting instead of the empty dict.

2reactions
patrick91commented, Nov 6, 2018

Yeah using Filters() should work, so I guess there’s a bug somewhere 😃

I’ll see if I can have a look at it soon

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting the Default Value of Input Fields - Salesforce Help
Select the field. · Click Configure in the Properties pane. · Select the source of the default value. Option, Description. Fixed Value, Use...
Read more >
How to setup Custom field (insight object type) default value?
Solved: Is it possible to setup default value for Custom field which is Insight obejct type? This Custom field type doesn't have "Edit...
Read more >
How to set a default value for a field in graphene
1. For default values... · name : string. Override the name of the Field. · description : string. A description of the type...
Read more >
Input Object Types - Hot Chocolate - ChilliCream
Input object type definitions differ from object types only in the used keyword and in that their fields can not have arguments.
Read more >
Default value - Async-graphql Book
You can define default values for input value types. Below are some examples. Object field.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found