Graphene-django: Auto schema generation

Created on 11 Nov 2016  路  5Comments  路  Source: graphql-python/graphene-django

So I started playing around with graphene-django last night. We have a complex data model so I don't want to write a shadow graphql schema for our models. We have a similar approach for generating DRF serializers using metaprogramming so I decided to try something with and currently have a query that generates a schema from our app:

from django.apps import apps
from graphene import relay, ObjectType, AbstractType, Schema, Node
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField


def build_query_objs():
    queries = {}
    models = apps.get_app_config('our_app_name').get_models()
    for model in models:
        model_name = model.__name__
        node = type('{model_name}Node'.format(model_name=model_name),
                    (DjangoObjectType,),
                    dict(
                        Meta=type('Meta',
                                  (object,),
                                  dict(model=model,
                                       interfaces=(relay.Node,))
                                  )
                    )
                    )
        queries.update({model_name: relay.Node.Field(node)})
        queries.update({'all_{model_name}'.format(model_name=model_name): DjangoFilterConnectionField(node)})
    return queries

Query = type('Query', (ObjectType,), build_query_objs())

schema = Schema(query=Query)

This worked well but noticed that I couldn't filter on any fields so I have more code (on dev machine atm) that generates filter_fields, and order_filter_field arguments for the generated Meta classes. With a single pass I can filter on 1 thing. I'd like to be able to filter on any nested filters eg customer__order__note_contains or something. Is there a good to get all these query-filter strings for each model?

It would be awesome if you left the filter_fields blank that all possible filters would be generated for the node Meta class.

Most helpful comment

@himalkaya https://github.com/timothyjlaurent/auto-graphene-django

^^ Sorry it took a bit. This only currently generates a schema with queries, not mutations. I haven't fired it up in over a year. I hope it helps.

All 5 comments

just facing the same problem, would you mind to share your solution? especially relational queries

@himalkaya

I'm no longer using Graphene on the project, but I do still have the schema generation code you could try. I'll try to post to github so you can try.

that would be great, thx

@timothyjlaurent did you posted already?

@himalkaya https://github.com/timothyjlaurent/auto-graphene-django

^^ Sorry it took a bit. This only currently generates a schema with queries, not mutations. I haven't fired it up in over a year. I hope it helps.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mraak picture mraak  路  3Comments

hyusetiawan picture hyusetiawan  路  4Comments

ZuluPro picture ZuluPro  路  3Comments

nickhudkins picture nickhudkins  路  3Comments

Eraldo picture Eraldo  路  3Comments