-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relay and NextJS #3889
Comments
Relay team makes an example here using next and relay https://github.com/relayjs/relay-examples/tree/main/data-driven-dependencies I'm basing myself on this to implement in my own project with nextjs. It would be nice to have more examples besides the preloaded queries |
Thanks that's a great start 👍 Unfortunately similar like the previous solutions it also looks like a POC which is still WIP with open TODOs but still I like the simplicity of the SSR <-> Hydration connection: Especially the page part is very explicit and gives full control over which parts will be preloaded: Maybe some parts of the relay helper could be published as a stand alone nextjs bridge. Code like Most teams of larger NextApps use TypeScript so I guess it would also be nice to have a clean NextJs + Relay + Typescript example - maybe right inside https://github.com/vercel/next.js/tree/canary/examples/ The part about data driven lazy loaded dependencies is mind blowing but maybe a little bit too much for a getting started example |
Yeah I think there's not many good examples of how to use relay with nextjs. @alunyov and I created https://github.com/relayjs/relay-examples/tree/main/data-driven-dependencies in order to make some progress towards changing that. Well... that partly started as an attempt to make data-driven-dependencies, an advanced feature of relay we use at Meta internally, work in open source... I agree that there's room for simpler examples to exist. It would make it really happy if we had more examples and documentation for using relay with nextjs (and perhaps other popular React app framework / setups too). Unfortunately creating such examples isn't a priority for the relay team at present. That said, if anyone is interested to help with creating examples and/or improving docs... please let us know... I'm interested in collaborating and supporting others in making such contributions... and perhaps some other members of the relay team might be as well! |
I tried to extract the core from https://github.com/relayjs/relay-examples/tree/main/data-driven-dependencies. The first draft is here https://github.com/jantimon/next-relay-demo It is using Some parts of data-driven-dependencies caused typescript errors: It also has the todo from data-driven-dependencies: One big question is the combination of the relay haste naming convention and the next folder based router.
Overall I really like the approach you chose for https://github.com/relayjs/relay-examples/tree/main/data-driven-dependencies as it allows to combine relay with SSG / SSR and client site data fetching 👍 |
Nice! I'll try to take a closer look at some point. I haven't used TypeScript much and want to play with it more (actually I'd attempted to introduce typescript into relay-examples/data-driven-dependencies but abandoned it after silencing a lot of errors).
Ah, yeah. The response cache is handy for enabling preloading of results but not well integrated in the public API. https://github.com/relayjs/relay-examples/blob/3499f7939e68a2a3a67d6d01cfa7459de43e89ff/data-driven-dependencies/lib/relay/environment.js#L31 (i.e. what you have at https://github.com/jantimon/next-relay-demo/blob/682de424013437dfb20b5fbd67b51e98f175d627/data/relayEnvironment.tsx#L21-L22) I did it this way because I couldn't attach the cache to the result of
But then accessing it later using
iirc I left the todo in there because I want to rethink I was thinking that instead of a response cache that uses (query id + variables) as cache key + ttl, we could make it so that there's the server can construct a
Yeah that's really awkward. I wanna improve that too. @tbezman is collaborating with us to make various relay features work well in non-haste environments. I don't think we've discussed making changes to the naming conventions that relay enforces (or have we, @captbaritone?) I think it would be great if we could give control to consumers to define their own naming conventions via the config. I'm not sure what's the best way to do that though. Maybe something similar to
Thanks! It took me something like 5 rewrites to figure out how to make Relay and Next's SSG and SSR play nice while keeping things relatively simple 😛. I'm quite pleased how it shaped up after all the rewrites. Would you be interested in exploring any of the ideas I mentioned above? |
I had bumped my version of nextjs to 12.1.6 and am seeing this error when adding a tagged template literal graphql mutation: |
@Choongkyu This is most likely because you're missing either |
@Choongkyu , what @petrbela sounds about right to me. If that doesn't work, maybe you can post some example minimum repro and I can check it out |
thank you. My
I'm not exactly sure how to repro the issue but walking away from it and returning to it somehow resolved it. Thank you. |
@tbezman @Choongkyu NextJs switched from BabelJS to SWC. The build-in Relay support in NextJs is only for SWC. This issue is about finding the best integration between the latest NextJs and Relay versions. If you are still using BabelJs this solution might not work for you.
I would love to work further on the integration but I would need some guidance. |
Is there a more up to date version of an example for integration Next.js with Relay? |
@punkpeye https://github.com/soneymathew/relay-3d-example is a WIP that may be of some help.
I agree @jantimon 3D is pretty exciting, this example showcases 3D feature of relay client Used https://github.com/relayjs/relay-examples/tree/main/data-driven-dependencies as a base for this... just barely managed to convert it all to Typescript 😅 |
I guess most of this discussion is outdated now that Next.JS 13 has been released. |
Maybe you saw it already that every NextJS project ships now with Relay support by default!
https://nextjs.org/blog/next-12-1
Basically it means that the swc version of the
babel-plugin-relay
is built in and can be configured directly in the next.js config:NextJS does not provide a concept for splitting data loading into components. (beside from waterfall loading)
For me this makes Relay a perfect match for many medium-sized to large NextJS websites and applications.
NextJS got incredibly popular for its simplicity - unfortunately setting up Relay for NextJS is really hard.
It has been tried several times but most tries got abandoned after the POC phase:
environment.getStore().getSource().toJSON()
...)I believe this could be done better and I believe that if someone from the Relay team could invest time here to connect the power of Relays typesafe modular data loading with NextJs it might be a big push for Relays popularity and could help the entire ecosystem.
The text was updated successfully, but these errors were encountered: