在 Ariadne GraphQL 响应中添加标头

Pyt*_*ony 5 python graphql ariadne-graphql

我正在使用 ariadne 使用 python 制作 graphql 服务器。

我想向响应添加标头,例如用于设置 cookie 的 Set-Cookie。

有办法这样做吗?如果没有,有没有办法在阿里阿德涅设置一个库克。

我使用 React 作为前端。

请帮忙。谢谢!

Pau*_*uAI 0

更改“#将cookie添加到响应对象”部分下面的代码

from ariadne import make_executable_schema, gql

type_defs = gql("""
    type Query {
        example: String!
    }
""")

@app.route("/graphql", methods=["POST"])
def graphql():
    def resolve_example(_, info, **kwargs):
        # Add the cookie to the response object
        info.context["response"].set_cookie("my-custom-cookie", "my-custom-value")
        return "Example"

    resolvers = {
        "Query": {
            "example": resolve_example
        }
    }

    schema = make_executable_schema(type_defs, resolvers)
    return GraphQLView.as_view(schema=schema)(request)
Run Code Online (Sandbox Code Playgroud)