Skip to content

feat: support directive on variable definition #61

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 40 additions & 41 deletions src/schema/ast.rs
Original file line number Diff line number Diff line change
@@ -2,12 +2,13 @@ use std::str::FromStr;

use thiserror::Error;

pub use crate::common::{Directive, Type, Value, Text};
pub use crate::common::{Directive, Text, Type, Value};
use crate::position::Pos;

#[derive(Debug, Clone, Default, PartialEq)]
pub struct Document<'a, T: Text<'a>>
where T: Text<'a>
where
T: Text<'a>,
{
pub definitions: Vec<Definition<'a, T>>,
}
@@ -31,7 +32,6 @@ impl<'a> Document<'a, String> {
}
}


#[derive(Debug, Clone, PartialEq)]
pub enum Definition<'a, T: Text<'a>> {
SchemaDefinition(SchemaDefinition<'a, T>),
@@ -78,7 +78,8 @@ pub struct ScalarType<'a, T: Text<'a>> {
}

impl<'a, T> ScalarType<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -98,7 +99,8 @@ pub struct ScalarTypeExtension<'a, T: Text<'a>> {
}

impl<'a, T> ScalarTypeExtension<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -120,7 +122,8 @@ pub struct ObjectType<'a, T: Text<'a>> {
}

impl<'a, T> ObjectType<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -144,7 +147,8 @@ pub struct ObjectTypeExtension<'a, T: Text<'a>> {
}

impl<'a, T> ObjectTypeExtension<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -188,7 +192,8 @@ pub struct InterfaceType<'a, T: Text<'a>> {
}

impl<'a, T> InterfaceType<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -212,7 +217,8 @@ pub struct InterfaceTypeExtension<'a, T: Text<'a>> {
}

impl<'a, T> InterfaceTypeExtension<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -235,7 +241,8 @@ pub struct UnionType<'a, T: Text<'a>> {
}

impl<'a, T> UnionType<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -257,7 +264,8 @@ pub struct UnionTypeExtension<'a, T: Text<'a>> {
}

impl<'a, T> UnionTypeExtension<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -279,7 +287,8 @@ pub struct EnumType<'a, T: Text<'a>> {
}

impl<'a, T> EnumType<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -301,7 +310,8 @@ pub struct EnumValue<'a, T: Text<'a>> {
}

impl<'a, T> EnumValue<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -322,7 +332,8 @@ pub struct EnumTypeExtension<'a, T: Text<'a>> {
}

impl<'a, T> EnumTypeExtension<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -344,7 +355,8 @@ pub struct InputObjectType<'a, T: Text<'a>> {
}

impl<'a, T> InputObjectType<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -366,7 +378,8 @@ pub struct InputObjectTypeExtension<'a, T: Text<'a>> {
}

impl<'a, T> InputObjectTypeExtension<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -401,6 +414,7 @@ pub enum DirectiveLocation {
EnumValue,
InputObject,
InputFieldDefinition,
VariableDefinition,
}

#[derive(Debug, Clone, PartialEq)]
@@ -414,7 +428,8 @@ pub struct DirectiveDefinition<'a, T: Text<'a>> {
}

impl<'a, T> DirectiveDefinition<'a, T>
where T: Text<'a>
where
T: Text<'a>,
{
pub fn new(name: T::Value) -> Self {
Self {
@@ -451,34 +466,19 @@ impl DirectiveLocation {
EnumValue => "ENUM_VALUE",
InputObject => "INPUT_OBJECT",
InputFieldDefinition => "INPUT_FIELD_DEFINITION",
VariableDefinition => "VARIABLE_DEFINITION",
}
}

/// Returns `true` if this location is for queries (execution)
pub fn is_query(&self) -> bool {
use self::DirectiveLocation::*;
match *self {
Query
| Mutation
| Subscription
| Field
| FragmentDefinition
| FragmentSpread
| InlineFragment
=> true,

Schema
| Scalar
| Object
| FieldDefinition
| ArgumentDefinition
| Interface
| Union
| Enum
| EnumValue
| InputObject
| InputFieldDefinition
=> false,
Query | Mutation | Subscription | Field | FragmentDefinition | FragmentSpread
| InlineFragment => true,

Schema | Scalar | Object | FieldDefinition | ArgumentDefinition | Interface | Union
| Enum | EnumValue | InputObject | InputFieldDefinition | VariableDefinition => false,
Comment on lines +477 to +481
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made code worse, and hard to compare. The original formatting is not an accident it's done to have it easier to compare.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use #[rustfmt::skip] here FWIW

}
}

@@ -492,11 +492,9 @@ impl DirectiveLocation {
#[error("invalid directive location")]
pub struct InvalidDirectiveLocation;


impl FromStr for DirectiveLocation {
type Err = InvalidDirectiveLocation;
fn from_str(s: &str) -> Result<DirectiveLocation, InvalidDirectiveLocation>
{
fn from_str(s: &str) -> Result<DirectiveLocation, InvalidDirectiveLocation> {
use self::DirectiveLocation::*;
let val = match s {
"QUERY" => Query,
@@ -517,6 +515,7 @@ impl FromStr for DirectiveLocation {
"ENUM_VALUE" => EnumValue,
"INPUT_OBJECT" => InputObject,
"INPUT_FIELD_DEFINITION" => InputFieldDefinition,
"VARIABLE_DEFINITION" => VariableDefinition,
_ => return Err(InvalidDirectiveLocation),
};

3 changes: 3 additions & 0 deletions tests/queries/var_directive.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query Foo($arg: SomeType @onVariableDefinition) {
field
}
4 changes: 4 additions & 0 deletions tests/query_roundtrips.rs
Original file line number Diff line number Diff line change
@@ -104,6 +104,10 @@ fn mutation_directive() {
roundtrip_default("mutation_directive");
}
#[test]
fn var_directive() {
roundtrip_default("var_directive");
}
#[test]
fn mutation_nameless_vars() {
roundtrip_default("mutation_nameless_vars");
}
2 changes: 2 additions & 0 deletions tests/schemas/directive.graphql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @onVariableDefinition on VARIABLE_DEFINITION