Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit bca2ddf

Browse files
committed
feat: import
1 parent 449c863 commit bca2ddf

File tree

17 files changed

+10283
-0
lines changed

17 files changed

+10283
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# * @classi/example-backend-engineers @classi/example-frontend-engineers

.github/workflows/lint.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
name: lint
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
jobs:
9+
lint:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
pull-requests: write
13+
contents: read
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: actions/setup-go@v3
17+
with:
18+
go-version-file: 'go.mod'
19+
- uses: actions/cache@v3
20+
with:
21+
path: ~/go/pkg/mod
22+
key: go-${{ hashFiles('**/go.sum') }}
23+
restore-keys: |
24+
go-
25+
- run: go mod download
26+
- uses: reviewdog/action-setup@v1
27+
with:
28+
reviewdog_version: v0.14.1
29+
- name: Run reviewdog
30+
env:
31+
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
run: |
33+
go run ./tools/analyze -schema './schemata/**/*.gql' 2>&1 | tee ret.txt
34+
cat ret.txt | reviewdog -efm='%f:%l %m' -reporter=github-pr-review -fail-on-error=true

.github/workflows/publish-schema.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
3+
name: publish schema
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
jobs:
10+
publish:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
packages: write
14+
contents: write
15+
issues: write
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-node@v3
19+
with:
20+
node-version-file: 'package.json'
21+
cache: 'npm'
22+
cache-dependency-path: '**/package-lock.json'
23+
- run: npm i
24+
- run: npx prettier -c ./schemata/**/*
25+
- run: npm run build
26+
- run: npx semantic-release
27+
if: false # disabled for example repository
28+
env:
29+
NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
schema-files.json
3+
index.js
4+
index.d.ts

.vscode/extensions.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"esbenp.prettier-vscode",
4+
"graphql.vscode-graphql"
5+
]
6+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[graphql]": {
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
"editor.formatOnSave": true
5+
}
6+
}

analyzer/requireauthorize/checker.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package requireauthorize
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"unicode"
7+
8+
"github.com/gqlgo/gqlanalysis"
9+
"github.com/vektah/gqlparser/v2/ast"
10+
)
11+
12+
var Analyzer = &gqlanalysis.Analyzer{
13+
Name: "requireauthorizescope",
14+
Run: run,
15+
}
16+
17+
const directiveName = "authorize"
18+
19+
func run(pass *gqlanalysis.Pass) (any, error) {
20+
if pass.Schema.Query != nil {
21+
if err := check(pass, fieldKindQuery, pass.Schema.Query.Fields); err != nil {
22+
return nil, err
23+
}
24+
}
25+
if pass.Schema.Mutation != nil {
26+
if err := check(pass, fieldKindMutation, pass.Schema.Mutation.Fields); err != nil {
27+
return nil, err
28+
}
29+
}
30+
return nil, nil
31+
}
32+
33+
func check(pass *gqlanalysis.Pass, fieldKind fieldKind, fields ast.FieldList) error {
34+
for _, f := range fields {
35+
if f.Position == nil { // injected
36+
continue
37+
}
38+
dir := f.Directives.ForName(directiveName)
39+
if dir == nil {
40+
pass.Reportf(f.Position, "directive %q not found", directiveName)
41+
continue
42+
}
43+
scopes := dir.Arguments.ForName("scopes")
44+
if scopes == nil {
45+
pass.Reportf(f.Position, "[BUG] scopes argument is not found")
46+
continue
47+
}
48+
expectedScope := buildExpectedScopeName(fieldKind, f.Name)
49+
var found bool
50+
for _, c := range scopes.Value.Children {
51+
if c.Value.Raw == expectedScope {
52+
found = true
53+
break
54+
}
55+
}
56+
if !found {
57+
pass.Reportf(f.Position, "the required scopes must include %q but no scopes found", expectedScope)
58+
}
59+
}
60+
return nil
61+
}
62+
63+
func buildExpectedScopeName(fk fieldKind, fieldName string) string {
64+
b := new(bytes.Buffer)
65+
fmt.Fprint(b, fk.prefix())
66+
for _, r := range fieldName {
67+
if unicode.IsUpper(r) {
68+
b.WriteRune('_')
69+
}
70+
b.WriteRune(unicode.ToUpper(r))
71+
}
72+
return b.String()
73+
}
74+
75+
type fieldKind int
76+
77+
func (fk fieldKind) prefix() string {
78+
switch fk {
79+
case fieldKindQuery:
80+
return "QUERY_"
81+
case fieldKindMutation:
82+
return "MUTATION_"
83+
default:
84+
return "UNKNOWN_"
85+
}
86+
}
87+
88+
const (
89+
fieldKindQuery fieldKind = iota + 1
90+
fieldKindMutation
91+
)

go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/classi/example-graphql-api-schema
2+
3+
go 1.20
4+
5+
require (
6+
github.com/gqlgo/gqlanalysis v0.3.5
7+
github.com/vektah/gqlparser/v2 v2.5.1
8+
)
9+
10+
require (
11+
github.com/Yamashou/gqlgenc v0.11.1 // indirect
12+
github.com/agnivade/levenshtein v1.1.1 // indirect
13+
github.com/mattn/go-zglob v0.0.3 // indirect
14+
)

go.sum

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

graphql.config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
3+
schema:
4+
- 'schemata/**/*.gql'

0 commit comments

Comments
 (0)