Search code examples
reactjsgraphqlrelayjsgraphene-python

Auto-generating a graphql schema for relay (Graphene server)


I am new to Relay and am trying to put together my first app. I already have a GraphQL server (using Graphene) that is backed by a PostgreSQL DB via SQLAlchemy automap, and published as a Flask app. Now, I'm trying to put together the front end, and it looks like the relay-compiler is expecting a GraphQL schema file on the client-side. I'm wondering if there is a way to have this schema file be dynamically auto generated, and how that could be set up.

I'm using https://github.com/kriasoft/react-static-boilerplate as the starting point for my app.

Thanks.


Solution

  • After browsing around the Graphene codebase I found schema_printer in the utils module of graphql-python that gets the job done for me:

    import json
    from schema import schema
    import sys
    from graphql.utils import schema_printer
    
    my_schema_str = schema_printer.print_schema(schema)
    fp = open("schema.graphql", "w")
    fp.write(my_schema_str)
    fp.close()