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.

[GraphQL] Can I sync Recoil state with GraphQL?

See original GitHub issue

What I am working on is a collaborative spreadsheet, where table cells between different views (view is the projection of a table, table cannot exist without views, a view may contain different cells as filtering may happen) will be shared, and changes between users on the same view will be shared. So I am looking for a state management tool, that serves as a intermediate layer between my GraphQL API and my UI components, that it can push the notification to the backend and receive the subscription from it, while also used as a synchronization tool for cells in different views.

What I have tried so far is I have created an atom family for all the cells in my table, those cells will for user be used for different views of that table. This is what I have tried, by creating the useCellValue class function, I wrapped not only the atom update but also the GraphQL update inside of it. It looks fine, so by calling the cellUpdater retrieved from that function, I can update not only the atom but also the backend.

type CellValueQuery = {workspaceId: string, tableId: string, recordId: string, fieldId: string}

export const cellValueStates = atomFamily<CellValue, CellValueQuery>({
  key: 'cell-value-states',
  default: null,
});

class Table {
    cellValueStates: (param: CellValueQuery) => RecoilState<CellValue>;

    useCellValue(cellValueQuery: CellValueQuery): [CellValue, SetterOrUpdater<CellValue>] {
      const [cellValue, setLocalCell] = useRecoilState(this.cellValueStates(cellValueQuery));

      const cellUpdater = (cellValue) => {
        const [setRemoteCell, { data }] = useMutation(UPDATE_CELL_MUTATION);
        setLocalCell(cellValue);
        setRemoteCell({variables: {workspaceId: cellValueQuery.workspaceId, tableId: cellValueQuery.tableId, 
          recordId: cellValueQuery.recordId, fieldId: cellValueQuery.fieldId, cellValue}
        })
      }

      return [cellValue, cellUpdater];
    }
}

So my questions are

  1. Since I have many of those cells, I realized that I may need to use the hook useCellValue I created in a for loop or whatever way, I know this is not really possible by design. How am I able to achieve that with recoil?
  2. Where can I subscribe to the changes from the GraphQL backend? I am wishing to do it in a centralized place like Table class where all the updates on atoms happened there and those changes can later be propagated to cells in views that are using that atom’s state.

Thanks a lot! As someone who is really new to frontend I am really looking for a best solution for my app and hopefully this can be elegantly done by Recoil!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
drarmstrcommented, Jan 5, 2021

Correct. But, using useRecoilState() to subscribe to local Recoil changes can be useful if you want to setup a bi-direction sync.

Atom Effects were developed to make it easier to setup and define this type of state synchronization. However, they currently don’t support React Hooks, which is problematic for working with GraphQL. We’re pursuing a few options, but the easiest for you now would be to use a component like this with effects to do the state synchronization.

1reaction
drarmstrcommented, Jun 2, 2022

We know have the recoil-relay library released for working with GraphQL and Rcoil.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recoil and GraphQL with Relay
It provides selectors which can easily query with GraphQL. The queries are synced with the Recoil data-flow graph so downstream selectors can derive...
Read more >
React Hooks, Recoil and Apollo GraphQL Client - Foo, software
State and data management is a complicated when it comes to React. This post illustrates a modern approach with Recoil and Apollo GraphQL...
Read more >
Recoil (@recoiljs) / Twitter
Want to sync Recoil state with the browser URL? The recoil-sync library has been ... Recoil can now interoperate with Relay for GraphQL....
Read more >
A quick glance at Recoil - Medium
It's been a while since I posted some articles about React state management ... We will use Todo app with GraphQL API backend...
Read more >
Querying Using GraphQL and Storing Data in Recoil - YouTube
Copy link. Info. Shopping. Tap to unmute. If playback doesn't begin shortly, try restarting your device. Your browser can 't play this video....
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