|
| 1 | +defmodule Absinthe.Type.BuiltIns.IncrementalDirectives do |
| 2 | + @moduledoc """ |
| 3 | + Draft-spec incremental delivery directives: @defer and @stream. |
| 4 | +
|
| 5 | + These directives are part of the [Incremental Delivery RFC](https://github.com/graphql/graphql-spec/blob/main/rfcs/DeferStream.md) |
| 6 | + and are not yet part of the finalized GraphQL specification. |
| 7 | +
|
| 8 | + ## Usage |
| 9 | +
|
| 10 | + To enable @defer and @stream in your schema, import this module: |
| 11 | +
|
| 12 | + defmodule MyApp.Schema do |
| 13 | + use Absinthe.Schema |
| 14 | +
|
| 15 | + import_types Absinthe.Type.BuiltIns.IncrementalDirectives |
| 16 | +
|
| 17 | + query do |
| 18 | + # ... |
| 19 | + end |
| 20 | + end |
| 21 | +
|
| 22 | + You will also need to enable incremental delivery in your pipeline: |
| 23 | +
|
| 24 | + pipeline_modifier = fn pipeline, _options -> |
| 25 | + Absinthe.Pipeline.Incremental.enable(pipeline, |
| 26 | + enabled: true, |
| 27 | + enable_defer: true, |
| 28 | + enable_stream: true |
| 29 | + ) |
| 30 | + end |
| 31 | +
|
| 32 | + Absinthe.run(query, MyApp.Schema, |
| 33 | + variables: variables, |
| 34 | + pipeline_modifier: pipeline_modifier |
| 35 | + ) |
| 36 | +
|
| 37 | + ## Directives |
| 38 | +
|
| 39 | + - `@defer` - Defers execution of a fragment spread or inline fragment |
| 40 | + - `@stream` - Streams list field items incrementally |
| 41 | + """ |
| 42 | + |
| 43 | + use Absinthe.Schema.Notation |
| 44 | + |
| 45 | + alias Absinthe.Blueprint |
| 46 | + |
| 47 | + directive :defer do |
| 48 | + description """ |
| 49 | + Directs the executor to defer this fragment spread or inline fragment, |
| 50 | + delivering it as part of a subsequent response. Used to improve latency |
| 51 | + for data that is not immediately required. |
| 52 | + """ |
| 53 | + |
| 54 | + repeatable false |
| 55 | + |
| 56 | + arg :if, :boolean, |
| 57 | + default_value: true, |
| 58 | + description: "When true, fragment may be deferred. When false, fragment will not be deferred and data will be included in the initial response. Defaults to true." |
| 59 | + |
| 60 | + arg :label, :string, |
| 61 | + description: "A unique label for this deferred fragment, used to identify it in the incremental response." |
| 62 | + |
| 63 | + on [:fragment_spread, :inline_fragment] |
| 64 | + |
| 65 | + expand fn |
| 66 | + %{if: false}, node -> |
| 67 | + # Don't defer when if: false |
| 68 | + node |
| 69 | + |
| 70 | + args, node -> |
| 71 | + # Mark node for deferred execution |
| 72 | + defer_config = %{ |
| 73 | + label: Map.get(args, :label), |
| 74 | + enabled: true |
| 75 | + } |
| 76 | + Blueprint.put_flag(node, :defer, defer_config) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + directive :stream do |
| 81 | + description """ |
| 82 | + Directs the executor to stream list fields, delivering list items incrementally |
| 83 | + in multiple responses. Used to improve latency for large lists. |
| 84 | + """ |
| 85 | + |
| 86 | + repeatable false |
| 87 | + |
| 88 | + arg :if, :boolean, |
| 89 | + default_value: true, |
| 90 | + description: "When true, list field may be streamed. When false, list will not be streamed and all data will be included in the initial response. Defaults to true." |
| 91 | + |
| 92 | + arg :label, :string, |
| 93 | + description: "A unique label for this streamed field, used to identify it in the incremental response." |
| 94 | + |
| 95 | + arg :initial_count, :integer, |
| 96 | + default_value: 0, |
| 97 | + description: "The number of list items to return in the initial response. Defaults to 0." |
| 98 | + |
| 99 | + on [:field] |
| 100 | + |
| 101 | + expand fn |
| 102 | + %{if: false}, node -> |
| 103 | + # Don't stream when if: false |
| 104 | + node |
| 105 | + |
| 106 | + args, node -> |
| 107 | + # Mark node for streaming execution |
| 108 | + stream_config = %{ |
| 109 | + label: Map.get(args, :label), |
| 110 | + initial_count: Map.get(args, :initial_count, 0), |
| 111 | + enabled: true |
| 112 | + } |
| 113 | + Blueprint.put_flag(node, :stream, stream_config) |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments