-
Notifications
You must be signed in to change notification settings - Fork 23
Description
When trying to connect to the graphql endpoint with Apollo, the following error occurs:
Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
However curl pulls just well:
curl -X POST -H "Content-Type: application/json" --data '{"query": "{hello}"}' http://randomhost/graphql
{"data":{"hello": "world"}}
Attached as such:
app = web.Application()
GraphQLView.attach(app, schema=schema, executor=AsyncioExecutor(loop=app.loop), graphiql=True)
Update:
Tried to create a child for GraphQLView as such:
class GQLView(GraphQLView):
def process_preflight(self, request):
""" Preflight request support for apollo-client
https://www.w3.org/TR/cors/#resource-preflight-requests """
headers = request.headers
origin = headers.get('Origin', '')
method = headers.get('Access-Control-Request-Method', '').upper()
accepted_methods = ['GET', 'POST', 'PUT', 'DELETE']
if method and method in accepted_methods:
return web.Response(
status=200,
headers={
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods': ', '.join(accepted_methods),
'Access-Control-Max-Age': str(self.max_age),
'Access-Control-Allow-Headers': "Content-Type"
}
)
return web.Response(status=400)
When using this, I can see the query hits the server (packet capture), and I get this error:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.