facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.37k stars 1.82k forks source link

No Documentation for `deferred Fragments` or `@defer` #4438

Open reckter opened 1 year ago

reckter commented 1 year ago

In the Documentation here https://relay.dev/docs/tutorial/queries-2/ , if you expand the "Deep dive: When to use a secondary query" (nice addition btw!), it mentions "deferred Fragments" and that they will be explained "later".

But I couldn't find any explanation on the topic anywhere else :/ There is a Glossary entry for @defer, but it does not explain anything really.

The relay compiler also doesn't recognise @defer out of the box.

What is the state of the deferred fragments feature?

sibelius commented 1 year ago

enable in your graphql server, and it should work

michael-haberzettel commented 1 year ago

I recently struggled with the same question!

@defer is a directive that must be declared in schema to be used. To be more precise, the server must handle this directive to defer some parts of the reponse. Relay addresses the subject in its latest video published here: https://relay.dev/docs/principles-and-architecture/videos/

At this moment, few graphql servers are able to manage this directive nativally. If you can enable it in your server, the directive will be declared automatically in the schema.

Resources:

reckter commented 1 year ago

A thanks @sibelius @michael-haberzettel ! Enabling in in our server (yoga), was pretty easy.

But I couldn't get it to actually work in the frontend with relay. No combination of meros, fetch-multipart-grahpql, relay-modern-middleware, seems to work for me :/

The furthest I got was that non-defer queries work normaly, but it seemed like defered queries (1) didn't correctly forward the first part of the response and (2) relay didn't understand the "incremental" property? (but even translating between the two didn't get me further, "no data in response" errors, almost everywhere).

Do you happen to have any more pointers? Or maybe even a working example you can point me to?

The video sadly is missing all the technical details to actually get it to work :/

michael-haberzettel commented 1 year ago

I'm stuck with my graphql server that can't handle these directives... so I can't help you :cry:

jakobkhansen commented 8 months ago

@reckter did you ever get anywhere with this? Is it possible to use defer with Relay at the moment?

reckter commented 8 months ago

@jakobkhansen I gave up, when I discovered that one of our core backend technologies wouldn't work with delayed queries (neo4j-graphql). iirc I managed to get it to work in relay somehow, at least I remember seeing 2 responses comming back. I can't remember if that rendered correctly or not though. sorry.

alessbell commented 3 months ago

Thought I'd share a demo of Relay using @defer with an Apollo Router back-end: https://apollographql.github.io/graphql-testing-library/?path=/docs/example-relay--docs

My fetchQuery implementation: https://github.com/apollographql/graphql-testing-library/blob/main/.storybook/stories/components/relay/relay-environment.ts