How to fetch data from GraphQL in React

0 votes

How to fetch data from GraphQL in React

I'm trying to fetch data from a GraphQL API in a React application. Can someone help me with the best way to do this?

Dec 12, 2024 in Web Development by Nidhi
• 5,440 points
53 views

1 answer to this question.

0 votes

The best way to retrieve data from GraphQL in React is to use a library such as Apollo Client, which makes working with GraphQL easier.

This tutorial is simple and easy to follow:

1. Install Apollo Client and GraphQL

First, install the required dependencies:

npm install @apollo/client graphql

2. Set up Apollo Client

Create an Apollo Client instance and set it up in your React app. Typically, this is done in the src/index.js or src/App.js.

import React from 'react';

import ReactDOM from 'react-dom';

import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';

import App from './App';

// Create Apollo Client

const client = new ApolloClient({

  uri: 'https://my-graphql-endpoint.com/graphql', // Replace with your GraphQL endpoint

  cache: new InMemoryCache(),

});


ReactDOM.render(

  <ApolloProvider client={client}>

    <App />

  </ApolloProvider>,

  document.getElementById('root')

);

3. Fetch data using useQuery Hook

In your component, use the useQuery hook to fetch data.

import React from 'react';

import { useQuery, gql } from '@apollo/client';

// Define your query

const GET_DATA = gql`

  query {

    data {

      id

      name

    }

  }

`;

const DataComponent = () => {

  const { loading, error, data } = useQuery(GET_DATA);

  if (loading) return <p>Loading</p>;

  if (error) return <p>Error: {error.message}</p>;

  return (

    <div>

      <h2>Data Fetching:</h2>

      {data && data.map(item => (

        <div key={item.id}>

          <p>{item.name}</p>

        </div>

      ))}

    </div>

  );

};

export default DataComponent;

4. Run your React app

Make sure your React app is running. It will fetch the data from the GraphQL endpoint and display it accordingly.


 

answered Dec 12, 2024 by Navya

Related Questions In Web Development

0 votes
0 answers

How to filter JSON Data in JavaScript or jQuery?

How to filter JSON data using Javascript ...READ MORE

Jul 1, 2022 in Web Development by rajatha
• 7,680 points
2,521 views
0 votes
0 answers

How to show message in model body if bootstrap model have no data in jquery

I'm trying to display a text message ...READ MORE

Jul 29, 2022 in Web Development by gaurav
• 23,260 points
781 views
0 votes
2 answers

How to user Jquery's Plugin "Mondial relay" in react js properly?

Hi, have you found a solution? I'm facing ...READ MORE

answered Nov 30, 2022 in Web Development by Kévin
1,139 views
0 votes
0 answers

How to obtain data from random wikipedia page with jquery

$(".wikiResult h3").load("https://en.wikipedia.org/wiki/Special:Random #firstHeading i"); I would like to ...READ MORE

Aug 11, 2022 in Web Development by gaurav
• 23,260 points
554 views
0 votes
1 answer

How can I remove a port from url for node app using nginx

If you run your node server on ...READ MORE

answered Apr 10, 2018 in DevOps on Cloud by ajs3033
• 7,300 points
4,131 views
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
1,880 views
+2 votes
4 answers
0 votes
1 answer

How to integrate GraphQL queries in React components without performance issues?

Key Steps: Install Dependencies: Use Apollo Client, the ...READ MORE

answered Dec 13, 2024 in Web Development by Navya
34 views
0 votes
1 answer

How to change an uncontrolled input in React?

In React, uncontrolled components are those that ...READ MORE

answered Nov 19, 2024 in Web Development by kavya
94 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP