Skip to content

Commit 1225ab0

Browse files
committed
Deprecated directive (#384)
This adds a new directive as part of the experimental schema language: ``` directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE ``` It also adds support for this directive in the schemaPrinter and buildASTSchema. Additionally exports a new helper `specifiedDirectives` which is encoured to be used when addressing the collection of all directives defined by the spec. The `@deprecated` directive is optimistically added to this collection. While it's currently experimental, it will become part of the schema definition language RFC. Commit: 5375c9b20452801b69dba208cac15d32e02ac608 [5375c9b] Parents: 0aa78f61a2 Author: Lee Byron <[email protected]> Date: 9 May 2016 at 5:56:16 AM SGT Labels: HEAD
1 parent 988ab2e commit 1225ab0

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

directives.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ const (
2424
DirectiveLocationInputFieldDefinition = "INPUT_FIELD_DEFINITION"
2525
)
2626

27+
// DefaultDeprecationReason Constant string used for default reason for a deprecation.
28+
const DefaultDeprecationReason = "No longer supported"
29+
30+
// SpecifiedRules The full list of specified directives.
31+
var SpecifiedDirectives = []*Directive{
32+
IncludeDirective,
33+
SkipDirective,
34+
DeprecatedDirective,
35+
}
36+
2737
// Directive structs are used by the GraphQL runtime as a way of modifying execution
2838
// behavior. Type system creators will usually not create these directly.
2939
type Directive struct {
@@ -90,7 +100,7 @@ func NewDirective(config DirectiveConfig) *Directive {
90100
return dir
91101
}
92102

93-
// IncludeDirective is used to conditionally include fields or fragments
103+
// IncludeDirective is used to conditionally include fields or fragments.
94104
var IncludeDirective = NewDirective(DirectiveConfig{
95105
Name: "include",
96106
Description: "Directs the executor to include this field or fragment only when " +
@@ -108,7 +118,7 @@ var IncludeDirective = NewDirective(DirectiveConfig{
108118
},
109119
})
110120

111-
// SkipDirective Used to conditionally skip (exclude) fields or fragments
121+
// SkipDirective Used to conditionally skip (exclude) fields or fragments.
112122
var SkipDirective = NewDirective(DirectiveConfig{
113123
Name: "skip",
114124
Description: "Directs the executor to skip this field or fragment when the `if` " +
@@ -125,3 +135,22 @@ var SkipDirective = NewDirective(DirectiveConfig{
125135
DirectiveLocationInlineFragment,
126136
},
127137
})
138+
139+
// DeprecatedDirective Used to declare element of a GraphQL schema as deprecated.
140+
var DeprecatedDirective = NewDirective(DirectiveConfig{
141+
Name: "deprecated",
142+
Description: "Marks an element of a GraphQL schema as no longer supported.",
143+
Args: FieldConfigArgument{
144+
"reason": &ArgumentConfig{
145+
Type: String,
146+
Description: "Explains why this element was deprecated, usually also including a " +
147+
"suggestion for how to access supported similar data. Formatted" +
148+
"in [Markdown](https://daringfireball.net/projects/markdown/).",
149+
DefaultValue: DefaultDeprecationReason,
150+
},
151+
},
152+
Locations: []string{
153+
DirectiveLocationFieldDefinition,
154+
DirectiveLocationEnumValue,
155+
},
156+
})

introspection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func init() {
136136
Description: "Location adjacent to an inline fragment.",
137137
},
138138
"SCHEMA": &EnumValueConfig{
139-
Value: DirectiveLocationSchema,
139+
Value: DirectiveLocationSchema,
140140
Description: "Location adjacent to a schema definition.",
141141
},
142142
"SCALAR": &EnumValueConfig{

schema.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ type TypeMap map[string]Type
2626
// });
2727
// Note: If an array of `directives` are provided to GraphQLSchema, that will be
2828
// the exact list of directives represented and allowed. If `directives` is not
29-
// provided then a default set of the built-in `[ @include, @skip ]` directives
30-
// will be used. If you wish to provide *additional// directives to these
31-
// built-ins, you must explicitly declare them. Example:
32-
// directives: [
33-
// myCustomDirective,
34-
// GraphQLIncludeDirective,
35-
// GraphQLSkipDirective
36-
// ]
29+
// provided then a default set of the specified directives (e.g. @include and
30+
// @skip) will be used. If you wish to provide *additional* directives to these
31+
// specified directives, you must explicitly declare them. Example:
32+
//
33+
// const MyAppSchema = new GraphQLSchema({
34+
// ...
35+
// directives: specifiedDirectives.concat([ myCustomDirective ]),
36+
// })
3737
type Schema struct {
3838
typeMap TypeMap
3939
directives []*Directive
@@ -67,13 +67,10 @@ func NewSchema(config SchemaConfig) (Schema, error) {
6767
schema.mutationType = config.Mutation
6868
schema.subscriptionType = config.Subscription
6969

70-
// Provide `@include() and `@skip()` directives by default.
70+
// Provide specified directives (e.g. @include and @skip) by default.
7171
schema.directives = config.Directives
7272
if len(schema.directives) == 0 {
73-
schema.directives = []*Directive{
74-
IncludeDirective,
75-
SkipDirective,
76-
}
73+
schema.directives = SpecifiedDirectives
7774
}
7875
// Ensure directive definitions are error-free
7976
for _, dir := range schema.directives {

0 commit comments

Comments
 (0)