From d9e209a19d55ef1353f6fa6838306269cdb0b9de Mon Sep 17 00:00:00 2001 From: ilslv <47687266+ilslv@users.noreply.github.com> Date: Mon, 20 Dec 2021 20:51:46 +0300 Subject: [PATCH] Add `@deprecated` directive (#1008) --- juniper/src/schema/model.rs | 25 ++++++++ juniper/src/tests/introspection_tests.rs | 7 +++ juniper/src/tests/schema_introspection.rs | 70 +++++++++++++++++++++++ 3 files changed, 102 insertions(+) diff --git a/juniper/src/schema/model.rs b/juniper/src/schema/model.rs index cc48f592..39990d1f 100644 --- a/juniper/src/schema/model.rs +++ b/juniper/src/schema/model.rs @@ -85,10 +85,14 @@ pub enum DirectiveLocation { Scalar, #[graphql(name = "FRAGMENT_DEFINITION")] FragmentDefinition, + #[graphql(name = "FIELD_DEFINITION")] + FieldDefinition, #[graphql(name = "FRAGMENT_SPREAD")] FragmentSpread, #[graphql(name = "INLINE_FRAGMENT")] InlineFragment, + #[graphql(name = "ENUM_VALUE")] + EnumValue, } impl<'a, QueryT, MutationT, SubscriptionT> @@ -214,6 +218,10 @@ impl<'a, S> SchemaType<'a, S> { "include".to_owned(), DirectiveType::new_include(&mut registry), ); + directives.insert( + "deprecated".to_owned(), + DirectiveType::new_deprecated(&mut registry), + ); directives.insert( "specifiedBy".to_owned(), DirectiveType::new_specified_by(&mut registry), @@ -545,6 +553,21 @@ where ) } + fn new_deprecated(registry: &mut Registry<'a, S>) -> DirectiveType<'a, S> + where + S: ScalarValue, + { + Self::new( + "deprecated", + &[ + DirectiveLocation::FieldDefinition, + DirectiveLocation::EnumValue, + ], + &[registry.arg::("reason", &())], + false, + ) + } + fn new_specified_by(registry: &mut Registry<'a, S>) -> DirectiveType<'a, S> where S: ScalarValue, @@ -570,10 +593,12 @@ impl fmt::Display for DirectiveLocation { DirectiveLocation::Mutation => "mutation", DirectiveLocation::Subscription => "subscription", DirectiveLocation::Field => "field", + DirectiveLocation::FieldDefinition => "field definition", DirectiveLocation::FragmentDefinition => "fragment definition", DirectiveLocation::FragmentSpread => "fragment spread", DirectiveLocation::InlineFragment => "inline fragment", DirectiveLocation::Scalar => "scalar", + DirectiveLocation::EnumValue => "enum value", }) } } diff --git a/juniper/src/tests/introspection_tests.rs b/juniper/src/tests/introspection_tests.rs index 16416b2f..f4f06491 100644 --- a/juniper/src/tests/introspection_tests.rs +++ b/juniper/src/tests/introspection_tests.rs @@ -208,6 +208,13 @@ async fn test_introspection_directives() { "INLINE_FRAGMENT", ], }, + { + "name": "deprecated", + "locations": [ + "FIELD_DEFINITION", + "ENUM_VALUE", + ], + }, { "name": "specifiedBy", "locations": [ diff --git a/juniper/src/tests/schema_introspection.rs b/juniper/src/tests/schema_introspection.rs index fbea3b98..d71b8dc8 100644 --- a/juniper/src/tests/schema_introspection.rs +++ b/juniper/src/tests/schema_introspection.rs @@ -1050,6 +1050,12 @@ pub(crate) fn schema_introspection_result() -> Value { "isDeprecated": false, "deprecationReason": null }, + { + "name": "FIELD_DEFINITION", + "description": null, + "isDeprecated": false, + "deprecationReason": null + }, { "name": "FRAGMENT_SPREAD", "description": null, @@ -1067,6 +1073,12 @@ pub(crate) fn schema_introspection_result() -> Value { "description": null, "isDeprecated": false, "deprecationReason": null + }, + { + "name": "ENUM_VALUE", + "description": null, + "isDeprecated": false, + "deprecationReason": null } ], "possibleTypes": null @@ -1376,6 +1388,31 @@ pub(crate) fn schema_introspection_result() -> Value { } ] }, + { + "name": "deprecated", + "description": null, + "isRepeatable": false, + "locations": [ + "FIELD_DEFINITION", + "ENUM_VALUE" + ], + "args": [ + { + "name": "reason", + "description": null, + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null + } + }, + "defaultValue": null + } + ] + }, { "name": "specifiedBy", "description": null, @@ -2345,6 +2382,11 @@ pub(crate) fn schema_introspection_result_without_descriptions() -> Value { "isDeprecated": false, "deprecationReason": null }, + { + "name": "FIELD_DEFINITION", + "isDeprecated": false, + "deprecationReason": null + }, { "name": "FRAGMENT_SPREAD", "isDeprecated": false, @@ -2359,6 +2401,11 @@ pub(crate) fn schema_introspection_result_without_descriptions() -> Value { "name": "SCALAR", "isDeprecated": false, "deprecationReason": null + }, + { + "name": "ENUM_VALUE", + "isDeprecated": false, + "deprecationReason": null } ], "possibleTypes": null @@ -2650,6 +2697,29 @@ pub(crate) fn schema_introspection_result_without_descriptions() -> Value { } ] }, + { + "name": "deprecated", + "isRepeatable": false, + "locations": [ + "FIELD_DEFINITION", + "ENUM_VALUE" + ], + "args": [ + { + "name": "reason", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "SCALAR", + "name": "String", + "ofType": null + } + }, + "defaultValue": null + } + ] + }, { "name": "specifiedBy", "isRepeatable": false,