Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DjangoObjectType Generates Wrong Schema on Arrayfield #536

Closed
helloqiu opened this issue Oct 18, 2018 · 10 comments
Closed

DjangoObjectType Generates Wrong Schema on Arrayfield #536

helloqiu opened this issue Oct 18, 2018 · 10 comments

Comments

@helloqiu
Copy link
Contributor

Description

Get wrong result for python manage.py graphql_schema.
Here's an example repo which can make it more clear:
https://github.com/helloqiu/graphene_bug_example

If I define the model as followed:

from django.db import models
from django.contrib.postgres.fields import ArrayField


class Example(models.Model):
    simple_array = ArrayField(models.CharField(max_length=255))

and write the schema like this:

from graphene_django import DjangoObjectType
import graphene

from graphene_bug_example.models import Example as ExampleModel


class Example(DjangoObjectType):
    required_field = graphene.String(required=True)

    class Meta:
        model = ExampleModel


class Query(graphene.ObjectType):
    examples = graphene.List(Example)

schema = graphene.Schema(query=Query)

then run python manage.py graphql_schema, the String in simpleArray, which is an ArrayField, is nullable:

...
            {
              "name": "simpleArray",
              "description": "",
              "args": [],
              "type": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "LIST",
                  "name": null,
                  "ofType": {
                    "kind": "SCALAR",
                    "name": "String",
                    "ofType": null
                  }
                }
              },
              "isDeprecated": false,
              "deprecationReason": null
            },
...

Possible Solution

@convert_django_field.register(ArrayField)
def convert_postgres_array_to_list(field, registry=None):
    base_type = convert_django_field(field.base_field)
    if not isinstance(base_type, (List, NonNull)):
        base_type = type(base_type)
    return List(base_type, description=field.help_text, required=not field.null)

https://github.com/graphql-python/graphene-django/blob/master/graphene_django/converter.py#L212
I think we should restore required parameter after base_type = type(base_type).
Something like this:

...
    if not isinstance(base_type, (List, NonNull)):
        base_type = type(base_type) if not base_type.kwargs['required'] else NonNull(type(base_type))
...

Maybe I will create a PR if this is verified as a bug by the maintainer.

@helloqiu
Copy link
Contributor Author

Can't wait to make a PR. :)

@lucas-bremond
Copy link
Contributor

Has this issue been addressed already?

@phalt
Copy link
Contributor

phalt commented May 9, 2019

@lucas-bremond it looks like #3 references this. Are you still experiencing the issue?

@helloqiu
Copy link
Contributor Author

@phalt It seems that #3 has nothing related to this issue. The reference record you see above is a PR to @Hispar own fork.

Has this issue been addressed already?

@lucas-bremond I don't think so.

@phalt
Copy link
Contributor

phalt commented May 10, 2019

Thanks for clearing it up @helloqiu - adding this as an accepted change. Feel free to open a PR and contribute!

@phalt phalt added this to Accepted in Improvements May 10, 2019
helloqiu added a commit to helloqiu/graphene-django that referenced this issue May 22, 2019
@stale
Copy link

stale bot commented Jul 9, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 9, 2019
@RasmusKlett
Copy link

I'm experiencing this problem as well. It seems the pull request has stalled?

@stale stale bot removed the wontfix label Jul 10, 2019
@stale
Copy link

stale bot commented Sep 8, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Archivec
Copy link

Archivec commented Jul 4, 2022

Same issue exists for DjangoFormMutation with postgres SimpleArrayField as a form field. The generated schema results in a string.

from django.contrib.postgres.forms import SimpleArrayField


class SampleForm(forms.Form):
    array_string = SimpleArrayField(forms.CharField(max_length=128))

class SampleMutation(DjangoFormMutation):
    output = String()
    
    @classmethod
    def perform_mutate(cls, form, info):
        ...
input SampleMutationInput {
    arrayString: String!
}

Solution

Similar to OP's solution for model field, but instead uses graphene_django.forms.converter to register the form field.

@HasanIstanbouli
Copy link

Still having issue with converting ArrayField, where it's been shown as String in graphene schema, here is my code:

from django.contrib.postgres.fields import ArrayField
class ReportExport(models.Model):
	emails = ArrayField(models.EmailField(), blank=True, null=True)

class ReportExportForm(forms.ModelForm):

	class Meta:
		model = ReportExport
		exclude = []


class MutationReportExport(DjangoModelFormMutation):

	class Meta:
		form_class = ReportExportForm
		exclude_fields = ["some_fields"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Improvements
  
Accepted
Development

No branches or pull requests

6 participants