From 25e09f9d292d73a3a63d61836e239d51c5707fdb Mon Sep 17 00:00:00 2001 From: fiatjaf_ Date: Sat, 22 Apr 2023 19:16:42 -0300 Subject: [PATCH] support @defer directive. (#255) --- validator/prelude.graphql | 3 +++ validator/schema.go | 2 +- validator/schema_test.go | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/validator/prelude.graphql b/validator/prelude.graphql index bdca0096..e199da5d 100644 --- a/validator/prelude.graphql +++ b/validator/prelude.graphql @@ -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!]! diff --git a/validator/schema.go b/validator/schema.go index 5e8bace7..e7c661d1 100644 --- a/validator/schema.go +++ b/validator/schema.go @@ -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 diff --git a/validator/schema_test.go b/validator/schema_test.go index c5f88fc1..e7ed5cde 100644 --- a/validator/schema_test.go +++ b/validator/schema_test.go @@ -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")