Relay: Bulk insert into store

Created on 21 Dec 2017  路  9Comments  路  Source: facebook/relay

The Problem:

We are attempting to add local data to the store because we keep a local schema that allows us to have client only fields. We have been using commitLocalUpdate calls whenever we wanted to change some client only fields. We came across a case where we want to update a bunch of data, and not just a field at a time. When using the store API from something like commitLocalUpdate I would like to have the ability to set a query response to the store with the same ease of creating an optimisticResponse

My thoughts so far:

My first thought was that I could just do a regular mutation and intercept it in the network layer. This seems hacky.

Next, I thought, okay, how about giving commitLocalUpdate the same interface as normal mutations. This seems like forcing a square peg in a round hole.

Now I'm thinking, maybe there should be some kind of bulk insert API for the store. It would make the imperative store API so much more accessible to us mortals.

I would love to talk this through and would be happy to provide a pull request. I know how important encouraging the right usage of the framework is, so I would be happy to discuss the implications of this also.

wontfix

Most helpful comment

@brysgo do u have a repo example with these ideas?

All 9 comments

I'm curious to hear what kind of local data you have that's so big to put into the store? This is certainly a field I think we haven't explored enough yet. Maybe Relay could even cover some redux style use cases!

If you use true local schema extensions that are parsed by the Relay compiler, we currently can't do this easily.

The Relay compiler generates 2 types of generated files:

  • Fragments for reading the data of a single GraphQL fragment or query without traversing into spreaded fragments. This is used for example in fragment containers. These artifacts contain client fields, otherwise containers couldn't read them out.
  • Queries/Mutations/Subscriptions these contain all the embedded fragments and are then compressed. This is used when we want to traverse the full data to normalize it into the store. These artifacts do not contain client only fields as they're not returned from the server. For this reason, we cannot normalize them. (Actually just realized that this might be a bug with client only objects and garbage collection which also uses these artifacts for mark/sweep GC)

I use graphql's little known extend schema functionality to add my local stuff. Then I use something I wrote called graphql-mask to filter out local data from queries.

I'm not sure how most people do client only data, but for me I like to keep it all in relay.

@brysgo do u have a repo example with these ideas?

I'd be happy to throw one together to show how I'm doing local data.

Alright, just threw this togeather really quick so don't judge. Basically, it is an example of how I am using client schema extensions instead of something like redux so that my front end data can all operate through one model.

brysgo/relay-example

@brysgo it looks this repo is private yet

Sorry for the broken link. Thanks @hisapy for the assist.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings