Giter VIP home page Giter VIP logo

Comments (18)

rafalp avatar rafalp commented on July 22, 2024 2

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.

from ariadne.

patrys avatar patrys commented on July 22, 2024 2

You can wrap one in the other:

CORSMiddleware(GraphQL(...))

GraphQL middlewares are separate from ASGI middlewares.

from ariadne.

simon-zumbrunnen avatar simon-zumbrunnen commented on July 22, 2024 1

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=['*']))

from ariadne.

patrys avatar patrys commented on July 22, 2024 1

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)?

from ariadne.

007vasy avatar 007vasy commented on July 22, 2024 1

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

from ariadne.

chriswingler avatar chriswingler commented on July 22, 2024

Great! Thank you @rafalp !

from ariadne.

mapapa avatar mapapa commented on July 22, 2024

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

from ariadne.

patrys avatar patrys commented on July 22, 2024

Please see https://www.starlette.io/middleware/#corsmiddleware

from ariadne.

mapapa avatar mapapa commented on July 22, 2024

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

from ariadne.

mapapa avatar mapapa commented on July 22, 2024

Excellent, many thanks really appreciated :)

from ariadne.

rafalp avatar rafalp commented on July 22, 2024

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

from ariadne.

simon-zumbrunnen avatar simon-zumbrunnen commented on July 22, 2024

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

from ariadne.

simon-zumbrunnen avatar simon-zumbrunnen commented on July 22, 2024

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

No. I never used this.

from ariadne.

newtoniumx3 avatar newtoniumx3 commented on July 22, 2024

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

from ariadne.

simon-zumbrunnen avatar simon-zumbrunnen commented on July 22, 2024

What do you want to setup? CORSMiddleware?

from ariadne.

newtoniumx3 avatar newtoniumx3 commented on July 22, 2024

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)

from ariadne.

simon-zumbrunnen avatar simon-zumbrunnen commented on July 22, 2024

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.

from ariadne.

newtoniumx3 avatar newtoniumx3 commented on July 22, 2024

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!

from ariadne.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.