graphql-python / flask-graphql

Adds GraphQL support to your Flask application.
MIT License
1.32k stars 140 forks source link

Enabling "explorer" #68

Open orome opened 4 years ago

orome commented 4 years ago

Is there a way to enable the GraphiQL explorer for a Python (flask-graphql) backend?

KingDarBoja commented 4 years ago

Do something like as:

view_func = GraphQLView.as_view("graphql", schema=Schema(query=some_api_schema.Query), graphiql=True)
app.add_url_rule("/graphql", view_func=view_func)

Notice the graphiql=True argument being passed,

datavistics commented 4 years ago

@KingDarBoja , I think they mean like this: https://stackoverflow.com/questions/59791068/how-do-i-add-the-explorer-to-graphiql-using-flask-graphql

Using graphiql-explorer.

Do you have an example template that would work? Ive been trying to follow the stack overflow ticket, but Im not a react expert.

KingDarBoja commented 4 years ago

Ah, that thing is easy to implement, guess I could add that extension to the graphiql template on https://github.com/graphql-python/graphql-server as all server integration code lives there since v3.

datavistics commented 4 years ago

@KingDarBoja , any ideas for a quick fix, ie a template string? I tried myself like the ticket mentioned but had no luck.

KingDarBoja commented 4 years ago

@datavistics Best idea should be providing a working example, which make use of the graphiql_template option as described at https://github.com/graphql-python/graphql-server/blob/master/docs/flask.md . I could write it but would be at the weekend as I'm currently busy these days.

datavistics commented 4 years ago

Thanks for responding, and if you wrote it over the weekend that would be hugely helpful.

I tried for longer than I care to admit, but Im just at a loss between the js + react -> html -> jinja. I didnt see how to pass the schema, or some of the other parameters required.

KingDarBoja commented 4 years ago

@datavistics I hate doing stuff with React but was able to make it work with a custom template by looking at other repositories which tried to implement it (on JS ofc) like https://github.com/OneGraph/graphiql-explorer/issues/29#issue-517377769 .

My current result

image

What do you need (and I have done so far) on the graphiql_template:

GraphiQL Template on some py file ```py GRAPHIQL_VERSION = "1.0.3" GRAPHIQL_TEMPLATE = """ {{graphiql_html_title}}
Loading...
""" ```

Then just pass to GraphQLView class at graphiql_template field like below.

from flask import Flask
from graphql_server.flask import GraphQLView

from graphiql_explorer import GRAPHIQL_TEMPLATE, GRAPHIQL_VERSION
from schema import schema

app = Flask(__name__)

app.add_url_rule('/graphql', view_func=GraphQLView.as_view(
    'graphql',
    schema=schema,
    graphiql=True,
    graphiql_template=GRAPHIQL_TEMPLATE,
    graphiql_version=GRAPHIQL_VERSION,
))

if __name__ == '__main__':
    app.run()

NOTE: I am using graphql-server beta version so please follow these setup to know how to install flask integration with this or use flask-graphql beta v3.

Also, some stuff doesn't work at all like clicking on the explorer nodes as I haven't added the rest of options listed at https://github.com/OneGraph/graphiql-explorer-example/blob/master/src/App.js#L171 example but this initial setup should be the way to go.

KingDarBoja commented 4 years ago

Speaking of that, I found https://github.com/strawberry-graphql/strawberry/pull/293 which seems to point out some issues regarding graphql-explorer 🤔 I will take a closer look at it later.

datavistics commented 4 years ago

@KingDarBoja thanks a ton, but unfortunately I couldnt get it to work. Im using graphene-elastic which depends on graphene < 3.

datavistics commented 4 years ago

@KingDarBoja Is there a way to use this for graphene 2.1.8 and Flask-GraphQL 2.0.1 ?

KingDarBoja commented 4 years ago

@KingDarBoja Is there a way to use this for graphene 2.1.8 and Flask-GraphQL 2.0.1 ?

Flask-GraphQL 2 supports passing custom graphiql templates as well.

Ambro17 commented 4 years ago

@KingDarBoja Is your working graphiql version with explorer published on github? I can't get it to work and a working example from where to start would be a lifesaver

samhaaf commented 3 years ago

@KingDarBoja Thanks for this!

Would love to see it get fully developed. I.e. clicking on the nodes fills the query; node selections get updated when query is updated; node fields can be selected.

ScottChapman commented 1 year ago

Has anyone had any luck using graphiql v2?