Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Input field Optional[OneToManyInput] is a required field in the resulting schema #9

Closed
gersmann opened this issue Feb 18, 2022 · 8 comments

Comments

@gersmann
Copy link
Contributor

gersmann commented Feb 18, 2022

Not sure if this is expected behaviour, but if I define an input type like this:

@gql.django.input(ServiceInstance)
class ServiceInstancePartialInput(NodeInput):
    id: Optional[gql.ID]
    service: Optional[OneToManyInput]

The resulting schema marks 'service' as required field:
image

I can pass an empty object into service, since set is not required, but I'd expect service to be not required instead.
Edit: the schema allows for it, but if I do that the mutation fails.

@bellini666
Copy link
Member

Hey @gersmann ,

Hrm, I'm trying to replicate the issue here but it is working fine for me. The schema shows as not required and I can not send service at all to the input and it will work. It will not work if the field cannot be null though, for obvious reasons.

Can you provide more information regarding this? Like, the traceback itself, how are you doing the mutation, etc?

@gersmann
Copy link
Contributor Author

gersmann commented Feb 21, 2022

@bellini666 you're right, not sure how that schema came to be, I didn't reproduce the problem correctly. It happens when inheriting a base type, this should reproduce it:

@gql.django.input(ServiceInstance)
class ServiceInstancePartialInput(NodeInput):
    id: gql.ID
    service: Optional[OneToManyInput]
    key: Optional[str]

@gql.django.input(Connector)
class ConnectorPartialInput(ServiceInstancePartialInput):
    pass
@gql.type
class ServiceMutation:
    update_connector: ConnectorType = gql.django.update_mutation(ConnectorPartialInput)
{
  "data": null,
  "errors": [
    {
      "message": "Field 'ConnectorPartialInput.service' of required type 'OneToManyInput!' was not provided.",
      "locations": [
        {
          "line": 18,
          "column": 25
        }
      ],
      "path": null
    }
  ]
}

Stack:

edit: removed

@gersmann
Copy link
Contributor Author

Noticed that inheritance behaves a bit strange in general, for instance if case a Django foreign key relationship is inherited, it is represented by the generic DjangoModelType, but if it is defined directly on the child type, it is represented by the correct concrete GraphQL type.

@bellini666
Copy link
Member

Hey @gersmann ,

You are calling your types Partial, but you are using gql.django.input on them instead of gql.django.partial (or even gql.django.input(partial=True)). That may be the issue with it being required.

Noticed that inheritance behaves a bit strange in general, for instance if case a Django foreign key relationship is inherited, it is represented by the generic DjangoModelType, but if it is defined directly on the child type, it is represented by the correct concrete GraphQL type.

Don't know if I understood what you meant here... Could you provide an example of that too?

@gersmann
Copy link
Contributor Author

gersmann commented Feb 25, 2022

@bellini666

sorry for the screenshots, haven't been able to export the schema with django yet (apps aren't ready...)

So this definitiion:

@gql.django.type(Customer)
class CustomerType:
    id: gql.auto
    name: gql.auto


@gql.django.type(ServiceInstance)
class ServiceInstanceType:
    customer: CustomerType

@gql.django.type(Connector)
class ConnectorType(ServiceInstanceType):
    pass

@gql.type
class Query:
    service_instances: List[ServiceInstanceType] = gql.field()
    connectors: List[ConnectorType] = gql.field()

Gives me for ServiceInstanceType:
image

But for ConnectorType:
image

Thats what I meant this inheritance behaving a bit strange.

@gersmann
Copy link
Contributor Author

This here replicates the original issue, also seems like it is inheritance-related:

@gql.django.input(ServiceInstance)
class ServiceInstanceInput:
    service: Optional[OneToManyInput]


@gql.django.input(Connector)
class ConnectorInput(ServiceInstanceInput):
    updated_at: gql.auto


@strawberry.type
class Mutation:
    create_service_instance: ServiceInstanceType = gql.django.create_mutation(ServiceInstanceInput)
    create_connector: ConnectorType = gql.django.create_mutation(ConnectorInput)

ServiceInstanceInput:
image

ConnectorInput:
image

@bellini666
Copy link
Member

Hey @gersmann ,

Think I found the issue. Should be fixed in the release I'm doing right now. Please reopen this issue if it is not

@gersmann
Copy link
Contributor Author

@bellini666 Thanks a lot.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants