Skip to content

Commit

Permalink
graphql post and setRoute fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Apr 27, 2022
1 parent 236dd39 commit dd46ebd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
46 changes: 31 additions & 15 deletions api/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func NewHandler(db client.DB, c *HandlerConfig) *handler {
db: db,
}

h.setRoutes()

if c != nil {
if c.Logger != nil {
h.logger = withLogger(c.Logger)
Expand All @@ -56,6 +54,8 @@ func NewHandler(db client.DB, c *HandlerConfig) *handler {

h.logger = defaultLogger()

h.setRoutes()

return h
}

Expand Down Expand Up @@ -103,12 +103,28 @@ func dump(c *context) {
}

func execGQL(c *context) {
query := c.req.URL.Query().Get("query")
var query string
if c.req.Method == "GET" {
query = c.req.URL.Query().Get("query")
} else {
body, err := io.ReadAll(c.req.Body)
if err != nil {
http.Error(c.res, err.Error(), http.StatusBadRequest)
return
}
query = string(body)
}

if query == "" {
http.Error(c.res, "missing GraphQL query", http.StatusBadRequest)
return
}

result := c.db.ExecQuery(c.req.Context(), query)

err := json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}
}
Expand All @@ -129,7 +145,7 @@ func loadSchema(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -143,7 +159,7 @@ func loadSchema(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -157,7 +173,7 @@ func loadSchema(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}
}
Expand All @@ -180,7 +196,7 @@ func getBlock(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -196,7 +212,7 @@ func getBlock(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -211,7 +227,7 @@ func getBlock(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -224,7 +240,7 @@ func getBlock(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -239,7 +255,7 @@ func getBlock(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -253,7 +269,7 @@ func getBlock(c *context) {

err = json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand All @@ -267,7 +283,7 @@ func getBlock(c *context) {
// result.Errors = []interface{}{err.Error()}
// err = json.NewEncoder(c.res).Encode(result)
// if err != nil {
// http.Error(c.res, err.Error(), 500)
// http.Error(c.res, err.Error(), http.StatusInternalServerError)
// return
// }
// c.c.res.WriteHeader(http.StatusBadRequest)
Expand All @@ -288,7 +304,7 @@ func getBlock(c *context) {

err := json.NewEncoder(c.res).Encode(result)
if err != nil {
http.Error(c.res, err.Error(), 500)
http.Error(c.res, err.Error(), http.StatusInternalServerError)
return
}

Expand Down
1 change: 1 addition & 0 deletions api/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func (h *handler) setRoutes() {
h.Get("/dump", h.handle(dump))
h.Get("/blocks/get/{cid}", h.handle(getBlock))
h.Get("/graphql", h.handle(execGQL))
h.Post("/graphql", h.handle(execGQL))
h.Post("/schema/load", h.handle(loadSchema))
}

0 comments on commit dd46ebd

Please sign in to comment.