facebook / facebook/relay

Normalizing/Denormalizing data on demand

Open
#3,774 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Hi there 👋

let me provide some context:

we are using relay-runtime v12.0.0

Our environment implementation is similar to [this](https://github.com/vercel/next.js/blob/canary/examples/with-relay-modern/lib/relay.js)

The current call on the server looks like this, and we pass the data to the SSR-ed app via an element with an data-relay attribute
```js

const environment = initEnvironment({})

const queryProps = await fetchQuery_DEPRECATED(environment, Query, variables)
const initialRecords = environment.getStore().getSource().toJSON()

return {
queryProps,
initialRecords
}
```

now we've come to a situation where we need to use some of the data from that query on the server as well, but the data is normalized/masked and pretty hard to use outside react components.

I've tried to implement something like this:
```js
const environment = initEnvironment({})

const queryProps = await fetchQuery_DEPRECATED(environment, Query, variables)
const initialRecords = environment.getStore().getSource().toJSON()

const request = getRequest(Query)
const operation = createOperationDescriptor(request, variables)

const response = await environment.execute({ operation }).toPromise()
```

and yes, I get the response in an expected way, a simple graphql response but based on the duration of environment.execute (+100ms) it looks like it is doing a 2nd request instead of using the data obtained by fetchQuery. That extra time sadly isn't something that we can afford

I am wondering if something out of these 3 things are possible:
- there is a way to use just `environment.execute` and transform the normal graphql response into the needed initialRecords and queryProps?
- other way around: If there is way to get the standard graphql response by using initialRecords and queryProps?
- there is a way to make environment.execute use the data that is already fetched by fetchQuery?

Thank you

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.