question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to load apollo graphQL data

See original GitHub issue

I’m trying to load user data from a graphql query, specifically react-apollo hooks. it doesn’t appear useEffect updates are captured. The state.set({…}) doesn’t seem to update; It could be that I’m not processing useQuery() results appropriately. Can you offer me help here? I’m sure apollo and graphql users would appreciate and demonstrate how this use works.

import {useQuery} from 'react-apollo';

function getLoggedUser() {
  const {data, loading} = useQuery(queries.GET_ME);
  const {me} = data || {};
  return {me, loading};
}

Here’s where I create the state

function useUserState( ) {
  const {me, loading} = getLoggedUser();

  const state = createState({
      userName: '', 
      userTitle: '',
   });

  useEffect(() => {

    const init = () => {
      state.set({...me});
    };

    if (me) {
      init();
    }

  }, [me]);

  return useState(state);
}

This is what have in my render function.

function renderUserState(){
   const  userState = useUserState()
   console.log( userState.get());   
}

// {useName:'', useTitle: ''}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
avkonstcommented, Aug 26, 2020

Hope it answers your questions. I have use apollo client with Hookstate. It was successful. Although I found later there are lighter GraphQL clients, which do the queries only and do not manage the state - this is what I needed, because the state was managed by Hookstate.

1reaction
avkonstcommented, Aug 26, 2020
  1. useQuery uses useState inside too. So you have got 2 state hooks and one effect hook to sync states. There is better alternative: do not use apollo hooks, just use apollo client to execute queries (do not remember function). The client will return you the Promise, which you can map to the desired state value and initialize hookstate state directly with it. See async states with Hookstate: https://hookstate.js.org/docs/asynchronous-state
Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries - Apollo GraphQL Docs
This article shows how to fetch GraphQL data in React with the useQuery hook and attach the result to your UI. You'll also...
Read more >
Fetching data with queries | Full-Stack Quickstart
Apollo Client provides a fetchMore helper function to assist with paginated queries. It enables you to execute the same query with different values...
Read more >
Queries - Apollo GraphQL Docs
This article demonstrates how to fetch GraphQL data in React with the useQuery hook and attach the result to your UI. You'll also...
Read more >
Connecting to data sources | Full-Stack Quickstart
Let's connect the SpaceX v2 REST API to our server. To do so, we'll use the RESTDataSource class from the apollo-datasource-rest package. This...
Read more >
Data sources - Apollo GraphQL Docs
Data sources are classes that Apollo Server can use to encapsulate fetching data from a particular source, such as a database or a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found