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.

Getting recoil state inside fetch-query function

See original GitHub issue

Im using react with relay… + Recoil for state management

In relay fetch query i check for cookies and if cookie with user_id change, im regenerating environment and i wanna get current state from recoil where global user id is stored (for needs to compare it with cookie )

But im not able to find way to get that out of hook… i need to check that cookies and get recoil state in all fetch respons… that is why im trying to put it in global fetch query…

What is correct way of accessing recoil state out of useRecoilState … out of using hook… ?

function fetchQuery(
  operation,
  variables,
  cacheConfig
) {
  const queryID = operation.text;
  const isMutation = operation.operationKind === "mutation";
  const isQuery = operation.operationKind === "query";
  const forceFetch = cacheConfig && cacheConfig.force;

  // Try to get data from cache on queries
  const fromCache = cache.get(queryID, variables);

  const [userStore, setUserStore] = useRecoilState(projectselector);

  function checkCookies() {
    const cookie_user_id = getUserIdFromCookie();

    if (cookie_user_id) {
      const value = parseInt(cookie_user_id);

      if (value !== 0 && userStore.user_id !== value) {
        setUserStore({ user_id: value });
        return true;
      }
    }
    return false;
  }

  if (isQuery && fromCache !== null && !forceFetch) {
    return fromCache;
  }

  // Otherwise, fetch data from server
  return fetch(BASE_GRAPHQL_URL, {
    credentials: "include",
    // mode: "cors",
    method: "POST",
    headers: {
      Accept: "application/json",
      // "Access-Control-Allow-Origin": "http://localhost:3000/",
      "Content-Type": "application/json",
    },

    body: JSON.stringify({
      query: operation.text,
      variables,
      operationName: operation.name,
    }),
  })
    .then((response) => {
      if (checkCookies()) {
        // todo
      }

      return response.json();
    })
    .then((json) => {
      // Update cache on queries
      if (isQuery && json) {
        cache.set(queryID, variables, json);
      }
      // Clear cache on mutations
      if (isMutation) {
        cache.clear();
      }

      return json;
    });
}

export function createEnvironment() {
  const network = Network.create(fetchQuery);

  const store = new Store(new RecordSource());

  return new Environment({
    network,
    store,
  });
}

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
drarmstrcommented, Sep 30, 2020

@drarmstr This question comes up a lot, perhaps there should be a page in the docs about sync’ing with state that lives outside of Recoil?

@BenjaBobs - There is this page in the docs discussing some state synchronization approaches. Though, I’m not a big fan of it. I’m planning to provide a new guide in the coming weeks based on the experimental API.

0reactions
drarmstrcommented, Jun 2, 2022

The recoil-relay library is now available for working with GraphQL and Recoil using Relay.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous Data Queries - Recoil
Recoil provides a way to map state and derived state to React components via a data-flow graph. What's really powerful is that the...
Read more >
Mastering data fetching with React Query and Next.js
Learn how React Query simplifies data fetching and caching for you and how it works in tandem with the Next.js pre-rendering methods.
Read more >
How to Use Recoil for State Management in Your React Projects
useRecoilValue is a hook provided by recoil that only returns the current state of date in the atom. We will use this hook...
Read more >
calling recoil inside a function - reactjs - Stack Overflow
But when i place LikeApi module (function) inside the button function, i get an error: import {LikeApi} from ".
Read more >
Syncing query parameters with react state - Inkoop
To achieve this, we will need a way to set and fetch query ... We will be using it inside our react components...
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