Auto schema generation
See original GitHub issueSo 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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top GitHub Comments
@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.
@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.