onlinecode

React Apollo Hooks & Testing: useQuery with refetch and MockedProvider example

React Apollo Hooks & Testing: useQuery with refetch and MockedProvider example

In this post we will give you information about React Apollo Hooks & Testing: useQuery with refetch and MockedProvider example. Hear we will give you detail about React Apollo Hooks & Testing: useQuery with refetch and MockedProvider exampleAnd how to use it also give you demo for it if it is necessary.

In this example, we’ll see how to use the new useQuery hook available from the apollo react-hooks package to send GraphQL queries and receive response with fetched data from APIs.

We’ll also see how to test a React component that makes use of Apollo React hooks using MockedProvider available from @apollo/react-testing

The Apollo client provides two ways to send GraphQL queries:

  • Using the query method,
  • Using the useQuery React hook

The useQuery React hook allows you to pass a GraphQL query and will take care of fetching the data. automatically.

Please note that the useQuery hook is not a built-in React hook such as useState or useEffect hooks but a custom hook provided by the @apollo/react-hooks package

We assume that you already have a React application ready. Let’s also assume it’s called react-app.

Navigate inside the project’s folder and install the following libraries:

$ npm install graphql --save $ npm install graphql-tag --save$ npm install apollo-client --save $ npm install apollo-link-http --save $ npm install apollo-cache-inmemory --save $ npm install react-apollo --save

Next, open the src/index.js file and add the following code to set up Apollo and connect it to your component(s) as follows:

importReactfrom'react';importReactDOMfrom'react-dom';import'./index.css';importAppfrom'./App';import*asserviceWorkerfrom'./serviceWorker';import{ApolloClient}from'apollo-client';import{InMemoryCache}from'apollo-cache-inmemory';import{HttpLink}from'apollo-link-http';import{ApolloProvider}from'@apollo/react-hooks';constcache=newInMemoryCache();constlink=newHttpLink({uri:'https://graphql-pokemon.now.sh/'})constclient=newApolloClient({cache,link})ReactDOM.render(<ApolloProviderclient={client}><App/></ApolloProvider>, document.getElementById('root'));

Next, you can send GraphQL queries as follows:

importReactfrom'react';import{useQuery}from'@apollo/react-hooks';importgqlfrom"graphql-tag";exportconstQUERY=gql'{pokemons(first:150){idnumbername}}'functionApp(){const{data,loading,error,refetch}=useQuery(QUERY);if(loading)return<p>Loadingdata...</p>;if(error)return(<React.Fragment><p>Oops,error!</p><buttononClick={()=>refetch()}>Pleasetryagain!</button></React.Fragment>);console.log(data);return(<React.Fragment><h1>ReactApollouseQueryExample</h1><divclassName="container">{data&&data.pokemons&&data.pokemons.map((item,index)=>(<divkey={index}><p>{item.name}</p></div>))}</div></React.Fragment>);}exportdefaultApp;

Let’s explain the code! We first import the useQuery hook and gql tag (which parses a string into a GraphQL query).

Next, we defined a constant called QUERY which holds our GraphQL query.

Next, inside the App component, we call the useQuery hook with our example query which will take care of sending the query to the API and return a result object that has data and other information about the response.

Next, we destructure the result object into data, error, loading and refetch properties.

refetch is a function that can be used to re-send the GraphQL query in case of error.

Finally we do some conditional rendering depending on the values of the error and loading variables.

If data is done loading, we iterate over the data variable using the JavaScript map() method and display the name of each item.

When you call the useQuery React hook, Apollo returns the data along with other properties. Most importantly:

  • loading: A boolean that tells if the request is still not done. If loading equals true, then the request is not yet finished.
  • error: It has information about what went wrong with your query if there is an error.
  • data: An object that contains the result of the GraphQL query returned from the API server.

Testing the Component

Now how to test a React component that makes use of the useQuery hook?

We can use MockedProvider available from the @apollo/react-testing package as follows.

Open the src/App.test.js file and update as follows:

import{MockedProvider}from'@apollo/react-testing';import{act,render}from'@testing-library/react';importReactfrom'react';importApp,{QUERY}from'./App';constMOCKS=[{request:{query:QUERY,},result:{data:{pokemons:[{id:1,number:1name:'Pikatchu'}],},},},];asyncfunctionwait(ms=){awaitact(()=>{returnnewPromise(resolve=>{setTimeout(resolve,ms);});});}it('renders',async()=>{const{container}=render(<MockedProvideraddTypename={false}mocks={MOCKS}><App/></MockedProvider>);expect(container.textContent).toBe('Loading data...');awaitwait();expect(container.textContent).toMatch('React Apollo useQuery Example');expect(container.textContent).toMatch('Pikatchu');});

Conclusion

In this quick example, we’ve learned about the Apollo React useQuery hook and seen how to refetch data and how to test a React component that makes use of Apollo React hooks using the MockedProvider and React Testing Library


Hope this code and post will helped you for implement React Apollo Hooks & Testing: useQuery with refetch and MockedProvider example. 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

Exit mobile version