Skip to content

Commit

Permalink
add directive sdl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bramvanneerven committed May 18, 2024
1 parent ccf06a6 commit 176439d
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/directive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use async_graphql::*;
use serde::{Deserialize, Serialize};

#[tokio::test]
pub async fn test_directive_skip() {
Expand Down Expand Up @@ -127,3 +128,95 @@ pub async fn test_custom_directive() {
value!({ "value": "&abc*" })
);
}

#[tokio::test]
pub async fn test_no_unused_directives() {
struct Query;

#[Object]
impl Query {
pub async fn a(&self) -> String {
"a".into()
}
}

let sdl = Schema::new(Query, EmptyMutation, EmptySubscription).sdl();

assert!(!sdl.contains("directive @deprecated"));
assert!(!sdl.contains("directive @specifiedBy"));
assert!(!sdl.contains("directive @oneOf"));
}

#[tokio::test]
pub async fn test_includes_deprecated_directive() {
#[derive(SimpleObject)]
struct A {
#[graphql(deprecation = "Use `Foo` instead")]
a: String,
}

struct Query;

#[Object]
impl Query {
pub async fn a(&self) -> A {
A { a: "a".into() }
}
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);

assert!(schema.sdl().contains(r#"directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | ENUM_VALUE"#))
}

#[tokio::test]
pub async fn test_includes_specified_by_directive() {
#[derive(Serialize, Deserialize)]
struct A {
a: String,
}

scalar!(
A,
"A",
"This is A",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ"
);

struct Query;

#[Object]
impl Query {
pub async fn a(&self) -> A {
A { a: "a".into() }
}
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);

assert!(schema
.sdl()
.contains(r#"directive @specifiedBy(url: String!) on SCALAR"#))
}

#[tokio::test]
pub async fn test_includes_one_of_directive() {
#[derive(OneofObject)]
enum AB {
A(String),
B(i64),
}

struct Query;

#[Object]
impl Query {
pub async fn ab(&self, _input: AB) -> bool {
true
}
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);

assert!(schema.sdl().contains(r#"directive @oneOf on INPUT_OBJECT"#))
}

0 comments on commit 176439d

Please sign in to comment.