如何修改一个对象,无论其存在与否。

huangapple go评论62阅读模式
英文:

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 argsparameter 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: &quot;test&quot;,
   url: &quot;www.test.com&quot;,
}]

function update({id, ...args}) {
  links[id] = { ...links[id], ...args }
}

update({id: 0, url: &#39;www.wikipedia.org&#39;})

console.log(links)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 01:21:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定