Skip to content

Commit

Permalink
support for "deprecated" directive on enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 13, 2017
1 parent 498fe39 commit e06f585
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
44 changes: 44 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,50 @@ func TestDeprecatedDirective(t *testing.T) {
}
`,
},
{
Schema: graphql.MustParseSchema(`
schema {
query: Query
}
type Query {
}
enum Test {
A
B @deprecated
C @deprecated(reason: "We don't like it")
}
`, &testDeprecatedDirectiveResolver{}),
Query: `
{
__type(name: "Test") {
enumValues {
name
}
allEnumValues: enumValues(includeDeprecated: true) {
name
isDeprecated
deprecationReason
}
}
}
`,
ExpectedResult: `
{
"__type": {
"enumValues": [
{ "name": "A" }
],
"allEnumValues": [
{ "name": "A", "isDeprecated": false, "deprecationReason": null },
{ "name": "B", "isDeprecated": true, "deprecationReason": "No longer supported" },
{ "name": "C", "isDeprecated": true, "deprecationReason": "We don't like it" }
]
}
}
`,
},
})
}

Expand Down
27 changes: 23 additions & 4 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Schema struct {
entryPointNames map[string]string
objects []*Object
unions []*Union
enums []*Enum
}

func (s *Schema) Resolve(name string) common.Type {
Expand Down Expand Up @@ -68,8 +69,9 @@ type Enum struct {
}

type EnumValue struct {
Name string
Desc string
Name string
Directives map[string]common.DirectiveArgs
Desc string
}

type InputObject struct {
Expand Down Expand Up @@ -208,6 +210,14 @@ func (s *Schema) Parse(schemaString string) error {
}
}

for _, enum := range s.enums {
for _, value := range enum.Values {
if err := resolveDirectives(s, value.Directives); err != nil {
return err
}
}
}

return nil
}

Expand Down Expand Up @@ -239,7 +249,14 @@ func resolveField(s *Schema, f *Field) error {
return err
}
f.Type = t
for name, args := range f.Directives {
if err := resolveDirectives(s, f.Directives); err != nil {
return err
}
return resolveInputObject(s, &f.Args)
}

func resolveDirectives(s *Schema, directives map[string]common.DirectiveArgs) error {
for name, args := range directives {
d, ok := s.Directives[name]
if !ok {
return errors.Errorf("directive %q not found", name)
Expand All @@ -255,7 +272,7 @@ func resolveField(s *Schema, f *Field) error {
}
}
}
return resolveInputObject(s, &f.Args)
return nil
}

func resolveInputObject(s *Schema, io *common.InputMap) error {
Expand Down Expand Up @@ -300,6 +317,7 @@ func parseSchema(s *Schema, l *lexer.Lexer) {
enum := parseEnumDecl(l)
enum.Desc = desc
s.Types[enum.Name] = enum
s.enums = append(s.enums, enum)
case "input":
input := parseInputDecl(l)
input.Desc = desc
Expand Down Expand Up @@ -378,6 +396,7 @@ func parseEnumDecl(l *lexer.Lexer) *Enum {
v := &EnumValue{}
v.Desc = l.DescComment()
v.Name = l.ConsumeIdent()
v.Directives = common.ParseDirectives(l)
enum.Values = append(enum.Values, v)
}
l.ConsumeToken('}')
Expand Down
18 changes: 13 additions & 5 deletions introspection/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ func (r *Type) EnumValues(args *struct{ IncludeDeprecated bool }) *[]*EnumValue
return nil
}

l := make([]*EnumValue, len(t.Values))
for i, v := range t.Values {
l[i] = &EnumValue{v}
var l []*EnumValue
for _, v := range t.Values {
if _, ok := v.Directives["deprecated"]; !ok || args.IncludeDeprecated {
l = append(l, &EnumValue{v})
}
}
return &l
}
Expand Down Expand Up @@ -277,11 +279,17 @@ func (r *EnumValue) Description() *string {
}

func (r *EnumValue) IsDeprecated() bool {
return false
_, ok := r.value.Directives["deprecated"]
return ok
}

func (r *EnumValue) DeprecationReason() *string {
return nil
args, ok := r.value.Directives["deprecated"]
if !ok {
return nil
}
reason := args["reason"].(string)
return &reason
}

type Directive struct {
Expand Down

0 comments on commit e06f585

Please sign in to comment.