Persisted queries for Django GraphQL
Dependencies
Django ≥ 1.11
Python ≥ 3.4
Installation
Install last stable version from Pypi.
pip install django-graphql-persist
Include the PersistMiddleware middleware in your MIDDLEWARE settings:
MIDDLEWARE = [
...
'graphql_persist.middleware.PersistMiddleware',
...
]
Loaders
Django-graphql-persist searches for documents directories in a number of places, depending on your DEFAULT_LOADER_CLASSES variable.
By default, Django-graphql-persist uses these document loaders:
AppDirectoriesLoader
Loads documents from Django apps on the filesystem. For each app in INSTALLED_APPS, the loader looks for a documents/ subdirectory defined by APP_DOCUMENT_DIR variable.
FilesystemLoader
Loads documents from the filesystem, according to DOCUMENTS_DIRS variable.
URLLoader
Loads documents from urls, according to DOCUMENTS_DIRS.
GRAPHQL_PERSIST = {
'DOCUMENTS_DIRS': [
'/app/documents', # FilesystemLoader
'https:// ... /documents', # URLLoader
],
}
Cached Loader
By default, the document system reads your documents every time they’re rendered.
Configure the CachedEngine engine for caching documents.
GRAPHQL_PERSIST = {
'DEFAULT_LOADER_ENGINE_CLASS': [
graphql_persist.loaders.CachedEngine',
],
}
Persisted Query definition
You can split schemas into separate files…
documents/fragments.graphql
fragment userFields on UserType {
id
email
}
and define Pythonic imports prefixed with #.
documents/GetViewer.graphql
# from fragments import userFields
{
viewer {
...userFields
}
}
Persisted Query by id
{
"id": "GetViewer",
"variables": {}
}
Multiple Operations
documents/users.graphql
# from fragments import userFields
query GetViewer {
viewer {
...userFields
}
}
query GetUsers($last: Int!) {
users(last: $last) {
...userFields
}
}
Persisted Query by id and operationName
{
"id": "users",
"operationName": "GetUsers",
"variables": {
"last": 2
}
}
✋ Versioning
The versioning scheme is defined by the DEFAULT_VERSIONING_CLASS setting variable.
GRAPHQL_PERSIST = {
'DEFAULT_VERSIONING_CLASS': 'graphql_persist.versioning.AcceptHeaderVersioning'
}
This is the full list of versioning classes.
DEFAULT_VERSIONING_CLASS |
Example |
|---|---|
AcceptHeaderVersioning |
application/json; version=v1 |
VendorTreeVersioning |
application/vnd.example.v1+json |
QueryParameterVersioning |
?version=v1 |
HostNameVersioning |
v1.example.com |
Configure the versioning scheme and storage the GraphQL documents according to the version.
👇 Example
POST /graphql HTTP/1.1
Accept: application/json; version=v1.full
{
"id": "GetViewer",
"variables": {}
}
documents/
|
├── v1/
│ ├── full/
│ | └── GetViewer.graphql 👈
│ └── basic/
| | └── GetViewer.graphql
| └── fragments/
| └── users.graphql
└── v2/
└── full/
└── basic/
👉 documents/v1/full/GetViewer.graphql
# from ..fragments.users import userFields
{
viewer {
...userFields
}
}
Settings
Here’s a list of settings available in Django-graphql-persist and their default values.
DOCUMENTS_DIRS
List of directories or urls searched for GraphQL SDL definitions Default: ()
CACHE_NAME
Cache key name `CACHES[name]` to cache the queries results Default: 'default'
QUERY_KEY_HANDLER
A custom function `f(query_id, request)` to generate the persisted query keys Default: 'graphql_persist.query.query_key_handler'
DEFAULT_VERSIONING_CLASS
A versioning class to determine the `request.version` attribute Default: None
DEFAULT_LOADER_ENGINE_CLASS
Class that takes a list of template loaders and attempts to load templates from them in order Default: 'graphql_persist.loaders.Engine' Note: Set variable to 'graphql_persist.loaders.CachedEngine' for caching documents
DEFAULT_LOADER_CLASSES
A list of loader classes to import documents from a particular source
Default: (
'graphql_persist.loaders.AppDirectoriesLoader',
'graphql_persist.loaders.FilesystemLoader',
'graphql_persist.loaders.URLLoader',
)
APP_DOCUMENT_DIR
Subdirectory of installed applications for searches documents Default: 'documents'
DOCUMENTS_EXT
GraphQL document file extension Default: '.graphql'
DEFAULT_RENDERER_CLASSES
A list of renderer classes that may be used when returning a persisted query response Default: ()
Metadata
Release files for django-graphql-persist 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-graphql-persist-0.1.1.tar.gz | 13.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_graphql_persist-0.1.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 31.4 kB
Release files / django-graphql-persist-0.1.1.tar.gz
| Download URL | django-graphql-persist-0.1.1.tar.gz |
|---|---|
| Size | 13.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea1d459e680f4e7d42fa562c87426ea1a38b66c09975217b1368e6330b612725
|
|
BLAKE2b-256 checksum How to use checksums |
e07254fb3b78a11028749927d59787461aef891029de7d3e8d3caa206665dbba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.6
|
Release files / django_graphql_persist-0.1.1-py2.py3-none-any.whl
| Download URL | django_graphql_persist-0.1.1-py2.py3-none-any.whl |
|---|---|
| Size | 18.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
3d6b49b19de0ef1dbef4faef58a7bb856a3df06f2513ea537390b14236e0adcb
|
|
BLAKE2b-256 checksum How to use checksums |
336132416c8746b8812a28a68b8697a22c91aa30f081cb3be55a9cb8b29ccf94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.6
|