Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Apollo GraphQL Hooks - Creating Mutations

    nader dabitnader dabit

    In this video you'll learn how to create a new Apollo client in a React application & send mutations to a GraphQL API using the Apollo GraphQL Hooks library.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: In this video, we'll be looking at how to set up the Apollo client, use the useQuery hook, and then use the useMutation hook to send mutations to a GraphQL API and trigger our refetchQuery.

    If you've already seen the video on how to set up the Apollo client and use the useQuery hook, you can fast forward to the 5 minute and 45 second mark.

    We'll start off by creating a new React application called apollohooksapp.

    Next, we'll install the dependencies. We need to install apollo-client, apollo-cache-inmemory, apollo-link-context, apollo-link-http, graphql, graphql-tag, and the new @apollo/react-hooks@beta.

    If you're watching this video in the Apollo, React Hooks library is out of beta. You can just install @apollo/react-hooks.

    After everything has been installed, we'll open the application in our text editor. The first thing we'll do is open index.js and import our dependencies. Here, we'll be importing ApolloClient, InMemoryCache, setContext, createHttpLink, and ApolloProvider.

    Next, we'll create a new Http link by calling createHttpLink and passing in the URI of our GraphQL API.

    If you need to send headers to your GraphQL endpoint, create an authLink and set your header properties. Next, we'll create our client variable by calling new ApolloClient and passing in the link and the cache property as new InMemoryCache.

    If you do not have an authLink, you can just pass the HttpLink as the link property. Finally, we'll create an Apollo app component by passing in the client as the client into ApolloProvider.

    React.DOM.render will replace the app component with our new Apollo app component. Next, we'll open app.js and import gql from GraphQL tag and use query from addApollo/react-hooks.

    Next, we'll create a variable called query to place our GraphQL query. In the app function, we'll use the useQuery hook passing in the query to return the data and the loading state from the query.

    We'll check to see if the data is loading. If it is, we'll return a message that lets the user know that the data is loading. Finally, we'll map over the items array returned from the query, returning the item name and item description.

    Next, we'll run npm start to launch our app. When the app loads, we should see our items rendered to the screen. When we refresh, we should see a loading message. Now that we have queries working, we'll go back into app.js and import the useState hook from React, and the useMutation hook from apollo/react-hooks.

    Next, we'll define our GraphQL mutation in the variable called mutation. We'll be creating a form. We'll need a way to hold the values from the form input.

    To do so, we'll create two new pieces of state, one called name and one called description. Next, we'll set up the useMutation hook. The return value from the useMutation hook that will be using is going to be a function called createTaco and an error to check to see if there are any errors in our mutation.

    For the first argument to useMutation, we'll pass in our mutation definition. The second argument to useMutation will be the variables that we're passing into the mutation. The third argument to useMutation will be the refetchQueries that we'd like call after the mutation has been successful.

    Next, we'll check to see if there're any errors. If there are, we'll log them out. Finally, we'll create our form with two inputs and a button that when clicked will call createTaco.

    Next, we'll launch the app by running npm start. If we try to send the mutation with an empty input value, we'll notice that the GraphQL error has been logged out to the console.