Skip to content

feat: add graphql api to surface crds #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ build-doc:
build-gitter:
docker build . -f deploy/gitter.Dockerfile -t crdsdev/doc-gitter:latest

.PHONY: generate
generate:
go generate ./...

.PHONY: build-doc build-gitter
40 changes: 40 additions & 0 deletions api/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION
| FIELD_DEFINITION

scalar Spec

type Crd {
kind: String!
group: String!
version: String!
filename: String!
spec: Spec!

tagId: ID!
tag: Tag! @goField(forceResolver: true)
}

type Tag {
id: ID!
name: String!
time: String!
repo: Repository! @goField(forceResolver: true)

crds: [Crd!]! @goField(forceResolver: true)
}

type Repository {
name: String!
tags: [Tag!]! @goField(forceResolver: true)
}

type Query {
# This is a pretty niave paging strategy
repositories(skip: Int = 0, take: Int = 10): [Repository]!
tags(repo: String!): [Tag!]
tag(repo: String!, name: String!): Tag
}

schema {
query: Query
}
9 changes: 9 additions & 0 deletions cmd/doc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import (
"os"
"strings"

graphapi "github.com/crdsdev/doc/internal/api"
graphql "github.com/crdsdev/doc/internal/api/graphql"
crdutil "github.com/crdsdev/doc/pkg/crd"

gqlhandler "github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/crdsdev/doc/pkg/models"
"github.com/google/uuid"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -191,8 +196,12 @@ func start() {
log.Println("Starting Doc server...")
r := mux.NewRouter().StrictSlash(true)
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))
graphHandler := gqlhandler.NewDefaultServer(graphql.NewExecutableSchema(graphapi.New(db)))
graphHandler.Use(extension.FixedComplexityLimit(100))

r.HandleFunc("/", home)
r.PathPrefix("/static/").Handler(staticHandler)
r.PathPrefix("/graphql/").Handler(graphHandler)
r.HandleFunc("/github.com/{org}/{repo}@{tag}", org)
r.HandleFunc("/github.com/{org}/{repo}", org)
r.HandleFunc("/raw/github.com/{org}/{repo}@{tag}", raw)
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/crdsdev/doc
go 1.13

require (
github.com/99designs/gqlgen v0.13.0
github.com/go-git/go-git/v5 v5.0.0
github.com/go-openapi/spec v0.19.5 // indirect
github.com/golang/protobuf v1.4.3 // indirect
Expand All @@ -11,12 +12,14 @@ require (
github.com/gorilla/mux v1.7.4
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/jackc/pgtype v1.6.2
github.com/jackc/pgx/v4 v4.10.1
github.com/lib/pq v1.9.0 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/unrolled/render v1.0.3
github.com/vektah/gqlparser/v2 v2.1.0
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58 // indirect
golang.org/x/text v0.3.4 // indirect
Expand Down
37 changes: 37 additions & 0 deletions go.sum

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions gqlgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
schema:
- api/schema.graphql

exec:
filename: internal/api/graphql/generated.go
package: generated

model:
filename: internal/api/models/models_gen.go
package: models

resolver:
layout: follow-schema
dir: internal/api
package: api
filename_template: "{name}.resolvers.go"

models:
ID:
model:
- github.com/99designs/gqlgen/graphql.Int64
Spec:
model:
- github.com/99designs/gqlgen/graphql.Map
Loading