首页 > 其他分享 >fastapi + strawberry(graphql)

fastapi + strawberry(graphql)

时间:2023-10-24 23:34:34浏览次数:44  
标签:strawberry fastapi app Strawberry GraphQL using graphql

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

 

标签:strawberry,fastapi,app,Strawberry,GraphQL,using,graphql
From: https://www.cnblogs.com/lightsong/p/17786015.html

相关文章

  • BUG:net::ERR_CONNECTION_REFUSED(前端Vue2、后端FastAPI)
    BUG场景一个前后端分离的项目,前端使用Vue2框架,后端使用FastAPI,前端想要传输图片给后端,使用的相关接口为:'http://10.96.67.161:8081/uploadImg/'后端FastAPI运行的代码为:if__name__=='__main__':uvicorn.run(app="main:app",host="localhost",port=8081,reload=Tr......
  • fastapi接口参数限制
    路徑的例子Path(title="TheIDoftheitemtoget")查詢參數Query(title="Querystring",description="Querystringfortheitemstosearchinthedatabasethathaveagoodmatch",min_length=3,)body的字段Field(defau......
  • fastapi设置超时时间
    方法一:应用级别的超时设置一种设置FastAPI应用程序全局超时时间的方法是使用TimeoutMiddleware中间件。以下是一个示例:fromfastapiimportFastAPIfromfastapi.middleware.timeoutimportTimeoutMiddlewarefromdatetimeimporttimedeltaapp=FastAPI()#设置应用......
  • 造轮子之集成GraphQL
    先简单对比以下GraphQL和WebAPI:GraphQL和WebAPI(如RESTfulAPI)是用于构建和提供Web服务的不同技术。数据获取方式:WebAPI:通常使用RESTfulAPI,客户端通过发送HTTP请求(如GET、POST、PUT、DELETE)来获取特定的数据。每个请求通常返回一个固定的数据结构,包含在响应的主体中。Gra......
  • pycharm使用fastapi/uvicorn无法reload的问题
    省流pycharm的问题,建议控制台直接输uvicornmain:app--reload,而不是点击右上角的运行相关issuehttps://github.com/encode/uvicorn/issues/2000https://youtrack.jetbrains.com/issue/PY-60962/os.killpid-signal.CRTLCEVENT-ignored-when-running-python-program-from-Pych......
  • 使用Hot Chocolate和.NET 6构建GraphQL应用 —— 创建Attribute中间件
    需求在部分接口添加一个机器人校验的功能思路读者们可以看下使用HotChocolate和.NET6构建GraphQL应用(5)——实现Query过滤功能,我们可以自定义创建一个类似的特性中间件来对接口进行管理.添加了该特性的接口即可实现机器人校验功能.实现输入对象///用户输入public......
  • python3的模块FastAPI,APIRouter
    FastAPI将依赖项的值从include_router传递给路由FastAPI依赖项和include_router在FastAPI中,依赖项是一种重要的机制,用于处理从请求到响应的整个过程中所需的各种依赖关系,例如数据库连接、身份验证等。依赖项可以被注入到请求处理函数中,并在执行时提供所需的值。在FastAPI中,我......
  • fastapi关掉框架自带422响应文档
    app=FastAPI()defcustom_openapi():ifnotapp.openapi_schema:app.openapi_schema=get_openapi(title=app.title,version=app.version,openapi_version=app.openapi_version,description=app.desc......
  • 【Python】FastAPI 使用python3.6+构建API的Web框架
    现代、快速(高性能)的Web框架,用于构建基于Python的 API;基于Starlette和Pydantic库构建而成官网:https://fastapi.tiangolo.com/ 1、安装#对于生产环境,还需要一个ASGI服务器,如Uvicorn或Hypercorn#>pipinstall"uvicorn[standard]"pipinstallfastapipipi......
  • fastapi手动添加swagger文档描述
    fastapi手动添加swagger文档描述"""在正常开发过程中,fastapi会自动地将正确响应(status=200)和输入校验失败响应(status=422)添加到文档中.当有自定义的响应描述添加到文档中时,就需要我们手动添加到路径函数的:responses参数中.用户可以按照openapi的语法,将响应的描述信息添加......