fastapi + strawberry(graphql)

发布时间 2023-10-24 23:23:38作者: lightsong

Strawberry

https://fastapi.tiangolo.com/zh/how-to/graphql/

GraphQL with Strawberry

If you need or want to work with GraphQL, Strawberry is the recommended library as it has the design closest to FastAPI's design, it's all based on type annotations.

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

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

import strawberry
from fastapi import FastAPI
from strawberry.asgi import GraphQL


@strawberry.type
class User:
    name: str
    age: int


@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> User:
        return User(name="Patrick", age=100)


schema = strawberry.Schema(query=Query)


graphql_app = GraphQL(schema)

app = FastAPI()
app.add_route("/graphql", graphql_app)
app.add_websocket_route("/graphql", graphql_app)

 

Fast Api Strawberry GraphQL Async SQL Alchemy Boiler Plate

https://github.com/fanqingsong/fastapi_strawberry_graphql_sqlalchemy

Description

This code is a boiler plate for the implementation of GraphQL with Fast Api using Strawberry Library. For GraphQL server we have used Strawberry.

Features

  • Production ready Python web server using Uvicorn and Gunicorn.

  • Python FastAPI backend

  • Async Connection of SQL Alchemy with POSTGRESQL DataBase.

  • CRUD Operations of GraphQL using Strawberry Library.

  • Written Async Unit Tests using Pytest to test GraphQL queries and mutations.

  • Boiler Plate directory struture for GraphQL Python.

  • Get the data only from the columns using SQL Alchmey which are specified in GraphQL Query.

  • Deployment using Docker Container through Docker Compose file.

  • Deployed code at specific endpoint to test GraphQL.

  • Alembic migrations.

  • Jenkins (continuous integration).

 

Install Issues:

Error: pg_config executable not found.

https://www.cnblogs.com/qsxbc/p/13833710.html

 

ubuntu镜像

https://zhuanlan.zhihu.com/p/86370664

 

docker network

https://zhuanlan.zhihu.com/p/382779160