Skip to content
Permalink
Browse files

Fix golang compiles; add `/graphql` endpoint #32

- Add simple task resolver
  • Loading branch information...
upvalue committed Oct 7, 2018
1 parent 39fda0a commit 25a8ba62c75fd683f7403abf296b33a10c2631bb
Showing with 154 additions and 12 deletions.
  1. +1 βˆ’0 backend/app.go
  2. +134 βˆ’0 backend/graphql.go
  3. +1 βˆ’1 backend/habits.go
  4. +9 βˆ’0 backend/habitsweb.go
  5. +1 βˆ’1 go.mod
  6. +2 βˆ’2 go.sum
  7. +1 βˆ’3 main.go
  8. +5 βˆ’5 package.json
@@ -178,6 +178,7 @@ func App() *macaron.Macaron {

init("/habits", habitsInit)
init("/journal", journalInit)
graphqlInit(m)

m.Get("/test", func(c *macaron.Context) {
c.HTML(200, "test")
@@ -0,0 +1,134 @@
package backend

import (
"fmt"
"net/http"

"github.com/go-macaron/binding"
"github.com/graphql-go/graphql"
macaron "gopkg.in/macaron.v1"
)

func graphqlInit(m *macaron.Macaron) {

/*
statusEnum := graphql.NewEnum(graphql.EnumConfig{
Name: "Status",
Description: "Status of the task: unset/complete/incomplete",
Values: graphql.EnumValueConfigMap{
"UNSET": &graphql.EnumValueConfig{
Value: TaskUnset,
Description: "Task status is unset",
},
"COMPLETE": &graphql.EnumValueConfig{
Value: TaskComplete,
Description: "Task status is complete",
},
"INCOMPLETE": &graphql.EnumValueConfig{
Value: TaskIncomplete,
Description: "Task status is incomplete",
},
},
})
*/

commentInterface := graphql.NewObject(graphql.ObjectConfig{
Name: "Comment",
Description: "Comment",
Fields: graphql.Fields{
"Body": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "Body",
},
},
})

taskInterface := graphql.NewObject(graphql.ObjectConfig{
Name: "Task",
Description: "Task",
Fields: graphql.Fields{
"ID": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
Description: "ID",
},
"Name": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "Name",
},
"Minutes": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
Description: "Minutes spent on task as an integer",
},
"Date": &graphql.Field{
Type: graphql.NewNonNull(graphql.DateTime),
Description: "Date of the task, determining its scope",
},

"Order": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
Description: "Task order within scope",
},

"Scope": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
Description: "Task scope",
},

"Status": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int), //statusEnum,
Description: "Task status",
},

"Comment": &graphql.Field{
Type: graphql.NewNonNull(commentInterface),
Description: "Task comment",
},
},
})

queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"task": &graphql.Field{
Type: taskInterface,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Description: "Id",
Type: graphql.NewNonNull(graphql.Int),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var task Task
DB.Where("id = ?", p.Args["id"]).Preload("Comment").First(&task)

return task, nil
},
},
},
})

schema, _ := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})

executeQuery := func(query string, schema graphql.Schema) *graphql.Result {
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if len(result.Errors) > 0 {
fmt.Printf("wrong result, unexpected errors: %v", result.Errors)
}
return result
}

type Query struct {
Query string `json:"query"`
}

m.Post("/graphql", binding.Bind(Query{}), func(c *macaron.Context, q Query) {
result := executeQuery(q.Query, schema)
c.JSON(http.StatusOK, result)

})
}
@@ -48,7 +48,7 @@ const (

// Task represents a task in the database
type Task struct {
gorm.Model
ID uint
Name string
// The actual date of the task, regardless of when it was created
Date time.Time
@@ -129,6 +129,14 @@ func taskSelectScope(date *time.Time, to *time.Time, name *string, scope *int) f
}
}

func taskSelectSingle(c *macaron.Context) {
var task Task

DB.Where("id = ?", c.ParamsInt("id")).First(&task)

c.JSON(http.StatusOK, task)
}

func taskSelect(c *macaron.Context) {
var tasks []Task

@@ -572,6 +580,7 @@ func habitsInit(m *macaron.Macaron) {

// REST-ish api
m.Get("/tasks", taskSelect)
m.Get("/tasks/:id:int", taskSelectSingle)
m.Put("/tasks/:id:int", binding.Bind(Task{}), taskUpdate)
m.Post("/tasks", binding.Bind(Task{}), taskNew)
m.Delete("/tasks/:id:int", taskDelete)
2 go.mod
@@ -14,7 +14,7 @@ require (
github.com/google/go-cmp v0.2.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c // indirect
github.com/gorilla/websocket v0.0.0-20171013020858-71fa72d48423
github.com/ioddly/meditations v0.0.0-20180916035201-d37afab02bcc
github.com/graphql-go/graphql v0.7.6
github.com/jinzhu/gorm v0.0.0-20171010132839-0a51f6cdc55d
github.com/jinzhu/inflection v0.0.0-20170102125226-1c35d901db3d // indirect
github.com/jinzhu/now v0.0.0-20170212112655-d939ba741945
4 go.sum
@@ -25,8 +25,8 @@ github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c h1:16eHWuMGvCjSf
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v0.0.0-20171013020858-71fa72d48423 h1:wkOcdLjYkjfxpi9H4cZeErBrLIAhTRp6MWSk/kS9/h4=
github.com/gorilla/websocket v0.0.0-20171013020858-71fa72d48423/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/ioddly/meditations v0.0.0-20180916035201-d37afab02bcc h1:Li77fICAum5vaudArxXivp6FZSZMGtpK6Zel1oX1R+o=
github.com/ioddly/meditations v0.0.0-20180916035201-d37afab02bcc/go.mod h1:BFWhLlK0r8ReCJvxa6JEsVRknxpQnCTvgoFaTyZk+SM=
github.com/graphql-go/graphql v0.7.6 h1:3Bn1IFB5OvPoANEfu03azF8aMyks0G/H6G1XeTfYbM4=
github.com/graphql-go/graphql v0.7.6/go.mod h1:k6yrAYQaSP59DC5UVxbgxESlmVyojThKdORUqGDGmrI=
github.com/jinzhu/gorm v0.0.0-20171010132839-0a51f6cdc55d h1:9IrSs6/EmI/VUBGKVl2bDDqDW+MDif7nQ3/GqMKRpIk=
github.com/jinzhu/gorm v0.0.0-20171010132839-0a51f6cdc55d/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo=
github.com/jinzhu/inflection v0.0.0-20170102125226-1c35d901db3d h1:jRQLvyVGL+iVtDElaEIDdKwpPqUIZJfzkNLV34htpEc=
@@ -1,8 +1,6 @@
package main

import (
"github.com/ioddly/meditations/backend"
)
import backend "github.com/upvalue/meditations/backend"

func main() {
backend.Main()
@@ -11,7 +11,7 @@
"postinstall": "npm rebuild node-sass && webpack -p",
"cloc": "cloc frontend backend",
"doc-frontend": "typedoc --out docs",
"doc-backend": "(godoc -http=:8958 &); xdg-open http://localhost:8958/pkg/github.com/ioddly/meditations/backend",
"doc-backend": "(godoc -http=:8958 &); xdg-open http://localhost:8958/pkg/github.com/upvalue/meditations/backend",
"test-backend": "godep go test -v ./backend --cover",
"test-frontend": "./node_modules/.bin/jest",
"test": "yarn run test-frontend && yarn run test-backend",
@@ -34,7 +34,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/ioddly/meditations.git"
"url": "git+https://github.com/upvalue/meditations.git"
},
"keywords": [],
"author": "",
@@ -43,9 +43,9 @@
"node": ">=7.10.0"
},
"bugs": {
"url": "https://github.com/ioddly/meditations/issues"
"url": "https://github.com/upvalue/meditations/issues"
},
"homepage": "https://github.com/ioddly/meditations#readme",
"homepage": "https://github.com/upvalue/meditations#readme",
"dependencies": {
"@types/jest": "^23.1.0",
"@types/lodash": "^4.14.109",
@@ -71,7 +71,7 @@
"jest-cli": "^23.6.0",
"lodash": "^4.17.10",
"medium-editor": "^5.23.2",
"medium-editor-tables": "git+https://github.com/ioddly/medium-editor-tables.git",
"medium-editor-tables": "git+https://github.com/upvalue/medium-editor-tables.git",
"moment": "^2.18.1",
"mousetrap": "^1.6.2",
"node-sass": "^4.9.2",

0 comments on commit 25a8ba6

Please sign in to comment.
You can’t perform that action at this time.