Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. """
  2. # graphql query to run in the playground
  3. {
  4. hello
  5. }
  6. """
  7.  
  8. from ariadne import QueryType, graphql, make_executable_schema
  9. from ariadne.constants import PLAYGROUND_HTML
  10. from quart import Quart, request, jsonify
  11. from ariadne.asgi import GraphQL
  12.  
  13. app = Quart(__name__)
  14.  
  15. # define a graphql schema
  16. type_defs = """
  17. type Query {
  18. hello: String!
  19. }
  20. """
  21.  
  22. query = QueryType() # graphql backend seems to require a query object, everybody uses this pattern
  23.  
  24. # resolver for the field 'hello'
  25. @query.field("hello")
  26. async def resolve_hello(_, info) -> str:
  27. request = info.context
  28. user_agent = request.headers.get("User-Agent", "Guest")
  29. return "Hello, %s!" % user_agent
  30.  
  31. schema = make_executable_schema(type_defs, query) # kind of magical
  32.  
  33. @app.route("/", methods=["GET"])
  34. async def index() -> str: # just here to confirm quart is running
  35. return "Hello, Ariadne"
  36.  
  37. @app.route("/graphql", methods=["GET"])
  38. async def graphql_playgroud() -> str: # supplies playground environment
  39. return PLAYGROUND_HTML, 200
  40.  
  41. @app.route("/graphql", methods=["POST"])
  42. async def graphql_server() -> str:
  43. data = await request.get_json()
  44. success, result = await graphql( # this is the async interface, flask uses sync_graphql
  45. schema,
  46. data,
  47. context_value=request,
  48. debug=app.debug
  49. )
  50. return jsonify(result), 200 if success else 400
  51.  
  52. if __name__ == "__main__":
  53. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement