lgants
lgants

Reputation: 3835

How to Setup Local Store with Static Data in Relay Modern

I'm building a React app that fetches data from a server and also passes static data to various components based on a user's selection from a dropdown menu. I'd like to use Relay Modern, but I've been unable to find anything in the docs about whether it supports manually loading static data to the store. Anyone know if this is possible/how to implement?

btw, I've seen a few similar questions about this here and elsewhere. But, it appeared that those pertained to Relay Classic rather than Relay Modern, which implemented massive changes.

Upvotes: 1

Views: 419

Answers (1)

celticpride
celticpride

Reputation: 516

I have have asked myself the same question. There must be a way to hydrate the Store/RecordSource. As a workaround I have been doing something like this. I create a query for the data that I want to add to the store and call commitPayload.

import {
  createOperationSelector,
  getOperation
} from 'relay-runtime';

const query = graphql `{ viewer { name } }`;

const environment = new Environment(...);

environment.commitPayload(createOperationSelector(getOperation(query)), {
  viewer: {
    name: 'Me'
  }
});

Wondering if anyone from the relay team has some insight?

Upvotes: 1

Related Questions