Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OPTIONS requests #104

Closed
chriswingler opened this issue Feb 3, 2019 · 18 comments
Closed

Support OPTIONS requests #104

chriswingler opened this issue Feb 3, 2019 · 18 comments
Labels
bug Something isn't working decision needed Sounds like good idea, but will need closer scrutiny for final decision.

Comments

@chriswingler
Copy link

Hi, I keep getting a 405 when using the example code, and I'm wondering if it's a CORS issue, since my frontend and backend are running on different ports. If so, how do I enable CORS with Ariadne?

@rafalp
Copy link
Collaborator

rafalp commented Feb 4, 2019

Yeop, Ariadne only supports GET and POST, missing OPTIONS 😂

We'll be fixing that, but in meantime you would have to wrap GraphQLMiddleware with a custom one that handles OPTIONS request and sends CORS headers.

@rafalp rafalp added the bug Something isn't working label Feb 4, 2019
@rafalp rafalp added this to the 0.3.0 milestone Feb 4, 2019
@chriswingler
Copy link
Author

Great! Thank you @rafalp !

@rafalp rafalp added the decision needed Sounds like good idea, but will need closer scrutiny for final decision. label Feb 5, 2019
@rafalp rafalp removed this from the 0.3.0 milestone Apr 8, 2019
@rafalp rafalp changed the title 405 Method not allowed Support OPTIONS requests Jun 3, 2019
@mapapa
Copy link

mapapa commented Aug 12, 2019

Hi, I am currently running 0.5.0 and still get the same error. Going through the source code I cannot find an example using a CORS middleware namely one that instantiates GraphQL with a cors middleware such as CORSMiddleware from starlette.middleware.cors. Any help is highly appreciated. Thanks

@patrys
Copy link
Contributor

patrys commented Aug 12, 2019

@mapapa
Copy link

mapapa commented Aug 12, 2019

Hihi Patrys,

Many thanks for your reply; the buzzling bit in my case is how do I tell GaphQL to use this middleware namely:

from starlette.applications import Starlette
from starlette.middleware.cors import CORSMiddleware

app = Starlette()
app.add_middleware(CORSMiddleware, allow_origins=['*'])

then when I look into ariadne example I have
app = GraphQL(schema, debug=True, middleware=[???])

so how do I tell GraphQL to use this particular middleware?

Many thanks,
Manos

@patrys
Copy link
Contributor

patrys commented Aug 12, 2019

You can wrap one in the other:

CORSMiddleware(GraphQL(...))

GraphQL middlewares are separate from ASGI middlewares.

@mapapa
Copy link

mapapa commented Aug 12, 2019

Excellent, many thanks really appreciated :)

@rafalp
Copy link
Collaborator

rafalp commented Sep 13, 2019

Existing CORS middlewares already solve this problem, so we'll won't be doing this in Ariadne proper. Still, we'll want to eventually document this in our docs: mirumee/ariadne-website#5

@rafalp rafalp closed this as completed Sep 13, 2019
@simon-zumbrunnen
Copy link

I get Redirect is not allowed for a preflight request. when trying to subscribe to a graphql subscription.

Here is how I set up my middlewares:

app = Application(routes=routes, debug=True)
app.mount('/graphql', CORSMiddleware(GraphQL(schema, debug=True), allow_origins=['*']))

@patrys
Copy link
Contributor

patrys commented Mar 4, 2020

Are you sure you're not missing a trailing slash somewhere (eg. that the server is not trying to redirect you to the correct URL)?

@simon-zumbrunnen
Copy link

You're right. I looked at the URL forever to figure out if it was my fault, but I just didn't see it. Thanks alot. :-D

@007vasy
Copy link

007vasy commented Nov 24, 2020

@seimsel do you have example for graphql_sync? how to use cors in that case?

@simon-zumbrunnen
Copy link

@seimsel do you have example for graphql_sync? how to use cors in that case?

No. I never used this.

@newtoniumx3
Copy link

Hey guys I'm confused how do I set this up if I'm using flask_restful like so

from flask import Flask
from flask_restful import Api

app = Flask(__name__)
api = Api(app)
api.add_resource(Graphql, "/graphql")
from ariadne import ObjectType
from ariadne import graphql_sync
from ariadne import load_schema_from_path
from ariadne import make_executable_schema
from ariadne import snake_case_fallback_resolvers

query = ObjectType("Query")
mutation = ObjectType("Mutation")
query.set_field("one", resolve_one)
mutation.set_field("two", resolve_two)
type_defs = load_schema_from_path(SCHEMA_FILE)

schema = make_executable_schema(
    type_defs, query, mutation, snake_case_fallback_resolvers
)


class Graphql(Resource):
    def get(self):
        return PLAYGROUND_HTML, 200

    def post(self):
        data = request.get_json()

        success, result = graphql_sync(
            schema,
            data,
            context_value=request,
            debug=True
        )

        status_code = 200 if success else 400
        return result, status_code

@simon-zumbrunnen
Copy link

What do you want to setup? CORSMiddleware?

@newtoniumx3
Copy link

What do you want to setup? CORSMiddleware?

I was just trying to get CORS setup to allow origin * as my client is on a different port. I was just playing with this I think I might have found something that works when I use flask_cors, is that the right way to do this if I use ariadne?

from flask_cors import CORS
from flask import Flask
app = Flask(__name__)
cors = CORS(app)

@simon-zumbrunnen
Copy link

simon-zumbrunnen commented Jan 5, 2021

As far as I see Ariadne never sees any http request at all (since you're using graphql_sync). So this is a question on how to handle CORS in Flask. For which it seems you are on the right track.

@newtoniumx3
Copy link

As far as I see Ariadne never sees any http request at all (since you're using graphql_sync). So this is a question on how to handle CORS in Flask. For which it seems you are on the right track.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working decision needed Sounds like good idea, but will need closer scrutiny for final decision.
Projects
None yet
Development

No branches or pull requests

7 participants