0

I have a graphql mutation defined as follows

type Mutation {
    updateParts(
      partId: String!
      parts: PartRequest!
    ): UpdatePartsResponse!
}

input PartRequest {
    name: String!
    image: Strings!
    boltTypes: [Bolts]!
}

input Bolts {
    code: String!
    metadata: String!
}

Mhy requirement was to update fields upon selection as following.

  • update all
mutation {
  updateParts(
    partId: "0x1223"
    parts: {
      name: "some"
      image: "dark.png"
      boltTypes: [
        { code: "A", metadata: "B" }
      ]
    }
  ) 
  }
}
  • Update by selection: name only
mutation {
  updateParts(
    partId: "0x1223"
    parts: {
      name: "some"
    }
  ) 
  }
}
  • Update by selection: parts only
mutation {
  updateParts(
    partId: "0x1223"
    parts: {
      boltTypes: [
        { code: "A", metadata: "B" }
      ]
    }
  ) 
  }
}

How to construct a schema to achieve this ?

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
not 0x12
  • 19,360
  • 22
  • 67
  • 133

1 Answers1

0

You have to make your arguments optional to achieve this. Try the following schema:

type Mutation {
    updateParts(
        partId: String!
        parts: PartRequest!
    ): UpdatePartsResponse!
}

input PartRequest {
    name: String
    image: String
    boltTypes: [Bolts!]
}

input Bolts {
    code: String!
    metadata: String!
}

Notice the PartRequest input object. The fields are nullable. That means you don't necessarily need to provide a value for those arguments. The ! means they are NON_NULL types, which means you have to provide a value for those always.

I have made the boltTypes field type to [Bolts!] which means the boltTypes field can be null, but the array cannot have null as an element.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60