Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 225
llevar Avatar asked Jan 04 '23 07:01

llevar


2 Answers

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()
like image 57
llevar Avatar answered Jan 06 '23 10:01

llevar


For me, https://docs.graphene-python.org/projects/django/en/latest/introspection/ was helpful. In my case schema was defined in the schema.py file of apiApp, and so the command to retrieve schema.json was as following.

./manage.py graphql_schema --schema apiApp.schema.schema --out schema.json
like image 22
Yuji Avatar answered Jan 06 '23 11:01

Yuji