Persisted Documents๐
In this section, we'll cover Undine's support for persisted documents, which offer a way to persist known GraphQL documents on the server for caching, reducing network traffic, or to use as an operation allow-list.
Installation๐
To enable persisted documents, you must first add the undine.persisted_documents app to your INSTALLED_APPS:
Then, add the persisted document registration view to your URLconf:
Before running migrations, you should have a look at the PersistedDocument Model
in undine.persisted_documents.model. This Model can be swapped out with your own
implementation using the UNDINE_PERSISTED_DOCUMENTS_MODEL
setting, similar to how the User model can be swapped out with AUTH_USER_MODEL.
Whether you decide to do this or not, remember to run migrations afterwards.
Usage๐
Once the app is installed, Undine is ready to accept persisted documents.
Persisted documents work through the same GraphQL endpoint used for regular GraphQL requests,
but instead of a query string, you must provide a documentId instead.
A documentId can be obtained by registering a new persisted document using the persisted document
registration view, as specified by the PERSISTED_DOCUMENTS_PATH
setting. The view accepts a dictionary of documents like this
...and returns a dictionary of documentIds like this
...where each key in the documents dictionary is defined by the user,
so that a documentId corresponding to a document is returned in the same key.
The keys are not used for anything else.
Response for this view follows the GraphQL response format, so any errors are returned in the "errors" key.
Note that a document with the same selection set produces a different
documentIdif they have different whitespace, newlines, or comments. This is to ensure that error locations stay consistent.
Permissions๐
You'll likely want to protect the persisted documents registration view with a permission check
so that only some users can register new persisted documents. This can be done by setting the
PERSISTED_DOCUMENTS_PERMISSION_CALLBACK
setting to a function that accepts a request and a document_map as arguments.
Allow-list mode๐
Persisted documents can be used to create an allow-list for GraphQL operations.
Usually this is done to enhance security of a system by preventing
malicious queries from being executed. Undine can be configured to only accept
persisted documents by setting the PERSISTED_DOCUMENTS_ONLY
setting to True.
When operating in this mode, your clients should call PersistedDocumentsView
during build time to register their queries and mutations.