profusion / sgqlc

Simple GraphQL Client
https://sgqlc.readthedocs.io/
ISC License
506 stars 85 forks source link

Enums should be nullable on input #213

Closed kgadek closed 2 years ago

kgadek commented 2 years ago

Input fields deriving from Enum are not properly nullified. The resulting str(mutation) (prettified):

mutation {
  createUser(
    user: {
      firstName: "Jan"
      lastName: "Kowalski"
      middleName: null  // <-- this is string field, nullified correctly
      idType: None      // <-- BUG HERE: no value given to an enum should result in "null" instead
      idNumber: null
      // … snip
    }
  ) {
    // … snip
  }
}

Relevant code:

class IDType(sgqlc.types.Enum):
    __schema__ = admin_schema
    __choices__ = ('IDCard', 'Passport')

class UserInput(sgqlc.types.Input):
    __schema__ = admin_schema
    __field_names__ = ('first_name', 'middle_name', 'last_name', 'id_type', 'id_number', ...)
    first_name = sgqlc.types.Field(sgqlc.types.non_null(String), graphql_name='firstName')
    last_name = sgqlc.types.Field(sgqlc.types.non_null(String), graphql_name='lastName')
    middle_name = sgqlc.types.Field(String, graphql_name='middleName')
    id_type = sgqlc.types.Field(IDType, graphql_name='idType')
    id_number = sgqlc.types.Field(String, graphql_name='idNumber')
    # ... snip

Actual constructed values are None. It seems that the problem is in sgqlc.types.EnumMeta.__to_graphql_input__ which should check if value is None. This change fixed the problem for me

barbieri commented 2 years ago

thank you for spotting and fixing it :-) merged #214