Skip to content
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

[RFC] Alternative proposal for @stream/@defer #1018

Closed
wants to merge 22 commits into from
Closed

Conversation

benjie
Copy link
Member

@benjie benjie commented Mar 6, 2023

This RFC is an alternative solution to incremental delivery. This is a very early draft to enable sharing this solution with the incremental delivery working group, and, if ratified, may either be merged into @robrichard et al's excellent work in #742, or supersede it.

This PR currently only addresses the algorithms in the execution part of the specification (and is based off of a recent draft of the GraphQL specification, rather than any preceding work), so for accompanying specification text regarding the syntax and directives of incremental delivery, please see the excellent prior work in #742.

The goal of this proposal is to address the needs of incremental delivery whilst:

  1. Ensuring that the resolver at each path in the response is called at most once, to ensure that the individual payloads in an incremental delivery stream can always be reconciled into a final object that would be equivalent to the object that could be produced by removing all the @stream and @defer directives from the request.
  2. Ensuring that contents of a @defer'd or @stream'd selection set are sent atomically in a single event, such that MyFragment: __typename and other fragment identification approaches can be relied upon to confirm that the entire fragment is present.
  3. Avoiding duplicate delivery of leaves in order to reduce both network traffic, and memory load/redundant processing on client and server.
  4. Ensuring that you know if there is still pending data under a given path in the response.
  5. Allowing multiple streamed/deferred responses to be batched together into the same event to minimize client workload and reduce network traffic.

Non-goals of this proposal are:

  • Ensuring that you can check on the status of a specific @stream or @defer directives issued in the request.
    • There is generally not a one-to-one relationship between @stream/@defer directives in the request and any components in the response.
  • Allowing you to to set the priority of sibling @defers
    • There are no sibling defers, only nested defers
    • Currently ... @defer { ... @defer { a } } is equivalent to ... @defer { a }, but we might change this.

The significant change in this RFC is that it is built around the field merging algorithm that we already have, and allows merging @defers not just within a single selection set, but across the entire request. It works based on "defer layers" - thus for a query such as:

{
  a {
    b
    ... @defer { c { c1 } }
    d {
      e
      ... @defer { f }
    }
  }
  g
  ... @defer {
    a {
     c {
       ... @defer { c2 }
     }
    }
    h
  }
}

The first query to be resolved is as before:

{
  a {
    b
    d {
      e
    }
  }
  g
}

Yielding something like:

{
  data: {
    a: {
      b: "B",
      d: {
        e: "E"
      }
    },
    g: "G"
  },
  pending: [
    { id: 0, path: [] }
  ],
  hasNext: true
}

Next the first layer of @defer'd leaves is evaluated, which results in the following selection sets being evaluated at the following paths:

  • path: [], selection: { h }
  • path: ['a'], selection: { c { c1 } }
  • path: ['a', 'd'], selection: { f }

All three of these are evaluated (separately, in parallel) and then grouped together into the same event, something like:

{
  incremental: [
    { path: [], data: { h: "H" } },
    { path: ['a'], data: { c: { c1: "C1" } } },
    { path: ['a', 'd'], data: { f: "F" } }
  ],
  completed: [
    { id: 0 }
  ],
  pending: [
    { id: 1, path: ['a', 'c'] }
  ],
  hasNext: true
}

Finally we look at the next layer of @defer, which would yield:

  • path: ['a', 'c'], selection: { c2 }

Which would yield:

{
  incremental: [
    { path: ['a', 'c'], data: { c2: "C2" } }
  ],
  completed: [
    { id: 1 }
  ],
  hasNext: true
}

and finally:

{
  hasNext: false
}

(Note: we can probably optimize this to put the hasNext: false in the previous payload instead, but I've not yet tried to write that into the spec text.)

@netlify
Copy link

netlify bot commented Mar 6, 2023

Deploy Preview for graphql-spec-draft ready!

Name Link
🔨 Latest commit df84d1b
🔍 Latest deploy log https://app.netlify.com/sites/graphql-spec-draft/deploys/6411ff479ba52d0008baf0ee
😎 Deploy Preview https://deploy-preview-1018--graphql-spec-draft.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@benjie
Copy link
Member Author

benjie commented Apr 27, 2023

This PR is out of date versus what is currently being proposed. Closing.

@benjie benjie closed this Apr 27, 2023
@benjie benjie added the 💭 Strawman (RFC 0) RFC Stage 0 (See CONTRIBUTING.md) label Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💭 Strawman (RFC 0) RFC Stage 0 (See CONTRIBUTING.md)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant