Skip to content

Instantly share code, notes, and snippets.

@VictorKabata
Created October 29, 2022 16:50
Show Gist options
  • Save VictorKabata/9e275f1db88a425ec5137c0a3c909eae to your computer and use it in GitHub Desktop.
Save VictorKabata/9e275f1db88a425ec5137c0a3c909eae to your computer and use it in GitHub Desktop.
Simple graphQL API built using Golang
package main
import (
"fmt"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"log"
"net/http"
"time"
)
type Note struct {
ID int `json: "id"`
Title string `json: "title"`
Description string `json: "description"`
Created_At string `json: "created_at"`
}
/**Mock database*/
var notes = []Note{
{1, "Title 1", "Description 1", time.Now().UTC().String()},
{2, "Title 2", "Description 2", time.Now().UTC().String()},
{3, "Title 3", "Description 3", time.Now().UTC().String()},
}
func main() {
fmt.Println("Starting graphql server...")
noteType := graphql.NewObject(
graphql.ObjectConfig{
Name: "Note",
Description: "User generated note",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.Int,
},
"title": &graphql.Field{
Type: graphql.String,
},
"description": &graphql.Field{
Type: graphql.String,
},
"created_at": &graphql.Field{
Type: graphql.String,
},
// Addition fields
},
},
)
fields := graphql.Fields{
"notes": &graphql.Field{
Name: "Get all notes",
Type: graphql.NewList(noteType),
Description: "Get list of all notes",
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
return notes, nil
},
},
}
rootQuery := graphql.ObjectConfig{
Name: "RootQuery",
Fields: fields,
}
schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("Failed to create graphql schema: %v", err)
}
handler := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: false,
})
http.Handle("/graphql", handler)
http.Handle("/sandbox", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write(sandboxHTML) }))
log.Fatal(http.ListenAndServe(":8080", nil))
}
var sandboxHTML = []byte(`
<!DOCTYPE html>
<html lang="en">
<body style="margin: 0; overflow-x: hidden; overflow-y: hidden">
<div id="sandbox" style="height:100vh; width:100vw;"></div>
<script src="https://embeddable-sandbox.cdn.apollographql.com/_latest/embeddable-sandbox.umd.production.min.js"></script>
<script>
new window.EmbeddedSandbox({
target: "#sandbox",
// Pass through your server href if you are embedding on an endpoint.
// Otherwise, you can pass whatever endpoint you want Sandbox to start up with here.
initialEndpoint: "http://localhost:8080/graphql",
});
// advanced options: https://www.apollographql.com/docs/studio/explorer/sandbox#embedding-sandbox
</script>
</body>
</html>`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment