From 8d2ec7f04cf0542867b37741b0f5baa3dc3c2ba1 Mon Sep 17 00:00:00 2001 From: nametake Date: Fri, 26 Jan 2024 01:26:45 +0900 Subject: [PATCH] Checked nested ID --- iddirective.go | 54 +++++++++++++++++++-------------- testdata/a/schema/model.graphql | 4 +-- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/iddirective.go b/iddirective.go index 2e6ab29..3257ac8 100644 --- a/iddirective.go +++ b/iddirective.go @@ -9,40 +9,48 @@ func Analyzer() *gqlanalysis.Analyzer { return &gqlanalysis.Analyzer{ Name: "iddirective", Doc: "iddirective finds id fields with no id directive.", - Run: run(), + Run: run, } } -func run() func(pass *gqlanalysis.Pass) (interface{}, error) { - return func(pass *gqlanalysis.Pass) (interface{}, error) { - for _, t := range pass.Schema.Types { - if t.BuiltIn { - continue - } - if t.Kind == ast.InputObject { - for _, field := range t.Fields { - if field != nil && field.Type != nil { - if field.Type.NamedType == "ID" { - if field.Directives.ForName("id") == nil { - pass.Reportf(field.Position, "%s has no id directive", field.Name) - } +func isIDType(t *ast.Type) bool { + if t == nil { + return false + } + if t.NamedType == "ID" { + return true + } + return isIDType(t.Elem) +} + +func run(pass *gqlanalysis.Pass) (interface{}, error) { + for _, t := range pass.Schema.Types { + if t.BuiltIn { + continue + } + if t.Kind == ast.InputObject { + for _, field := range t.Fields { + if field != nil && field.Type != nil { + if isIDType(field.Type) { + if field.Directives.ForName("id") == nil { + pass.Reportf(field.Position, "%s has no id directive", field.Name) } } } } - if t.Kind == ast.Object { - for _, field := range t.Fields { - for _, arg := range field.Arguments { - if arg.Type.NamedType == "ID" { - if arg.Directives.ForName("id") == nil { - pass.Reportf(field.Position, "argument %s of %s has no id directive", arg.Name, field.Name) - } + } + if t.Kind == ast.Object { + for _, field := range t.Fields { + for _, arg := range field.Arguments { + if isIDType(arg.Type) { + if arg.Directives.ForName("id") == nil { + pass.Reportf(field.Position, "argument %s of %s has no id directive", arg.Name, field.Name) } } } } } - - return nil, nil } + + return nil, nil } diff --git a/testdata/a/schema/model.graphql b/testdata/a/schema/model.graphql index 6c0a480..15503a4 100644 --- a/testdata/a/schema/model.graphql +++ b/testdata/a/schema/model.graphql @@ -3,8 +3,8 @@ type Type { field: String! fieldWithIdDirective(id: ID! @id(kind: "Kind")): FieldWithIdDirective fieldWithNoIdDirective(id: ID!): FieldWithNoIdDirective # want "argument id of fieldWithNoIdDirective has no id directive" - fieldWithIdsDirective(id: [ID!]! @id(kind: "Kind")): FieldWithNoIdDirective - fieldWithNoIdsDirective(id: [ID!]!): FieldWithNoIdDirective # want "argument id of fieldWithNoIdDirective has no id directive" + fieldWithIdsDirective(ids: [ID!]! @id(kind: "Kind")): [FieldWithNoIdDirective!]! + fieldWithNoIdsDirective(ids: [ID!]!): [FieldWithNoIdDirective!]! # want "argument ids of fieldWithNoIdsDirective has no id directive" } type FieldWithIdDirective {