Skip to content

Commit

Permalink
Added a custom server example using Flask to the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jrast committed Apr 10, 2019
1 parent 8e9cca7 commit 7811f12
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions docs/custom-server.rst
Expand Up @@ -102,3 +102,72 @@ The following example presents a basic GraphQL server using a Django framework::
status_code = 200 if success else 400
# Send response to client
return JsonResponse(result, status=status_code)


Basic GraphQL server with Flask
--------------------------------

The following example presents a basic GraphQL server using a Flask::

from flask import Flask, request, jsonify
from ariadne import QueryType, graphql_sync, make_executable_schema
from ariadne.constants import PLAYGROUND_HTML


type_defs = """
type Query {
hello: String!
}
"""

query = QueryType()


@query.field("hello")
def resolve_hello(_, info):
request = info.context
user_agent = request.headers.get("User-Agent", "Guest")
return "Hello, %s!" % user_agent


app = Flask(__name__)
schema = make_executable_schema(type_defs, query)


@app.route('/graphql', methods=['GET'])
def graphql_playgroud():
"""Serving the GraphQL Playground

Note: This endpoint is not required if you do not want to provide the playground
But keep in mind that clients can still explore your API, for example by
using the GraphQL desktop app.
"""
return PLAYGROUND_HTML, 200


@app.route('/graphql', methods=['POST'])
def graphql_server():
"""Serve GraphQL queries"""
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.
success, result = graphql_sync(
schema,
data,
context_value=request,
debug=app.debug
)

status_code = 200 if success else 400
return jsonify(result), status_code


if __name__ == '__main__':
app.run(debug=True)

0 comments on commit 7811f12

Please sign in to comment.