Add Headers in Ariadne GraphQL Response

498 Views Asked by At

I am using ariadne to make a graphql server using python.

I want to add headers to the response like Set-Cookie for setting a cookie.

Is there a way to do so? If not is there a way to set a cooke in ariadne.

I am using React as frontend.

Please help. Thanks!

1

There are 1 best solutions below

0
On

Change the code below the "# Add the cookie to the response object" part

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)