-
Reading the documentation for Mockstore I have no clue on how to work with collections there. e.g. how to add, remove and retrieve. For example if I had a mutation that sets a user e.g.
Then in my resolver for Could you give me some examples on how to worh with collections in Mockstore? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
@alexstrat cc |
Beta Was this translation helpful? Give feedback.
-
If, in your schema, a given field returns a list of object type (or abstract type), then, in the The default mock resolvers will interpret these With your example: For the sake of clarity, I'm assuming we are working with such schema: type User {
id: ID!
name: String!
}
type Query {
users: [User!]!
} Accessing, the value in the store for the query store.get('Query', 'ROOT', 'users')
> [{ $ref: { key: 'abc-737dh-djdjd', typeName: 'User' } }, { $ref: { key: 'def-888dh-lklkl', typeName: 'User' } }] Note, as explained in the doc, the root types ( Adding an entity in the list: const userRefs = store.get('Query', 'ROOT', 'users')
const newUser = store.get('User', '12'); // will return a ref for user entity with id='12'
const newUserRefs = [...userRefs, newUser];
store.set('Query', 'ROOT', 'users', newUserRefs);
store.get('Query', 'ROOT', 'users')
> [{ $ref: { key: 'abc-737dh-djdjd', typeName: 'User' } }, { $ref: { key: 'def-888dh-lklkl', typeName: 'User' } }, { $ref: { key: '12', typeName: 'User' } }] Removing by name: const userRefs = store.get('Query', 'ROOT', 'users')
const newUserRefs = userRefs.filter((ref) => store.get(ref, 'name') !== 'Sam')
store.set('Query', 'ROOT', 'users', newUserRefs); @sporto Is this clearer with these examples? If so, what point was missing in the doc according to you? Notes on improving the doc: |
Beta Was this translation helpful? Give feedback.
-
Thanks, another question please. Let say that I have a client with users.
Or
I also need to test mutations that add, modify and remove users. What is the best way to go about this? At the moment I have a function to add mock data to the store each time I reset the tests. I add the client to the mock store, but I'm unsure on how to add the users to the client in a way that is retrievable in the two ways shown above. I'm trying something like this:
|
Beta Was this translation helpful? Give feedback.
-
Very poor documentation. graphql tools is quite helpful. but when it comes to documentation, even the simplest things have very poor documentation. |
Beta Was this translation helpful? Give feedback.
-
How would one go about replicating the functionality of |
Beta Was this translation helpful? Give feedback.
If, in your schema, a given field returns a list of object type (or abstract type), then, in the
MockStore
, the corresponding field value will be an array ofRef
s, ie references to entities in the store. Therefore, working with lists means working with this array ofRef
s: get, set, push, filter, pop etc..The default mock resolvers will interpret these
Ref
s properly, and deep resolve the fields values.With your example:
For the sake of clarity, I'm assuming we are working with such schema:
Accessing, the value in the store for the query
users
: