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

2

8

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.

Cork answered 18/10, 2017 at 11:25 Comment(0)
C
14

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()
Cork answered 18/10, 2017 at 13:25 Comment(1)
How to run this script?Rame
R
2

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
Rame answered 4/5, 2021 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.