graphql-compose / graphql-compose/graphql-compose-mongoose

Using Mongo's positional operator ($) in update mutations

Open
#394 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
706
Forks
98
PR merge metrics
No merged PRs in 30d

Description

Is it possible to use MongoDB's [positional operator](https://docs.mongodb.com/manual/reference/operator/update/positional/) in a generated `updateOne`/`updateMany` resolver?

For example, I would like to make an update like this (from Mongo's doc):
```
db.students.updateOne(
{ _id: 1, grades: 80 }, # Finding the 80 value in the grades array
{ $set: { "grades.$" : 82 } } # Replacing it by an 82
)
```

On my server, I've set the following generated update mutation:
```js
// ...
schemaComposer.Mutation.addFields({
studentUpdateOne: StudentTC.mongooseResolvers.updateOne(),
});
// ...
```

On my client, I've tried the following query and some others but I can't get the `$` to not generate syntax errors:
```
mutation {
studentUpdateOne(
filter: { # Filtering works OK
_id: 1,
grades: 80
},
record: { # Updating throws syntax errors
grades: { $: 82 } # I also tried ` "grades.$": 82 ` and ` grades["$"]: 82 ` with no luck
}
) {
record {
_id
grades
}
}
}
```

I am not yet very familiar with GraphQL syntax so I may be missing an easy fix.
But is it at all possible?
Thanks

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.