英文:
How to modify an object regarless of inexistence value
问题
I'm currently learning Graphql and I'm messing around with it,
I've this mutation resolver where links
is just an array of objects links = [{ ... }]
Mutation : {
update(parent, args) => {
links[args.id] = {
description: args.description,
url: args.url,
}
}
}
In this current configuration if i update the id=0
which contains the following:
{
id: 0,
description: "test",
url: "www.test.com",
}
The problem would be that if in the args
parameter there is no description
, then its value would be wiped out:
mutation {
update(id:0, url:"hello.com") {
url
}
}
results in
{
id: 0,
url: "hello.com"
}
Desired Behavior would be that if one of the two fields is not declared that would not affect its current value
What i tried:
Mutation : {
update(parent, args) => {
links[args.id] = {
description: (!!args.description ? void(0) : args.description ), // Check if description is null; if so, do nothing, else update the value
url: args.url,
}
}
}
I've to clarify that, assigning undefined
to description
is not what i want, I'm looking for a way to implement this using an inline if, and avoid idempotency, to keep things clean, I don't want something like this:
if (!!args.description)
...
else
...
英文:
I'm currently learning Graphql and I'm messing around with it,
I've this mutation resolver where links
is just an array of objects links = [{ ... }]
Mutation : {
update(parent, args) => {
links[args.id] = {
description: args.description,
url: args.url,
}
}
}
In this current configuration if i update the id=0
which contains the following:
{
id: 0,
description: "test",
url: "www.test.com",
}
The problem would be that if in the args
parameter there is no description
, then its value would be wiped out:
mutation {
update(id:0, url:"hello.com") {
url
}
}
results in
{
id: 0,
url: "hello.com"
}
Desired Behavior would be that if one of the two fields is not declared that would not affect its current value
What i tried:
Mutation : {
update(parent, args) => {
links[args.id] = {
description: (!!args.description ? void(0) : args.description ), // Check if description is null; if so, do nothing, else update the value
url: args.url,
}
}
}
I've to clarify that, assigning undefined
to description
is not what i want, I'm looking for a way to implement this using a inline if, and avoid idempotency, to keep things clean, i don't want something like this:
if (!!args.description)
...
else
...
答案1
得分: 1
使用扩展运算符:
const links = [{
id: 0,
description: "test",
url: "www.test.com",
}]
function update({id, ...args}) {
links[id] = { ...links[id], ...args }
}
update({id: 0, url: 'www.wikipedia.org'})
console.log(links)
英文:
Use the spread operator:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const links = [{
id: 0,
description: "test",
url: "www.test.com",
}]
function update({id, ...args}) {
links[id] = { ...links[id], ...args }
}
update({id: 0, url: 'www.wikipedia.org'})
console.log(links)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论