reactjsgraphqlapollo-client

can i use graphql in react without using Apollo?


I am trying to use the GraphQl with React. It is suggested to use the Apollo client for React to implement GraphQL in the application. But I don't want to use it in the first place.

Can I use GraphQL in React without using the Apollo client?


Solution

  • Yes. We can use GraphQL with React without using Apollo Client. We can use fetch or axios and can make things work the same.

    Below example demonstrates the usage of fetch to query a GraphQL endpoint.

    fetch(<graphql_endpoint>, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ query: '{ posts { title } }' }),
    })
    .then(res => res.json())
    .then(res => console.log(res.data));
    

    Same thing can be easily achieved using axios. Hope this helps!