GraphQL Mutations - #11 #GraphQL #GraphQLMutations #GraphQLTutorial #GraphQLSchema #APIDevelopment

Everyday Be Coding
Everyday Be Coding
99 بار بازدید - 3 ماه پیش - #GraphQL
#GraphQL #GraphQLMutations #GraphQLTutorial #GraphQLSchema #APIDevelopment #WebDevelopment #GraphQLAPI #Programming #CodingTutorial #SoftwareDevelopment #FullStackDevelopment #BackendDevelopment #LearnGraphQL #GraphQLGuide #TechTutorial #DeveloperTips #GraphQLExample #GraphQLAdvanced #DataModification #ServerSideDevelopment

GraphQL mutations are designed to handle operations that modify data on the server, such as creating, updating, or deleting records. Mutations in GraphQL have a structure similar to queries but with a focus on changing data. Here’s a detailed breakdown of how to use mutations effectively in GraphQL.

Basic Structure of a Mutation
A GraphQL mutation is similar to a query but designed for write operations. Here’s a simple example to create a new user:

graphql
Copy code
mutation {
 createUser(input: { name: "John Doe", email: "[email protected]" }) {
   user {
     id
     name
     email
   }
 }
}
This mutation defines an operation createUser that takes an input object containing name and email, and it returns the id, name, and email of the newly created user.

Defining Mutations in the Schema
To use mutations, you need to define them in your GraphQL schema. Here’s an example schema definition for the createUser mutation:

graphql
Copy code
type Mutation {
 createUser(input: CreateUserInput!): CreateUserPayload
}

input CreateUserInput {
 name: String!
 email: String!
}

type CreateUserPayload {
 user: User
}

type User {
 id: ID!
 name: String!
 email: String!
}
Mutation: The root type for all mutations.
createUser: A field on the mutation type representing the mutation.
CreateUserInput: An input type that specifies the parameters required to create a user.
CreateUserPayload: The return type for the createUser mutation.
User: A type representing the user entity.
Example of a More Complex Mutation
Here’s a more complex example involving nested input types and additional fields:

graphql
Copy code
mutation {
 createOrder(input: {
   userId: "1",
   items: [
     { productId: "101", quantity: 2 },
     { productId: "102", quantity: 1 }
   ],
   shippingAddress: {
     street: "123 Main St",
     city: "New York",
     state: "NY",
     zip: "10001"
   }
 }) {
   order {
     id
     totalAmount
     items {
       product {
         id
         name
       }
       quantity
     }
     shippingAddress {
       street
       city
       state
       zip
     }
   }
 }
}
This mutation creates an order with multiple items and a shipping address, returning the order details including the product information for each item.

Defining the Complex Mutation in the Schema
Here’s the corresponding schema definition for the complex mutation example:

graphql
Copy code
type Mutation {
 createOrder(input: CreateOrderInput!): CreateOrderPayload
}

input CreateOrderInput {
 userId: ID!
 items: [OrderItemInput!]!
 shippingAddress: AddressInput!
}

input OrderItemInput {
 productId: ID!
 quantity: Int!
}

input AddressInput {
 street: String!
 city: String!
 state: String!
 zip: String!
}

type CreateOrderPayload {
 order: Order
}

type Order {
 id: ID!
 totalAmount: Float!
 items: [OrderItem!]!
 shippingAddress: Address!
}

type OrderItem {
 product: Product
 quantity: Int!
}

type Product {
 id: ID!
 name: String!
 price: Float!
}

type Address {
 street: String!
 city: String!
 state: String!
 zip: String!
}
Using Variables in Mutations
Variables make your mutations more flexible and secure. Here’s the same createOrder mutation using variables:

graphql
Copy code
mutation createOrder($input: CreateOrderInput!) {
 createOrder(input: $input) {
   order {
     id
     totalAmount
     items {
       product {
         id
         name
       }
       quantity
     }
     shippingAddress {
       street
       city
       state
       zip
     }
   }
 }
}
When executing this mutation, you provide the input variable:

json
Copy code
{
 "input": {
   "userId": "1",
   "items": [
     { "productId": "101", "quantity": 2 },
     { "productId": "102", "quantity": 1 }
   ],
   "shippingAddress": {
     "street": "123 Main St",
     "city": "New York",
     "state": "NY",
     "zip": "10001"
   }
 }
}
Handling Errors in Mutations
GraphQL mutations can include error handling within the payload type. Here’s an example schema for a mutation with error handling:

graphql
Copy code
type Mutation {
 createUser(input: CreateUserInput!): CreateUserPayload
}

input CreateUserInput {
 name: String!
 email: String!
}

type CreateUserPayload {
 user: User
 errors: [Error!]
}

type User {
 id: ID!
 name: String!
 email: String!
}

type Error {
 message: String!
 code: String!
}
In this schema, the CreateUserPayload includes an errors field, which is a list of Error objects. When performing the mutation, the server can populate the errors field if there are any issues.
3 ماه پیش در تاریخ 1403/03/09 منتشر شده است.
99 بـار بازدید شده
... بیشتر