Allow directive on variable definition - [GraphQL - October 2021] #678

This commit is contained in:
Sunli 2021-10-28 16:02:51 +08:00
parent c6d26884a9
commit e9624f8093
9 changed files with 48 additions and 34 deletions

View File

@ -10,7 +10,7 @@ executable_definition = { operation_definition | fragment_definition }
operation_definition = { named_operation_definition | selection_set } operation_definition = { named_operation_definition | selection_set }
named_operation_definition = { operation_type ~ name? ~ variable_definitions? ~ directives? ~ selection_set } named_operation_definition = { operation_type ~ name? ~ variable_definitions? ~ directives? ~ selection_set }
variable_definitions = { "(" ~ variable_definition* ~ ")" } variable_definitions = { "(" ~ variable_definition* ~ ")" }
variable_definition = { variable ~ ":" ~ type_ ~ default_value? } variable_definition = { variable ~ ":" ~ type_ ~ directives? ~ default_value? }
selection_set = { "{" ~ selection+ ~ "}" } selection_set = { "{" ~ selection+ ~ "}" }
selection = { field | inline_fragment | fragment_spread } selection = { field | inline_fragment | fragment_spread }
@ -86,6 +86,7 @@ directive_location = {
| "FRAGMENT_DEFINITION" | "FRAGMENT_DEFINITION"
| "FRAGMENT_SPREAD" | "FRAGMENT_SPREAD"
| "INLINE_FRAGMENT" | "INLINE_FRAGMENT"
| "VARIABLE_DEFINITION"
| "SCHEMA" | "SCHEMA"
| "SCALAR" | "SCALAR"
| "OBJECT" | "OBJECT"

View File

@ -208,6 +208,8 @@ fn parse_variable_definition(
let variable = parse_variable(pairs.next().unwrap(), pc)?; let variable = parse_variable(pairs.next().unwrap(), pc)?;
let var_type = parse_type(pairs.next().unwrap(), pc)?; let var_type = parse_type(pairs.next().unwrap(), pc)?;
let directives = parse_opt_directives(&mut pairs, pc)?;
let default_value = parse_if_rule(&mut pairs, Rule::default_value, |pair| { let default_value = parse_if_rule(&mut pairs, Rule::default_value, |pair| {
parse_default_value(pair, pc) parse_default_value(pair, pc)
})?; })?;
@ -218,6 +220,7 @@ fn parse_variable_definition(
VariableDefinition { VariableDefinition {
name: variable, name: variable,
var_type, var_type,
directives,
default_value, default_value,
}, },
pos, pos,

View File

@ -323,6 +323,7 @@ fn parse_directive_definition(
"FRAGMENT_DEFINITION" => DirectiveLocation::FragmentDefinition, "FRAGMENT_DEFINITION" => DirectiveLocation::FragmentDefinition,
"FRAGMENT_SPREAD" => DirectiveLocation::FragmentSpread, "FRAGMENT_SPREAD" => DirectiveLocation::FragmentSpread,
"INLINE_FRAGMENT" => DirectiveLocation::InlineFragment, "INLINE_FRAGMENT" => DirectiveLocation::InlineFragment,
"VARIABLE_DEFINITION" => DirectiveLocation::VariableDefinition,
"SCHEMA" => DirectiveLocation::Schema, "SCHEMA" => DirectiveLocation::Schema,
"SCALAR" => DirectiveLocation::Scalar, "SCALAR" => DirectiveLocation::Scalar,
"OBJECT" => DirectiveLocation::Object, "OBJECT" => DirectiveLocation::Object,

View File

@ -10,7 +10,7 @@ use std::str::Chars;
/// Original position of an element in source code. /// Original position of an element in source code.
/// ///
/// You can serialize and deserialize it to the GraphQL `locations` format /// You can serialize and deserialize it to the GraphQL `locations` format
/// ([reference](https://spec.graphql.org/June2018/#sec-Errors)). /// ([reference](https://spec.graphql.org/October2021/#sec-Errors)).
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Default, Hash, Serialize, Deserialize)] #[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Default, Hash, Serialize, Deserialize)]
pub struct Pos { pub struct Pos {
/// One-based line number. /// One-based line number.

View File

@ -5,7 +5,7 @@ use async_graphql_value::{ConstValue, Name, Value};
/// An executable GraphQL file or request string. /// An executable GraphQL file or request string.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#ExecutableDocument). /// [Reference](https://spec.graphql.org/October2021/#ExecutableDocument).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ExecutableDocument { pub struct ExecutableDocument {
/// The operations of the document. /// The operations of the document.
@ -93,7 +93,7 @@ enum OperationsIterInner<'a> {
/// A GraphQL operation, such as `mutation($content:String!) { makePost(content: $content) { id } }`. /// A GraphQL operation, such as `mutation($content:String!) { makePost(content: $content) { id } }`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#OperationDefinition). /// [Reference](https://spec.graphql.org/October2021/#OperationDefinition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct OperationDefinition { pub struct OperationDefinition {
/// The type of operation. /// The type of operation.
@ -108,13 +108,15 @@ pub struct OperationDefinition {
/// A variable definition inside a list of variable definitions, for example `$name:String!`. /// A variable definition inside a list of variable definitions, for example `$name:String!`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#VariableDefinition). /// [Reference](https://spec.graphql.org/October2021/#VariableDefinition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct VariableDefinition { pub struct VariableDefinition {
/// The name of the variable, without the preceding `$`. /// The name of the variable, without the preceding `$`.
pub name: Positioned<Name>, pub name: Positioned<Name>,
/// The type of the variable. /// The type of the variable.
pub var_type: Positioned<Type>, pub var_type: Positioned<Type>,
/// The variable's directives.
pub directives: Vec<Positioned<Directive>>,
/// The optional default value of the variable. /// The optional default value of the variable.
pub default_value: Option<Positioned<ConstValue>>, pub default_value: Option<Positioned<ConstValue>>,
} }
@ -139,7 +141,7 @@ impl VariableDefinition {
/// A set of fields to be selected, for example `{ name age }`. /// A set of fields to be selected, for example `{ name age }`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#SelectionSet). /// [Reference](https://spec.graphql.org/October2021/#SelectionSet).
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct SelectionSet { pub struct SelectionSet {
/// The fields to be selected. /// The fields to be selected.
@ -148,7 +150,7 @@ pub struct SelectionSet {
/// A part of an object to be selected; a single field, a fragment spread or an inline fragment. /// A part of an object to be selected; a single field, a fragment spread or an inline fragment.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#Selection). /// [Reference](https://spec.graphql.org/October2021/#Selection).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Selection { pub enum Selection {
/// Select a single field, such as `name` or `weightKilos: weight(unit: KILOGRAMS)`. /// Select a single field, such as `name` or `weightKilos: weight(unit: KILOGRAMS)`.
@ -182,7 +184,7 @@ impl Selection {
/// A field being selected on an object, such as `name` or `weightKilos: weight(unit: KILOGRAMS)`. /// A field being selected on an object, such as `name` or `weightKilos: weight(unit: KILOGRAMS)`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#Field). /// [Reference](https://spec.graphql.org/October2021/#Field).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Field { pub struct Field {
/// The optional field alias. /// The optional field alias.
@ -217,7 +219,7 @@ impl Field {
/// A fragment selector, such as `... userFields`. /// A fragment selector, such as `... userFields`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#FragmentSpread). /// [Reference](https://spec.graphql.org/October2021/#FragmentSpread).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FragmentSpread { pub struct FragmentSpread {
/// The name of the fragment being selected. /// The name of the fragment being selected.
@ -228,7 +230,7 @@ pub struct FragmentSpread {
/// An inline fragment selector, such as `... on User { name }`. /// An inline fragment selector, such as `... on User { name }`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#InlineFragment). /// [Reference](https://spec.graphql.org/October2021/#InlineFragment).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct InlineFragment { pub struct InlineFragment {
/// The type condition. /// The type condition.
@ -241,7 +243,7 @@ pub struct InlineFragment {
/// The definition of a fragment, such as `fragment userFields on User { name age }`. /// The definition of a fragment, such as `fragment userFields on User { name age }`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#FragmentDefinition). /// [Reference](https://spec.graphql.org/October2021/#FragmentDefinition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FragmentDefinition { pub struct FragmentDefinition {
/// The type this fragment operates on. /// The type this fragment operates on.
@ -254,7 +256,7 @@ pub struct FragmentDefinition {
/// A type a fragment can apply to (`on` followed by the type). /// A type a fragment can apply to (`on` followed by the type).
/// ///
/// [Reference](https://spec.graphql.org/June2018/#TypeCondition). /// [Reference](https://spec.graphql.org/October2021/#TypeCondition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TypeCondition { pub struct TypeCondition {
/// The type this fragment applies to. /// The type this fragment applies to.

View File

@ -4,7 +4,7 @@
//! [`ServiceDocument`](struct.ServiceDocument.html), representing an executable GraphQL query and a //! [`ServiceDocument`](struct.ServiceDocument.html), representing an executable GraphQL query and a
//! GraphQL service respectively. //! GraphQL service respectively.
//! //!
//! This follows the [June 2018 edition of the GraphQL spec](https://spec.graphql.org/June2018/). //! This follows the [June 2018 edition of the GraphQL spec](https://spec.graphql.org/October2021/).
mod executable; mod executable;
mod service; mod service;
@ -19,7 +19,7 @@ pub use service::*;
/// The type of an operation; `query`, `mutation` or `subscription`. /// The type of an operation; `query`, `mutation` or `subscription`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#OperationType). /// [Reference](https://spec.graphql.org/October2021/#OperationType).
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum OperationType { pub enum OperationType {
/// A query. /// A query.
@ -42,7 +42,7 @@ impl Display for OperationType {
/// A GraphQL type, for example `String` or `[String!]!`. /// A GraphQL type, for example `String` or `[String!]!`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#Type). /// [Reference](https://spec.graphql.org/October2021/#Type).
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub struct Type { pub struct Type {
/// The base type. /// The base type.
@ -105,7 +105,7 @@ impl Display for BaseType {
/// from [`Directive`](struct.Directive.html) in that it uses [`ConstValue`](enum.ConstValue.html) /// from [`Directive`](struct.Directive.html) in that it uses [`ConstValue`](enum.ConstValue.html)
/// instead of [`Value`](enum.Value.html). /// instead of [`Value`](enum.Value.html).
/// ///
/// [Reference](https://spec.graphql.org/June2018/#Directive). /// [Reference](https://spec.graphql.org/October2021/#Directive).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ConstDirective { pub struct ConstDirective {
/// The name of the directive. /// The name of the directive.
@ -140,7 +140,7 @@ impl ConstDirective {
/// A GraphQL directive, such as `@deprecated(reason: "Use the other field")`. /// A GraphQL directive, such as `@deprecated(reason: "Use the other field")`.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#Directive). /// [Reference](https://spec.graphql.org/October2021/#Directive).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Directive { pub struct Directive {
/// The name of the directive. /// The name of the directive.

View File

@ -5,7 +5,7 @@ use async_graphql_value::Name;
/// A GraphQL file or request string defining a GraphQL service. /// A GraphQL file or request string defining a GraphQL service.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#Document). /// [Reference](https://spec.graphql.org/October2021/#Document).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ServiceDocument { pub struct ServiceDocument {
/// The definitions of this document. /// The definitions of this document.
@ -14,8 +14,8 @@ pub struct ServiceDocument {
/// A definition concerning the type system of a GraphQL service. /// A definition concerning the type system of a GraphQL service.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#TypeSystemDefinition). This enum also covers /// [Reference](https://spec.graphql.org/October2021/#TypeSystemDefinition). This enum also covers
/// [extensions](https://spec.graphql.org/June2018/#TypeSystemExtension). /// [extensions](https://spec.graphql.org/October2021/#TypeSystemExtension).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TypeSystemDefinition { pub enum TypeSystemDefinition {
/// The definition of the schema of the service. /// The definition of the schema of the service.
@ -28,8 +28,8 @@ pub enum TypeSystemDefinition {
/// The definition of the schema in a GraphQL service. /// The definition of the schema in a GraphQL service.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#SchemaDefinition). This also covers /// [Reference](https://spec.graphql.org/October2021/#SchemaDefinition). This also covers
/// [extensions](https://spec.graphql.org/June2018/#SchemaExtension). /// [extensions](https://spec.graphql.org/October2021/#SchemaExtension).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SchemaDefinition { pub struct SchemaDefinition {
/// Whether the schema is an extension of another schema. /// Whether the schema is an extension of another schema.
@ -46,8 +46,8 @@ pub struct SchemaDefinition {
/// The definition of a type in a GraphQL service. /// The definition of a type in a GraphQL service.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#TypeDefinition). This also covers /// [Reference](https://spec.graphql.org/October2021/#TypeDefinition). This also covers
/// [extensions](https://spec.graphql.org/June2018/#TypeExtension). /// [extensions](https://spec.graphql.org/October2021/#TypeExtension).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TypeDefinition { pub struct TypeDefinition {
/// Whether the type is an extension of another type. /// Whether the type is an extension of another type.
@ -81,7 +81,7 @@ pub enum TypeKind {
/// The definition of an object type. /// The definition of an object type.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#ObjectType). /// [Reference](https://spec.graphql.org/October2021/#ObjectType).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ObjectType { pub struct ObjectType {
/// The interfaces implemented by the object. /// The interfaces implemented by the object.
@ -92,7 +92,7 @@ pub struct ObjectType {
/// The definition of a field inside an object or interface. /// The definition of a field inside an object or interface.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#FieldDefinition). /// [Reference](https://spec.graphql.org/October2021/#FieldDefinition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FieldDefinition { pub struct FieldDefinition {
/// The description of the field. /// The description of the field.
@ -109,7 +109,7 @@ pub struct FieldDefinition {
/// The definition of an interface type. /// The definition of an interface type.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#InterfaceType). /// [Reference](https://spec.graphql.org/October2021/#InterfaceType).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct InterfaceType { pub struct InterfaceType {
/// The interfaces implemented by the interface. /// The interfaces implemented by the interface.
@ -120,7 +120,7 @@ pub struct InterfaceType {
/// The definition of a union type. /// The definition of a union type.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#UnionType). /// [Reference](https://spec.graphql.org/October2021/#UnionType).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct UnionType { pub struct UnionType {
/// The member types of the union. /// The member types of the union.
@ -129,7 +129,7 @@ pub struct UnionType {
/// The definition of an enum. /// The definition of an enum.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#EnumType). /// [Reference](https://spec.graphql.org/October2021/#EnumType).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EnumType { pub struct EnumType {
/// The possible values of the enum. /// The possible values of the enum.
@ -138,7 +138,7 @@ pub struct EnumType {
/// The definition of a value inside an enum. /// The definition of a value inside an enum.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#EnumValueDefinition). /// [Reference](https://spec.graphql.org/October2021/#EnumValueDefinition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EnumValueDefinition { pub struct EnumValueDefinition {
/// The description of the argument. /// The description of the argument.
@ -151,7 +151,7 @@ pub struct EnumValueDefinition {
/// The definition of an input object. /// The definition of an input object.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#InputObjectType). /// [Reference](https://spec.graphql.org/October2021/#InputObjectType).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct InputObjectType { pub struct InputObjectType {
/// The fields of the input object. /// The fields of the input object.
@ -160,7 +160,7 @@ pub struct InputObjectType {
/// The definition of an input value inside the arguments of a field. /// The definition of an input value inside the arguments of a field.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#InputValueDefinition). /// [Reference](https://spec.graphql.org/October2021/#InputValueDefinition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct InputValueDefinition { pub struct InputValueDefinition {
/// The description of the argument. /// The description of the argument.
@ -177,7 +177,7 @@ pub struct InputValueDefinition {
/// The definition of a directive in a service. /// The definition of a directive in a service.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#DirectiveDefinition). /// [Reference](https://spec.graphql.org/October2021/#DirectiveDefinition).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DirectiveDefinition { pub struct DirectiveDefinition {
/// The description of the directive. /// The description of the directive.
@ -192,7 +192,7 @@ pub struct DirectiveDefinition {
/// Where a directive can apply to. /// Where a directive can apply to.
/// ///
/// [Reference](https://spec.graphql.org/June2018/#DirectiveLocation). /// [Reference](https://spec.graphql.org/October2021/#DirectiveLocation).
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DirectiveLocation { pub enum DirectiveLocation {
/// A [query](enum.OperationType.html#variant.Query) [operation](struct.OperationDefinition.html). /// A [query](enum.OperationType.html#variant.Query) [operation](struct.OperationDefinition.html).
@ -233,4 +233,6 @@ pub enum DirectiveLocation {
/// An [input value definition](struct.InputValueDefinition.html) on an input object but not a /// An [input value definition](struct.InputValueDefinition.html) on an input object but not a
/// field. /// field.
InputFieldDefinition, InputFieldDefinition,
/// An [variable definition](struct.VariableDefinition.html).
VariableDefinition,
} }

View File

@ -0,0 +1,3 @@
query Foo($a: Int @directive = 10, $b: Int @directive) {
value
}

View File

@ -4,3 +4,5 @@ directive @test1(service: String!) on FIELD_DEFINITION
directive @test2(service: String!) on FIELD directive @test2(service: String!) on FIELD
directive @test3(service: String!) on ENUM_VALUE directive @test3(service: String!) on ENUM_VALUE
directive @test4(service: String!) on ENUM directive @test4(service: String!) on ENUM
directive @test5(service: String!) on VARIABLE_DEFINITION