Skip to main content
TopMiniSite

Back to all posts

How to Add A List In GraphQL?

Published on
8 min read
How to Add A List In GraphQL? image

Best GraphQL List Tutorials to Buy in October 2025

1 The Road to GraphQL: Your journey to master pragmatic GraphQL in JavaScript with React.js and Node.js

The Road to GraphQL: Your journey to master pragmatic GraphQL in JavaScript with React.js and Node.js

BUY & SAVE
$29.99
The Road to GraphQL: Your journey to master pragmatic GraphQL in JavaScript with React.js and Node.js
2 Learning GraphQL: Declarative Data Fetching for Modern Web Apps

Learning GraphQL: Declarative Data Fetching for Modern Web Apps

BUY & SAVE
$32.94 $45.99
Save 28%
Learning GraphQL: Declarative Data Fetching for Modern Web Apps
3 Learning Resources 9"x 11" Double Sided X-Y Axis Dry Erase Mats, Graphing, Math Classroom Accessories, Teaching Aids, Set of 10, Ages 6+

Learning Resources 9"x 11" Double Sided X-Y Axis Dry Erase Mats, Graphing, Math Classroom Accessories, Teaching Aids, Set of 10, Ages 6+

  • BOOST MATH SKILLS WITH DRY-ERASE GRAPHING MATS FOR EASY LEARNING!
  • PERFECT FOR GRADES K+, ENHANCES NUMERACY AND RELATIONAL CONCEPTS!
  • IDEAL GIFT FOR ANY OCCASION-FUN LEARNING WRAPPED IN CREATIVITY!
BUY & SAVE
$9.99
Learning Resources 9"x 11" Double Sided X-Y Axis Dry Erase Mats, Graphing, Math Classroom Accessories, Teaching Aids, Set of 10, Ages 6+
4 Black Hat GraphQL: Attacking Next Generation APIs

Black Hat GraphQL: Attacking Next Generation APIs

BUY & SAVE
$40.42 $59.99
Save 33%
Black Hat GraphQL: Attacking Next Generation APIs
5 Beginning GraphQL with React, NodeJS and Apollo

Beginning GraphQL with React, NodeJS and Apollo

BUY & SAVE
$7.99
Beginning GraphQL with React, NodeJS and Apollo
6 ASP.NET Core 9 Web API Cookbook: Over 60 hands-on recipes for building and securing enterprise web APIs with REST, GraphQL, and more

ASP.NET Core 9 Web API Cookbook: Over 60 hands-on recipes for building and securing enterprise web APIs with REST, GraphQL, and more

BUY & SAVE
$39.99
ASP.NET Core 9 Web API Cookbook: Over 60 hands-on recipes for building and securing enterprise web APIs with REST, GraphQL, and more
7 Apps and Services with .NET 8: Build practical projects with Blazor, .NET MAUI, gRPC, GraphQL, and other enterprise technologies

Apps and Services with .NET 8: Build practical projects with Blazor, .NET MAUI, gRPC, GraphQL, and other enterprise technologies

BUY & SAVE
$22.00
Apps and Services with .NET 8: Build practical projects with Blazor, .NET MAUI, gRPC, GraphQL, and other enterprise technologies
+
ONE MORE?

To add a list in GraphQL, you can define a field with a list type in the GraphQL schema. This allows you to retrieve multiple items of a specific type as an array. The syntax for adding a list in GraphQL is as follows:

type MyObjectType { myListField: [ItemType]! }

In the example above, MyObjectType represents an object type, and myListField is the field that will contain a list of ItemType items. The square brackets [] around ItemType indicate that it is a list type. The exclamation mark ! denotes that the list is non-null, meaning it must always contain a value (an empty list is still valid).

You can replace ItemType with any GraphQL type, such as String, Int, Boolean, or even a custom object type.

Once you have defined the list field in the schema, you can query for it in your GraphQL query and receive an array of items in the response.

Note: To add items to the list or modify the list dynamically, you need to provide a GraphQL mutation or have data manipulation operations defined in your API.

How to delete multiple items from a list in GraphQL?

In GraphQL, you can delete multiple items from a list by using mutations. Mutations allow you to make changes to the data in a GraphQL API. Here's an example of how you can delete multiple items from a list using a mutation:

First, define the mutation in your GraphQL schema. The mutation should include an input type that takes an array of IDs or any other criteria to identify the items you want to delete. For example:

type Mutation { deleteItems(input: DeleteItemsInput!): DeleteItemsPayload! }

input DeleteItemsInput { itemIds: [ID!]! }

type DeleteItemsPayload { success: Boolean! deletedItemIds: [ID!]! }

Next, implement the mutation resolver in your GraphQL resolver functions. The resolver should take the DeleteItemsInput as an argument and perform the deletion logic. It should return a DeleteItemsPayload that indicates success and provides the list of deleted item IDs. Here's an example implementation using JavaScript:

const Mutation = { deleteItems: (parent, { input }, context, info) => { const { itemIds } = input;

// Perform deletion logic here
const deletedItemIds = \[\];

// Loop through the itemIds and delete each item
itemIds.forEach(itemId => {
  // Delete the item with the given ID and push the ID to deletedItemIds array
  // Example implementation:
  deleteItemFromDatabase(itemId);
  deletedItemIds.push(itemId);
});

return {
  success: true,
  deletedItemIds,
};

}, };

Finally, you can execute the mutation in your GraphQL client by passing the list of item IDs you want to delete in the itemIds field of the deleteItems mutation. Here's an example mutation query in GraphQL:

mutation { deleteItems(input: { itemIds: [1, 2, 3] }) { success deletedItemIds } }

Make sure to customize the mutation and resolver logic based on your specific requirements and use the appropriate database operations to delete the items.

How to create an array in GraphQL?

In GraphQL, an array is represented as a List type. To create an array in GraphQL, follow these steps:

  1. Define the List type in your GraphQL schema:

type MyType { myArray: [String] }

In the above example, myArray is defined as a List of String type.

  1. Create a resolver function that fetches the data for the field myArray:

const resolverFunctions = { MyType: { myArray: () => { // Fetch the array data from your data source return ["item1", "item2", "item3"]; }, }, };

In the above example, the resolver function returns an array of string items.

  1. Add the resolver functions to your GraphQL server:

const { ApolloServer, gql } = require("apollo-server");

const typeDefs = gql` type MyType { myArray: [String] }

type Query { myField: MyType } `;

const resolvers = { Query: { myField: () => ({}), }, ...resolverFunctions };

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => { console.log(`Server ready at ${url}`); });

In the above example, the MyType is added to the GraphQL schema and the resolver functions are added to the resolvers object.

  1. Now you can query the array in GraphQL:

query { myField { myArray } }

The above query will return the array:

{ "data": { "myField": { "myArray": ["item1", "item2", "item3"] } } }

You can use the array in your GraphQL queries and mutations as needed.

How to add a list of strings in GraphQL?

In GraphQL, you can add a list of strings as a field in your schema, just like any other field. Here's an example of how to do it:

  1. Define your schema in schema.graphql file or wherever you store your schema definition. Add a new field with the type [String] to represent a list of strings:

type MyType { id: ID! name: String! stringList: [String]! }

type Query { myType: MyType }

  1. Implement the resolver functions for the Query and MyType types. Here's an example using the graphql-js library:

const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: { myType: { type: MyType, resolve: () => { // Generate some dummy data for the example const stringList = ['GraphQL', 'is', 'awesome']; return { id: '1', name: 'My Type', stringList: stringList, }; }, }, }, }), types: [MyType], });

const MyType = new GraphQLObjectType({ name: 'MyType', fields: { id: { type: GraphQLID }, name: { type: GraphQLString }, stringList: { type: new GraphQLList(GraphQLString) }, }, });

  1. With this setup, when you query for myType, you will receive an object with an ID, name, and the stringList field containing the list of strings:

query { myType { id name stringList } }

The response will be:

{ "data": { "myType": { "id": "1", "name": "My Type", "stringList": ["GraphQL", "is", "awesome"] } } }

That's it! You can now add a list of strings to your GraphQL schema and retrieve it through queries.

How to pass an array as an argument in GraphQL?

In GraphQL, the syntax to pass an array as an argument is similar to passing any other value. Here's an example:

  1. Define your GraphQL schema, which includes the array argument in the appropriate field:

type Query { getItems(ids: [ID!]!): [Item!]! }

type Item { id: ID! name: String! }

Here, the getItems field takes an array of non-null ID values as an argument.

  1. Implement the resolver function for the getItems field in your GraphQL server:

const resolvers = { Query: { getItems: (_, { ids }) => { // ids will be an array of IDs passed as an argument // Perform your logic to retrieve and return the items with the given IDs // Example implementation:

  const items = \[
    { id: '1', name: 'Item 1' },
    { id: '2', name: 'Item 2' },
    { id: '3', name: 'Item 3' },
  \];

  return items.filter(item => ids.includes(item.id));
},

}, };

In this resolver function, ids will be an array of IDs passed as an argument. You can perform your logic to retrieve and return the items with the given IDs.

  1. Query the GraphQL endpoint passing an array of IDs as an argument:

query { getItems(ids: ["1", "3"]) { id name } }

In this example query, we are passing an array of IDs ["1", "3"] to the getItems field.

The GraphQL server will then execute the resolver function, retrieve the items with the given IDs, and return them as a response.

How to remove an item from a list in GraphQL?

In GraphQL, you typically cannot directly modify the data on the server. Instead, you modify the data indirectly by sending a mutation to the server. To remove an item from a list in GraphQL, you would follow these steps:

  1. Define a mutation in your GraphQL schema that represents the action of removing an item from the list. This mutation should define the input parameters needed to identify the item you want to remove.
  2. Implement the resolver function for the mutation on the server side. This resolver function should handle the logic of removing the item from the list based on the provided input.
  3. Send a mutation request from the client to the server. The mutation request should use the defined mutation, and pass the necessary input parameters to identify the item to be removed.
  4. The server executes the resolver function, which removes the specified item from the list, and returns a response to the client indicating the success or failure of the removal.

Here's an example of what the code might look like for a GraphQL schema with a mutation to remove an item from a list:

type Item { id: ID! name: String! }

type Mutation { removeItem(id: ID!): Boolean }

And a corresponding server-side resolver function implementation in a hypothetical language:

const Resolvers = { Mutation: { removeItem: async (parent, { id }, context) => { const removedIndex = context.items.findIndex(item => item.id === id); if (removedIndex === -1) { throw new Error("Item not found"); }

  context.items.splice(removedIndex, 1);
  
  return true;
}

} }

In this example, the removeItem mutation takes an id as an argument to identify the specific item to remove. The server-side resolver function then finds the item in the items list stored in the context (which typically represents your data source, like a database), removes it, and returns true indicating the successful removal.

Please note that the actual implementation and syntax might vary based on the GraphQL library or framework you are using, but the general approach of defining a mutation and implementing a resolver function remains the same.