Skip to content

Instantly share code, notes, and snippets.

@donvito
Last active September 18, 2019 04:27
Show Gist options
  • Save donvito/8eea71b4c8c176d0df791bb7bd35c75c to your computer and use it in GitHub Desktop.
Save donvito/8eea71b4c8c176d0df791bb7bd35c75c to your computer and use it in GitHub Desktop.
GraphQL Go
[
{
"id": 1,
"position": "Software Engineer",
"company": "Apple",
"description": "job description",
"skillsRequired": ["Go", "GraphQL"],
"location": "location",
"employmentType": "full-time"
},
{
"id": 2,
"position": "Software Engineering Manager",
"company": "Google",
"description": "job description",
"skillsRequired": ["Scrum", "JIRA"],
"location": "location",
"employmentType": "full-time"
},
{
"id": 3,
"position": "System Engineer",
"company": "Microsoft",
"description": "job description",
"skillsRequired": ["Linux", "Kubernetes"],
"location": "location",
"employmentType": "full-time"
},
{
"id": 4,
"position": "Frontend Engineer",
"company": "Facebook",
"description": "job description",
"skillsRequired": ["React", "GraphQL"],
"location": "location",
"employmentType": "full-time"
}
]
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/graphql-go/graphql"
)
//Job struct
type Job struct {
ID int `json:"id"`
Position string `json:"position"`
Company string `json:"company"`
Description string `json:"description"`
SkillsRequired []string `json:"skillsRequired"`
Location string `json:"location"`
EmploymentType string `json:"employmentType"`
}
var jobType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Job",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.Int,
},
"position": &graphql.Field{
Type: graphql.String,
},
"skillsRequired": &graphql.Field{
Type: graphql.NewList(graphql.String),
},
},
},
)
func main() {
//Open the file data.json
jsonf, err := os.Open("data.json")
if err != nil {
log.Fatalf("failed to open json file, error: %v", err)
}
//Load the data into jsonDataFromFile - a byte array
jsonDataFromFile, _ := ioutil.ReadAll(jsonf)
defer jsonf.Close()
var jobsData []Job
err = json.Unmarshal(jsonDataFromFile, &jobsData)
if err != nil {
log.Fatalf("failed to parse json, error: %v", err)
}
// Define the GraphQL Schema
fields := graphql.Fields{
"jobs": &graphql.Field{
Type: graphql.NewList(jobType),
Description: "All Jobs",
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
return jobsData, 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 new schema, error: %v", err)
}
// Query
query := `
{
jobs {
id
position
skillsRequired
}
}
`
params := graphql.Params{Schema: schema, RequestString: query}
r := graphql.Do(params)
if len(r.Errors) > 0 {
log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
}
rJSON, _ := json.Marshal(r)
fmt.Printf("%s \n", rJSON)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment