graphql-compose / graphql-compose/graphql-compose-mongoose
update Mutation: push to Array instead of replacing it.
- Dominant language
- TypeScript
- Stars
- 706
- Forks
- 98
- PR merge metrics
- No merged PRs in 30d
Description
Let's say I have a Schema like this:
```js
const mySchema = new Schema({
foo: [{ type: String }]
}]
```
And I want to update an existing Document - from what I know currently we only have the option to replace our fields with new ones. Are there any options to
1. Push certain elements to a field
2. Pop certain elements from a field
Example push:
I have a document
```js
{
foo: [ "a", "b", "c"]
}
```
and want to add `"d"` to the array.
Currently I would need to
- Query the document to get foo
- Create a new array
- Mutate the document with the new array
which would cost many recources
_or_
- Create a custom Mutation that pushes instead of updates
which I see problematic if we have Schemas with multiple arrays and we want to allow mutations like
- update Array1
- replace Array2
Same Problems exist with popping elements.
I think it would be great if we had some kind of options in the Mutation itself. It would be super neat if we had a syntax that allows stuff like this:
```js
{
_id: "6099cb18f294f83dbcbf8936",
foo: ["blue"],
bar: ["dolores"],
xyz: ["deleteThis", "butNotThat"],
}
```
```graphql
mutation {
somethingUpdateById(
_id: "6099cb18f294f83dbcbf8936"
record: {
foo: ["green", "yellow"]
bar: ["Lorem", "Ipsum"]
xyz: ["deleteThis"]
}
options: {
arrays: {
foo: replace
bar: push
xyz: pop
}
}
}) {
record {
foo
bar
xyz
}
}
}
```
```js
#response
{
foo: ["green", "yellow"]
bar: ["dolores", "Lorem", "Ipsum"]
xyz: ["butNotThat"]
}
```
I know this is asking for extremely much, but perhaps I inspire someone to implement something like it. My skills unfortunately aren't high enough to do it myself.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.