Giter VIP home page Giter VIP logo

Comments (3)

ManiMozaffar avatar ManiMozaffar commented on August 19, 2024

I'd do the PR myself, but I wonder to see if that's something maintainers agree with me or not.

from graphene.

tcleonard avatar tcleonard commented on August 19, 2024

Graphql has a weird relationship with nullable field.
The nuance of "a required nullable field" is not achievable as far as I know in the schema definition... indeed the "no value" is null and is valid for optional fields too.
Meaning that

type Mutation {
  doNothing(input: DoNothingInput!): DoNothingPayload
}

type DoNothingPayload {
  nullableInt: Int
}

input DoNothingInput {
  nullableInt: Int
}

that can be obtained with:

nullable_int = graphene.Int(required=False)

serves both purposes of being nullable and being optional.

Indeed you can do the following 3 behavior:

  • you provide an actual integer input:
mutation {
    doNothing(input: { nullableInt: 1 }) {
        nullableInt
    }
}

and in your resolver "nullable_int" in input is True (and input.nullable_int is 1 and input["nullable_int"] is 1 )

  • you can provide null input:
mutation {
    doNothing(input: { nullableInt: null }) {
        nullableInt
    }
}

and in your resolver "nullable_int" in input is True (and input.nullable_int is None and input["nullable_int"] is None )

  • you can provide no input at all:
mutation {
    doNothing(input: { }) {
        nullableInt
    }
}

and in your resolver "nullable_int" in input is False (and input.nullable_int is None and input["nullable_int"] actually fails)

So if you want to have a required nullable input, your only way to do it is to make the type required=False and add an assertion in your mutation

assert "nullable_int" in input

There is no way as far as I know to enforce that at schema level...

from graphene.

tcleonard avatar tcleonard commented on August 19, 2024

Note that this issue is an open conversation in the grapqh specs: graphql/graphql-spec#476

from graphene.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.