Felix DUSENGIMANA
Felix DUSENGIMANA

Reputation: 1

Integrating FastAPI with graphene Graphql

from starlette_graphene3 import GraphQLApp
from graphene import Schema, Int, String, List, ObjectType
from fastapi import FastAPI


class Query(ObjectType):
    hello = String(name=String(default_value="World"))

    def resolve_hello(self, info, name):
        return 'Hello ' + name


app = FastAPI()


@app.get("/")
def home():
    return {
        "message": "Hello you may consider going to /graphql."
    }


schema = Schema(query=Query)
app.mount("/", GraphQLApp(schema=schema))

The code above are running without error but if I try to access the page from browser this is the output. enter image description here

The console results after accessing that graphql route is enter image description here

How can I solve this issue. Please I need your help. thank you!

Upvotes: 0

Views: 798

Answers (2)

hector
hector

Reputation: 967

GraphQl was removed from Starlette since version version 0.17.0.

https://www.starlette.io/graphql/

To make it work you have to import a graphic handler for the graphiQL ui:

from starlette_graphene3 import GraphQLApp, make_graphiql_handler

Then, you have to specify what happens on get:

app.mount("/graphql", GraphQLApp(schema=schema, on_get=make_graphiql_handler()))

You can use whatever you need as your mount point.

Upvotes: 0

bitnahian
bitnahian

Reputation: 586

If you're trying to hit /graphql endpoint, you should mount as follows:

app.mount("/graphql", GraphQLApp(schema=schema))

The first parameter in mount() accepts the route on which to serve the GraphQLApp on.

Upvotes: 1

Related Questions