Other technologies
Ariadne can be used to add GraphQL server to projects developed using any web framework that supports JSON responses.
Implementation details differ between frameworks, but same steps apply for most of them:
- Use
make_executable_schemato create executable schema instance. - Create view, route or controller (semantics vary between frameworks) that accepts
GETandPOSTrequests. - If request was made with
GETmethod, return response containing GraphQL Playground's HTML. - If request was made with
POST, disable any CSRF checks, test that its content type isapplication/jsonthen parse its content as JSON. Return400 BAD REQUESTif this fails. - Call
graphql_syncwith schema, parsed JSON and any other options that are fit for your implementation. graphql_syncreturns tuple that has two values:booleananddict. Use dict as data for JSON response, and boolean for status code. If boolean istrue, set response's status code to200, otherwise it should be400
See the Flask integration for implementation of this algorithm using Flask framework.
Asynchronous servers
If your server stack supports ASGI, you can use graphql to execute GraphQL queries asynchronously and subscribe for websocket connections initialized by subscriptions.
File uploads
To support file uploads, your POST method implementation will need to be extended to allow the multipart/form-data requests, following algorithm supplied below:
- Parse JSON stored in
operationsandmapvalue of HTTP request. Return response with400status code if parsing of those values fails. - Create
dict(or any Python object that implements__getitem__) that contains remaining query's values. If possible, filter off items that aren't an uploaded file. - Call
combine_multipart_datawithoperations,mapand data structure from step 2 as its values. - Call
graphqlorgraphql_syncwith value returned bycombine_multipart_data.
Rest of the algorithm is same as in regular queries.