Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Join theFastAPI Cloud waiting list 🚀
Follow@fastapi onX (Twitter) to stay updated
FollowFastAPI onLinkedIn to stay updated
Subscribe to theFastAPI and friends newsletter 🎉
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor

GraphQL

AsFastAPI is based on theASGI standard, it's very easy to integrate anyGraphQL library also compatible with ASGI.

You can combine normal FastAPIpath operations with GraphQL on the same application.

Tip

GraphQL solves some very specific use cases.

It hasadvantages anddisadvantages when compared to commonweb APIs.

Make sure you evaluate if thebenefits for your use case compensate thedrawbacks. 🤓

GraphQL Libraries

Here are some of theGraphQL libraries that haveASGI support. You could use them withFastAPI:

GraphQL with Strawberry

If you need or want to work withGraphQL,Strawberry is therecommended library as it has the design closest toFastAPI's design, it's all based ontype annotations.

Depending on your use case, you might prefer to use a different library, but if you asked me, I would probably suggest you tryStrawberry.

Here's a small preview of how you could integrate Strawberry with FastAPI:

importstrawberryfromfastapiimportFastAPIfromstrawberry.fastapiimportGraphQLRouter@strawberry.typeclassUser:name:strage:int@strawberry.typeclassQuery:@strawberry.fielddefuser(self)->User:returnUser(name="Patrick",age=100)schema=strawberry.Schema(query=Query)graphql_app=GraphQLRouter(schema)app=FastAPI()app.include_router(graphql_app,prefix="/graphql")

You can learn more about Strawberry in theStrawberry documentation.

And also the docs aboutStrawberry with FastAPI.

OlderGraphQLApp from Starlette

Previous versions of Starlette included aGraphQLApp class to integrate withGraphene.

It was deprecated from Starlette, but if you have code that used it, you can easilymigrate tostarlette-graphene3, that covers the same use case and has analmost identical interface.

Tip

If you need GraphQL, I still would recommend you check outStrawberry, as it's based on type annotations instead of custom classes and types.

Learn More

You can learn more aboutGraphQL in theofficial GraphQL documentation.

You can also read more about each those libraries described above in their links.


[8]ページ先頭

©2009-2026 Movatter.jp