gauravtiwari / relay-rails-blog

A graphql, relay and standard rails application powered demo weblog. We are using Graphql server and relay for our react component data needs.
https://relay-rails-blog.herokuapp.com/
139 stars 18 forks source link

Is it possible to have server-side rendering? #11

Open mongjong59 opened 7 years ago

mongjong59 commented 7 years ago

I tried to set up server-side rendering with isomorphic-relay. But I got stuck because it seems that with ExecJS (with Node.js backend), Promises can't be consumed. I tried a very basic Promise in my server startup file.

var promise = Promise.resolve("foobar");
var response = promise.then((res) => { console.log(res) }, (e) => {console.log(e)})
console.log(response)

The above code will log [Object Promise]. But normally it should be foobar and {}. I didn't find much about whether Promise are supported in ExecJS. But this is obviously a key in server-side rendering with a GraphQL client. Do you have any idea on this issue?

gauravtiwari commented 7 years ago

Hey @nostophilia, I haven't tried server side rendering with relay yet. You can checkout this repo, https://github.com/brandfolder/rails-graphql-relay (looks outdated though). I will look into this too.

mongjong59 commented 7 years ago

@gauravtiwari Seems we can get it work by means of React on Rails Node.js SSR (I pushed a fix on the server file, merged with v6.5.0).

However it doesn't seem an ideal way as it means extra Rails call stack which is not necessary. It would be great if we can:

  1. extract GraphQL queries for the app on Rails side
  2. Schema.execute(query)
  3. hydrate Relay/Apollo store
  4. render the app

This seems more feasible with Relay as the code of Isomorphic React Relay implies while the getQueriesFromTree of Apollo doesn't seem to return root queries with all sub-queries. Do you have idea whether there's an API for extracting root queries in Relay or Apollo and do they both support store hydration?