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_schema
to create executable schema instance. - Create view, route or controller (semantics vary between frameworks) that accepts
GET
andPOST
requests. - If request was made with
GET
method, return response containing GraphQL Playground's HTML. - If request was made with
POST
, disable any CSRF checks, test that its content type isapplication/json
then parse its content as JSON. Return400 BAD REQUEST
if this fails. - Call
graphql_sync
with schema, parsed JSON and any other options that are fit for your implementation. graphql_sync
returns tuple that has two values:boolean
anddict
. 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
operations
andmap
value of HTTP request. Return response with400
status 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_data
withoperations
,map
and data structure from step 2 as its values. - Call
graphql
orgraphql_sync
with value returned bycombine_multipart_data
passed indata
argument.
Rest of the algorithm is same as in regular queries.