How to add list in graphql?

Member

by dedrick , in category: Javascript , 7 months ago

How to add list in graphql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 7 months ago

@dedrick 

To add a list in GraphQL, you can define a field in your GraphQL schema with a type that represents a list. Here's an example:

1
2
3
4
5
6
7
8
type Person {
  name: String
  hobbies: [String]
}

type Query {
  person: Person
}


In the above example, the hobbies field is defined as a list of strings ([String]).


To add a list of hobbies to a person, you can provide an array value when querying or mutating the hobbies field. Here's an example mutation to add hobbies to a person:

1
2
3
4
5
6
mutation {
  addHobbies(hobbies: ["hobby1", "hobby2"]) {
    name
    hobbies
  }
}


In this example, we assume there is a mutation called addHobbies that takes an array of strings called hobbies as an argument. The mutation would return the updated person's name and hobbies fields.


By using a list type in GraphQL, you can easily handle querying and mutating multiple values of the same type.