mirror of
https://github.com/hasura/graphql-engine.git
synced 2025-01-05 22:34:22 +03:00
a398d3b190
GITHUB_PR_NUMBER: 6111 GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/6111 Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: 1f6517acfacb58c566bb5e48f74ea0dfa5c6f063
104 lines
2.9 KiB
Go
104 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
)
|
|
|
|
func MetadataAPI(c *gin.Context) {
|
|
// Get migrate instance
|
|
ecPtr, ok := c.Get("ec")
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// Convert to url.URL
|
|
ec, ok := ecPtr.(*cli.ExecutionContext)
|
|
if !ok {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: "cannot get execution context"})
|
|
return
|
|
}
|
|
t, err := migrate.NewMigrate(ec, false, "")
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
// Switch on request method
|
|
switch c.Request.Method {
|
|
case "GET":
|
|
files, err := t.ExportMetadata()
|
|
if err != nil {
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
queryValues := c.Request.URL.Query()
|
|
export := queryValues.Get("export")
|
|
if export == "true" {
|
|
err := t.WriteMetadata(files)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
return
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, &gin.H{"metadata": "Success"})
|
|
case "POST":
|
|
var request Request
|
|
|
|
// Bind Request body to Request struct
|
|
if c.BindJSON(&request) != nil {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: "Something went wrong"})
|
|
return
|
|
}
|
|
|
|
err := t.Query(request.Up)
|
|
if err != nil {
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
if err == migrate.ErrMigrationMode {
|
|
c.JSON(http.StatusBadRequest, &Response{Code: "migration_mode_enabled", Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
files, err := t.ExportMetadata()
|
|
if err != nil {
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
return
|
|
}
|
|
err = t.WriteMetadata(files)
|
|
if err != nil {
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, &gin.H{"message": "Success"})
|
|
default:
|
|
c.JSON(http.StatusMethodNotAllowed, &gin.H{"message": "Method not allowed"})
|
|
}
|
|
}
|