Persisted Documents🔗
In this section, we'll cover Undine's support for persisted documents — 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.
This can be done from the same GraphQL endpoint used for regular GraphQL requests
by providing a documentId
instead of a query
string.
A documentId
can be obtained by registering a persisted document using the
PersistedDocument
model. This can be done by posting the document to the
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 the corresponding documentId
is returned in in the same key.
The keys are not used for anything else.
Note that a document with the same selection set produces a different documentId
if they have different whitespace, newlines, or comments. This is to ensure that
error locations stay consistent.
Response for this view follows the GraphQL response format, so any errors are returned in the "errors" key.
You likely want to protect this view with a permission check. This can be done by setting the
PERSISTED_DOCUMENTS_PERMISSION_CALLBACK
setting to a function that accepts arequest
anddocument_map
as arguments.
Allow-list mode🔗
As mentioned, 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.