Skip to content

Commit

Permalink
Checked nested ID
Browse files Browse the repository at this point in the history
  • Loading branch information
nametake committed Jan 25, 2024
1 parent fcda91b commit 8d2ec7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
54 changes: 31 additions & 23 deletions iddirective.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions testdata/a/schema/model.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8d2ec7f

Please sign in to comment.