GraphQL Mutations in Apollo

GraphQL Mutations in Apollo

GraphQL Mutations in Apollo

In this post, we will give you information about GraphQL Mutations in Apollo. Here we will give you detail about GraphQL Mutations in Apollo And how to use it also give you a demo for it if it is necessary.

A GraphQL mutation is an API operation that modifies data. Like Query, Mutation is a special type in your GraphQL schema:

const schema = '
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment: CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
';

Every member of the Mutation type is a distinct API operation that you can use to modify data. In the above schema, there is exactly
one mutation: increment(). The increment() operation returns an object of type CountResult.

Implementing a Mutation for  GraphQL Mutations in Apollo

A GraphQL schema is just a list of type definitions. You also need to implement the business logic of the increment() mutation.
Like for queries, you implement the increment() mutation as a resolver on the Mutation type:

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

let count = 0;

const schema = gql('
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment: CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
');

const resolvers = {
  Query: {
    getCount: () => ({ count, time: Date.now() })
  },
  // 'increment' is just a resolver for the Mutation type
  Mutation: {
    increment: () => ({ count: ++count, time: Date.now() })
  },
  CountResult: {
    count: obj => obj.count,
    time: obj => obj.time
  }
};

const server = new ApolloServer({ typeDefs: schema, resolvers });
const handle = await server.listen();

const axios = require('axios');
// Call the 'increment' mutation
await axios.post(handle.url, {
  query: 'mutation { increment { count, time } }'
});

// After the 'increment' mutation, 'count' is now 1
const { data } = await axios.post(handle.url, {
  query: '{ getCount { count, time } }'
});

data.data; // { getCount: { count: 1, time: 1581442587371 } }

Note that, to actually call a mutation, you need to start your GraphQL query with the string 'mutation':

await axios.post(handle.url, {
  // Note 'mutation' below. Not necessary for queries, but
  // necessary for mutations.
  query: 'mutation { increment { count, time } }'
});

Mutation Arguments for  GraphQL Mutations in Apollo

A GraphQL mutation is a function like any other. You can pass arguments to your mutation as well. For example, if you want to allow increment() with a value other than 1, you can add a Number parameter to the increment() mutation:

const schema = '
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment(num: Int): CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
';

Apollo passes the arguments passed in to your mutation as the 2nd parameter to your mutation’s resolver function:

increment: (obj, args) => {
  args.num; // Whatever the user passed in 'increment()'
}

Below is a full implementation of increment() with arguments:

let count = 0;

const schema = gql('
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment(num: Int!): CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
');

const resolvers = {
  Query: {
    getCount: () => ({ count, time: Date.now() })
  },
  // 'increment' is just a resolver for the Mutation type
  Mutation: {
    increment: (obj, args) => {
      count += args.num;
      return { count, time: Date.now() };
    }
  },
  CountResult: {
    count: obj => obj.count,
    time: obj => obj.time
  }
};

const server = new ApolloServer({ typeDefs: schema, resolvers });
const handle = await server.listen();

let axios = require('axios');
// Call the 'increment' mutation with an argument. Note that
// GraphQL arguments are named: you need to put 'num: 5', not
// just '5'.
await axios.post(handle.url, {
  query: 'mutation { increment(num: 5) { count, time } }'
});

// After the 'increment' mutation, 'count' is now 5
const { data } = await axios.post(handle.url, {
  query: '{ getCount { count, time } }'
});

data.data; // { getCount: { count: 5, time: 1581442587371 } }

More Graphql Tutorials for  GraphQL Mutations in Apollo

  • An Introduction to GraphQL with Apollo

GraphQL is a query language and runtime for application programming interfaces (APIs) that prioritizes giving clients exactly the data they request and no more. GraphQL is designed to make APIs fast, flexible, and developer-friendly.

Here are some of the benefits of using GraphQL:

Performance: GraphQL queries can be much more efficient than traditional REST APIs, as they only fetch the data that is actually needed. This can lead to significant performance improvements, especially for large datasets.

Flexibility: GraphQL allows clients to request data in a variety of ways, which gives developers more control over how their applications work. This can be helpful for building complex applications that need to access data from multiple sources.

Developer-friendliness: GraphQL is a relatively easy language to learn, and there are a number of tools and resources available to help developers get started. This makes it a good choice for both experienced and beginner developers.

If you are looking for a way to build a more efficient, flexible, and developer-friendly API, then GraphQL is a good option to consider.

Here are some examples of how GraphQL can be used:

A social media app could use GraphQL to fetch the latest posts from a user’s friends. The app could specify exactly which fields of data it needs from each post, such as the title, body, and author. This would allow the app to fetch only the data that it needs, which would improve performance.

An e-commerce website could use GraphQL to fetch product information for a specific product. The website could specify which fields of data it needs from the product, such as the name, price, and description. This would allow the website to fetch only the data that it needs, which would improve performance.

A gaming app could use GraphQL to fetch the current state of the game. The app could specify which fields of data it needs from the game state, such as the player’s position, health, and inventory. This would allow the app to fetch only the data that it needs, which would improve performance.

If you are interested in learning more about GraphQL, there are a number of resources available online. The official GraphQL website has a number of tutorials and documentation that can help you get started. There are also a number of third-party tools and libraries that can help you build GraphQL APIs.

Hope this code and post will helped you for implement GraphQL Mutations in Apollo. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US