Comments (18)
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.
You can wrap one in the other:
CORSMiddleware(GraphQL(...))
GraphQL middlewares are separate from ASGI middlewares.
from ariadne.
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.
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.
@seimsel do you have example for graphql_sync
? how to use cors in that case?
from ariadne.
Great! Thank you @rafalp !
from ariadne.
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.
Please see https://www.starlette.io/middleware/#corsmiddleware
from ariadne.
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.
Excellent, many thanks really appreciated :)
from ariadne.
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.
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.
@seimsel do you have example for
graphql_sync
? how to use cors in that case?
No. I never used this.
from ariadne.
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.
What do you want to setup? CORSMiddleware?
from ariadne.
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.
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.
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)
- GraphiQL is using react development HOT 1
- Replace ApolloTracing with simple tracing extension HOT 3
- Rename main branch to `main` HOT 1
- Feature request: cache query parsing and validation HOT 10
- Breaking changes to Extension not noted in 0.20 changelog HOT 6
- Reference resolver HOT 2
- GraphiQL is not rendering (Explorer plugin error)
- Custom JSON implementation HOT 2
- Cannot install 0.15 HOT 2
- Expose all graphiql parameters HOT 1
- Union as directive argument HOT 4
- No way to send custom error from `on_connect` when using GraphQLTransportWSHandler
- execution_context_class argument for graphql_sync raises an error HOT 4
- Subscription Chat Application full example does not return message data HOT 2
- "str" is incompatible with "GraphQLSchema" HOT 2
- Update GraphQL fixing logic
- Python 3.12 has deprecation warnings
- Starlette integration tests are broken
- Re-tag the source HOT 1
- Ariadne `graphql` and `graphql_sync` will attempt to execute `subscription` operations
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ariadne.