aboutsummaryrefslogtreecommitdiffstats
path: root/graphql
diff options
context:
space:
mode:
authorQuentin Gliech <quentingliech@gmail.com>2018-07-20 00:25:30 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-20 14:32:23 +0200
commit1dd5118eebe0e993d55ae5433cb2dcfd2764bf4f (patch)
tree67f9b2e6bb3eb7cbc870436e34e7e57a708008e5 /graphql
parent59e6ae872cad5683a9f1887cbef382d0e76ecc61 (diff)
downloadgit-bug-1dd5118eebe0e993d55ae5433cb2dcfd2764bf4f.tar.gz
graphql: Actually get the bugs from the repo
Diffstat (limited to 'graphql')
-rw-r--r--graphql/handler.go35
-rw-r--r--graphql/schema.go38
-rw-r--r--graphql/types.go38
3 files changed, 89 insertions, 22 deletions
diff --git a/graphql/handler.go b/graphql/handler.go
index 7dbb7a0f..b49ca56b 100644
--- a/graphql/handler.go
+++ b/graphql/handler.go
@@ -1,17 +1,36 @@
package graphql
-import "github.com/graphql-go/handler"
+import (
+ "context"
+ "net/http"
-func NewHandler() (*handler.Handler, error) {
- schema, err := newGraphqlSchema()
+ "github.com/MichaelMure/git-bug/repository"
+ "github.com/graphql-go/handler"
+)
+
+type Handler struct {
+ Handler *handler.Handler
+ Repo repository.Repo
+}
+
+func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ ctx := context.WithValue(r.Context(), "repo", h.Repo)
+ h.Handler.ContextHandler(ctx, w, r)
+}
+
+func NewHandler(repo repository.Repo) (*Handler, error) {
+ schema, err := graphqlSchema()
if err != nil {
return nil, err
}
- return handler.New(&handler.Config{
- Schema: &schema,
- Pretty: true,
- GraphiQL: true,
- }), nil
+ return &Handler{
+ Handler: handler.New(&handler.Config{
+ Schema: &schema,
+ Pretty: true,
+ GraphiQL: true,
+ }),
+ Repo: repo,
+ }, nil
}
diff --git a/graphql/schema.go b/graphql/schema.go
index e929fb44..4cb893fe 100644
--- a/graphql/schema.go
+++ b/graphql/schema.go
@@ -1,21 +1,35 @@
package graphql
-import "github.com/graphql-go/graphql"
+import (
+ "github.com/MichaelMure/git-bug/bug"
+ "github.com/MichaelMure/git-bug/repository"
+ "github.com/graphql-go/graphql"
+)
-func newGraphqlSchema() (graphql.Schema, error) {
+func graphqlSchema() (graphql.Schema, error) {
+ fields := graphql.Fields{
+ "bug": &graphql.Field{
+ Type: bugType,
+ Args: graphql.FieldConfigArgument{
+ "id": &graphql.ArgumentConfig{
+ Type: bugIdScalar,
+ },
+ },
+ Resolve: func(p graphql.ResolveParams) (interface{}, error) {
+ repo := p.Context.Value("repo").(repository.Repo)
+ id, _ := p.Args["id"].(string)
+ bug, err := bug.FindBug(repo, id)
+ if err != nil {
+ return nil, err
+ }
+
+ snapshot := bug.Compile()
- rootQuery := graphql.ObjectConfig{
- Name: "RootQuery",
- Fields: graphql.Fields{
- "hello": &graphql.Field{
- Type: graphql.String,
+ return snapshot, nil
},
},
}
-
- schemaConfig := graphql.SchemaConfig{
- Query: graphql.NewObject(rootQuery),
- }
-
+ rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
+ schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
return graphql.NewSchema(schemaConfig)
}
diff --git a/graphql/types.go b/graphql/types.go
index edce9ea1..a320ca39 100644
--- a/graphql/types.go
+++ b/graphql/types.go
@@ -1,13 +1,44 @@
package graphql
-import "github.com/graphql-go/graphql"
+import (
+ "fmt"
+
+ "github.com/graphql-go/graphql"
+ "github.com/graphql-go/graphql/language/ast"
+
+ "github.com/MichaelMure/git-bug/bug"
+)
+
+func coerceString(value interface{}) interface{} {
+ if v, ok := value.(*string); ok {
+ return *v
+ }
+ return fmt.Sprintf("%v", value)
+}
+
+var bugIdScalar = graphql.NewScalar(graphql.ScalarConfig{
+ Name: "BugID",
+ Description: "TODO",
+ Serialize: coerceString,
+ ParseValue: coerceString,
+ ParseLiteral: func(valueAST ast.Value) interface{} {
+ switch valueAST := valueAST.(type) {
+ case *ast.StringValue:
+ return valueAST.Value
+ }
+ return nil
+ },
+})
// Internally, it's the Snapshot
var bugType = graphql.NewObject(graphql.ObjectConfig{
Name: "Bug",
Fields: graphql.Fields{
"id": &graphql.Field{
- Type: graphql.String,
+ Type: bugIdScalar,
+ Resolve: func(p graphql.ResolveParams) (interface{}, error) {
+ return p.Source.(bug.Snapshot).Id(), nil
+ },
},
"status": &graphql.Field{
Type: graphql.String,
@@ -17,6 +48,9 @@ var bugType = graphql.NewObject(graphql.ObjectConfig{
},
"labels": &graphql.Field{
Type: graphql.NewList(graphql.String),
+ Resolve: func(p graphql.ResolveParams) (interface{}, error) {
+ return p.Source.(bug.Snapshot).Labels, nil
+ },
},
// TODO: operations
},