Skip to content

Commit

Permalink
support @defer directive. (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf authored Apr 22, 2023
1 parent 3596b2e commit 25e09f9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions validator/prelude.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITIO
"The @specifiedBy built-in directive is used within the type system definition language to provide a scalar specification URL for specifying the behavior of custom scalar types."
directive @specifiedBy(url: String!) on SCALAR

"The @defer directive may be specified on a fragment spread to imply de-prioritization, that causes the fragment to be omitted in the initial response, and delivered as a subsequent response afterward. A query with @defer directive will cause the request to potentially return multiple responses, where non-deferred data is delivered in the initial response and data deferred delivered in a subsequent response. @include and @skip take precedence over @defer."
directive @defer(if: Boolean = true, label: String) on FRAGMENT_SPREAD | INLINE_FRAGMENT

type __Schema {
description: String
types: [__Type!]!
Expand Down
2 changes: 1 addition & 1 deletion validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, error) {
// scalars, it may (§3.13) define builtin directives. Here we check for
// that, and reject doubly-defined directives otherwise.
switch dir.Name {
case "include", "skip", "deprecated", "specifiedBy": // the builtins
case "include", "skip", "deprecated", "specifiedBy", "defer": // the builtins
// In principle here we might want to validate that the
// directives are the same. But they might not be, if the
// server has an older spec than we do. (Plus, validating this
Expand Down
5 changes: 5 additions & 0 deletions validator/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func TestLoadSchema(t *testing.T) {
require.Equal(t, "Boolean", boolDef.Name)
require.Equal(t, ast.Scalar, boolDef.Kind)
require.Equal(t, "The `Boolean` scalar type represents `true` or `false`.", boolDef.Description)

deferDef := s.Directives["defer"]
require.Equal(t, "defer", deferDef.Name, "@defer exists.")
require.Equal(t, "if", deferDef.Arguments[0].Name, "@defer has \"if\" argument.")
require.Equal(t, "label", deferDef.Arguments[1].Name, "@defer has \"label\" argument.")
})
t.Run("swapi", func(t *testing.T) {
file, err := os.ReadFile("testdata/swapi.graphql")
Expand Down

0 comments on commit 25e09f9

Please sign in to comment.