question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto schema generation

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
timothyjlaurentcommented, Sep 28, 2017

@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.

1reaction
timothyjlaurentcommented, Sep 19, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automatic Schema Generation
The automatic schema generation feature of the Application Server defines database tables based on the fields or properties in entities and the relationships ......
Read more >
Automatic schema generation - Amazon DocumentDB
Automatic schema generation ... The schema generator may truncate the length of generated identifiers (table names and column names) to ensure they fit...
Read more >
Hibernate hbm2ddl.auto schema generation - Vlad Mihalcea
In this article, we are going to see how the Hibernate hbm2ddl.auto schema generation tool works, and when it's appropriate to use it....
Read more >
Automatic Schema Generation for Document-Oriented Systems
Our approach for “schema” generation has been inspired from Software Product Lines strategies based on feature models.
Read more >
Auto-generating a graph model from an existing schema - IBM
The graph modeler allows you to auto-generate graphs, based on primary and foreign key constraints in the database. Procedure. From the Filter Schemas...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found