There are a few different ways to retrieve data from a
- Access the current
- Exchange a previous document for a newer copy
- Return documents from within a
Note: It is important to remember that a document retrieved from the store is considered stale-on-arrival and cannot be modified. It does not represent an instance but instead a stale copy, or snapshot, of the document at the time of retrieval. For an updated version of the document it needs to be exchanged for an updated copy (see below)
Querying Documents via the
A frozen read-only copy of the store is available via the
data property on the
The shape of a store is:
A GraphQL Schema with these two types:
Could have a store like:
Since the data is accessed by type as a regular array the usual array methods can be used for accessing the data, and Documents in the array can be treated as POJOs.
Exchange Documents for a Newer Copy
Since documents are immutable snapshots they need to be exchanged for updated copies. This can be done by using the
find method on a
Paper instance which will take the existing document to be exchanged for the latest copy.
Returning Documents from Mutation Transactions
It's often useful to be able to have immediate access to a
Document that has just been created/updated in a mutation transaction. This can be done by returning the Document, an array of Documents or an object of documents.
film variable representing the newly created document is returned and available outside via the
Similar with returning an object that can be destructured:
Or from an Array:
Traversing Connections Between Documents
GraphQL Paper has support for connections between types as defined by the GraphQL Schema. If it's a list of types or a singular type it can be handled by GraphQL Paper. Accessing connections on a document do not have any special API and are accessible like any other field, as a property on the document.
With a schema where a
Film can have multiple actors:
A film's actors could be accessed directly:
Connections between documents are created during a mutation and are covered in Mutating Data.