Custom Server example with Flask
See original GitHub issueHey there! I ported the custom server example from django to flask.
It’s quite short and it would be nice to add it to the docs.
from ariadne import gql, ResolverMap, make_executable_schema
from ariadne.constants import PLAYGROUND_HTML
from graphql import format_error, graphql_sync
from flask import request, jsonify
from flask.views import MethodView
type_defs = gql("""
type Query {
hello: String!
}
""")
query = ResolverMap("Query")
@query.field("hello")
def resolve_hello(_, info):
request = info.context
print(request.headers)
user_agent = request.headers.get("User-Agent", "Guest")
return "Hello, %s!" % user_agent
schema = make_executable_schema(type_defs, query)
class GraphQlView(MethodView):
def get(self):
return PLAYGROUND_HTML, 200
def post(self):
data = request.get_json()
if data is None or not isinstance(data, dict):
return 'Bad Request', 400
variables = data.get('variables')
if variables and not isinstance(variables, dict):
return 'Bad Request', 400
# Note: Passing the request to the context is option. In Flask, the current
# request is allways accessible as flask.request.
result = graphql_sync(
schema,
data.get('query'),
context_value=request,
variable_values=variables,
operation_name=data.get('operationName')
)
response = {"data": result.data}
if result.errors:
response["errors"] = [format_error(e) for e in result.errors]
return jsonify(response)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:14 (13 by maintainers)
Top Results From Across the Web
How to Deploy a Flask Web Server on Cloud Foundry
Learn how to efficiently deploy a Flash web server on Cloud Foundry without stressing yourself about the process in this tutorial!
Read more >Deploy to Production — Flask Documentation (2.2.x)
This part of the tutorial assumes you have a server that you want to deploy your application to. It gives an overview of...
Read more >How to build a web application using Flask and deploy it to the ...
This piece of code is stored in our main.py. Line 1: Here we are importing the Flask module and creating a Flask web...
Read more >How To Make a Web Application Using Flask in Python 3
A local Python 3 programming environment, follow the tutorial for ... Warning Flask uses a simple web server to serve our application in...
Read more >Creating RESTful Web APIs using Flask and Python
For this example, the statement This is executed BEFORE each request. will be printed on the server first and then the function for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I rewrote the example as a complete flask application, which is ready to run:
This structure is well known, even for Flask beginners. More expirienced users know how to adapt this to using Blueprints or a Method view.
It’s been a while since I’ve did anything with flask, but wouldn’t something like this be possible and more idiomatic?