graphql / graphql-spec

GraphQL is a query language and execution engine tied to any backend service.
https://spec.graphql.org
14.3k stars 1.12k forks source link

GraphQL Mutation how to make fields optional to update fields only by selection and generate schema #1013

Open Kalanamith opened 1 year ago

Kalanamith commented 1 year ago

I have a graphql mutation defined as follows

    updateParts(
      partId: String!
      parts: PartRequest!
    ): UpdatePartsResponse!

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

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

and the requirement was to update fields upon selection like

mutation {
  updateParts(
    partId: "0x1223"
    parts: {
      name: "some"
      image: "dark.png"
      boltTypes: [
        { code: "A", metadata: "B" }

      ]
    }
  ) 
  }
}

Update by selection , parts only

mutation {
  updateParts(
    partId: "0x1223"
    parts: {

      boltTypes: [
        { code: "A", metadata: "B" }

      ]
    }
  ) 
  }
}

How to construct a schema to achieve this ?

benjie commented 1 year ago

Make the fields on PartRequest nullable, this makes them optional. Optionality and nullability are effectively equivalent in GraphQL inputs currently (with the exception of where default values are present).