Skip to content

Commit

Permalink
Merge pull request #1 from gqlgo/add-initial
Browse files Browse the repository at this point in the history
最初の実装の追加
  • Loading branch information
sonatard authored Oct 29, 2022
2 parents 316c0b0 + 47b8d81 commit 1d82bf5
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Appify Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# iddirective

[![pkg.go.dev][gopkg-badge]][gopkg]

`iddirective` finds id fields with no @id directive and arguments in your GraphQL schema files.

```graphql
input NoIdDirectiveMutationInput {
name: String!
adminID: ID! # want "adminID has no id directive"
}
```

## How to use

A runnable linter can be created with multichecker package.
You can create own linter with your favorite Analyzers.

```go
package main

import (
"flag"
"github.com/gqlgo/iddirective"
"github.com/gqlgo/gqlanalysis/multichecker"
)

func main() {
multichecker.Main(
iddirective.Analyzer(),
)
}
```

`iddirective` provides a typical main function and you can install with `go install` command.

```sh
$ go install github.com/gqlgo/iddirective/cmd/iddirective@latest
```

The `iddirective` command has a flag, `schema` which will be parsed and analyzed by iddirective's Analyzer.

```sh
$ iddirective -schema="server/graphql/schema/**/*.graphql"
```

The default value of `schema` is "schema/*/**.graphql".

## Author

[![Appify Technologies, Inc.](appify-logo.png)](http://github.com/appify-technologies)

<!-- links -->
[gopkg]: https://pkg.go.dev/github.com/gqlgo/iddirective
[gopkg-badge]: https://pkg.go.dev/badge/github.com/gqlgo/iddirective?status.svg
Binary file added appify-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions cmd/iddirective/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/gqlgo/gqlanalysis/multichecker"
"github.com/gqlgo/iddirective"
)

func main() {
multichecker.Main(
iddirective.Analyzer(),
)
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/gqlgo/iddirective

go 1.19

require (
github.com/gqlgo/gqlanalysis v0.3.5
github.com/vektah/gqlparser/v2 v2.5.1
)

require (
github.com/Yamashou/gqlgenc v0.11.1 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/mattn/go-zglob v0.0.3 // indirect
)
41 changes: 41 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
github.com/Yamashou/gqlgenc v0.11.1 h1:eJbDHLYfZuGtzsBQd2NYuLF+CZ2nCq8EK/O8lgB5teQ=
github.com/Yamashou/gqlgenc v0.11.1/go.mod h1:xxbfApjwVVo01OaAcyeMITA7G4rqqI58CUW0a4X4S/g=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/gqlgo/gqlanalysis v0.3.5 h1:3IlJHUcInwfeJwKdzk3rBJH4kJJF1GRTSHIz89eOluM=
github.com/gqlgo/gqlanalysis v0.3.5/go.mod h1:nD/jAUCfaXrvWyakLEOsVB4ONmmwkPpQjw9LRGm78+k=
github.com/josharian/mapfs v0.0.0-20210615234106-095c008854e6 h1:c+ctPFdISggaSNCfU1IueNBAsqetJSvMcpQlT+0OVdY=
github.com/josharian/txtarfs v0.0.0-20210615234325-77aca6df5bca h1:a8xeK4GsWLE4LYo5VI4u1Cn7ZvT1NtXouXR3DdKLB8Q=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-zglob v0.0.3 h1:6Ry4EYsScDyt5di4OI6xw1bYhOqfE5S33Z1OPy+d+To=
github.com/mattn/go-zglob v0.0.3/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/vektah/gqlparser/v2 v2.5.1 h1:ZGu+bquAY23jsxDRcYpWjttRZrUz07LbiY77gUOHcr4=
github.com/vektah/gqlparser/v2 v2.5.1/go.mod h1:mPgqFBu/woKTVYWyNk8cO3kh4S/f4aRFZrvOnp3hmCs=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
48 changes: 48 additions & 0 deletions optional_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package iddirective

import (
"github.com/gqlgo/gqlanalysis"
"github.com/vektah/gqlparser/v2/ast"
)

func Analyzer() *gqlanalysis.Analyzer {
return &gqlanalysis.Analyzer{
Name: "iddirective",
Doc: "iddirective finds id fields with no id directive.",
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)
}
}
}
}
}
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)
}
}
}
}
}
}

return nil, nil
}
}
13 changes: 13 additions & 0 deletions optional_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package iddirective_test

import (
"testing"

"github.com/gqlgo/gqlanalysis/analysistest"
"github.com/gqlgo/iddirective"
)

func Test(t *testing.T) {
testdata := analysistest.TestData(t)
analysistest.Run(t, testdata, iddirective.Analyzer(), "a")
}
1 change: 1 addition & 0 deletions testdata/a/schema/directive.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
directive @id(kind: String!) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
16 changes: 16 additions & 0 deletions testdata/a/schema/model.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Type {
id: ID! # OK
field: String!
fieldWithIdDirective(id: ID! @id(kind: "Kind")): FieldWithIdDirective
fieldWithNoIdDirective(id: ID!): FieldWithNoIdDirective # want "argument id of fieldWithNoIdDirective has no id directive"
}

type FieldWithIdDirective {
id: ID! # OK
field: String!
}

type FieldWithNoIdDirective {
id: ID! # OK
field: String!
}
22 changes: 22 additions & 0 deletions testdata/a/schema/mutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type Mutation {
mutation(input: MutationInput!): MutationPayload!
NoIdDirectiveMutation(noIdDirectiveMutationInput: NoIdDirectiveMutationInput): NoIdDirectiveMutationPayload!
}

input MutationInput {
name: String!
id: ID! @id(kind: "Kind")
}

type MutationPayload {
field: String!
}

input NoIdDirectiveMutationInput {
name: String!
adminID: ID! # want "adminID has no id directive"
}

type NoIdDirectiveMutationPayload {
field: String!
}

0 comments on commit 1d82bf5

Please sign in to comment.