Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create basic example follow http://graphql.org/docs/getting-started/ #21

Merged
merged 5 commits into from
Oct 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/http/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"1": {
"id": "1",
"name": "Dan"
},
"2": {
"id": "2",
"name": "Lee"
},
"3": {
"id": "3",
"name": "Nick"
}
}
119 changes: 119 additions & 0 deletions examples/http/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/chris-ramon/graphql-go"
"github.com/chris-ramon/graphql-go/types"
)

type User struct {
Id string `json:"id"`
Name string `json:"name"`
}

var data map[string]User

/*
Create User object type with fields "id" and "name" by using GraphQLObjectTypeConfig:
- Name: name of object type
- Fields: a map of fields by using GraphQLFieldConfigMap
Setup type of field use GraphQLFieldConfig
*/
var userType = types.NewGraphQLObjectType(
types.GraphQLObjectTypeConfig{
Name: "User",
Fields: types.GraphQLFieldConfigMap{
"id": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
"name": &types.GraphQLFieldConfig{
Type: types.GraphQLString,
},
},
},
)

/*
Create Query object type with fields "user" has type [userType] by using GraphQLObjectTypeConfig:
- Name: name of object type
- Fields: a map of fields by using GraphQLFieldConfigMap
Setup type of field use GraphQLFieldConfig to define:
- Type: type of field
- Args: arguments to query with current field
- Resolve: function to query data using params from [Args] and return value with current type
*/
var queryType = types.NewGraphQLObjectType(
types.GraphQLObjectTypeConfig{
Name: "Query",
Fields: types.GraphQLFieldConfigMap{
"user": &types.GraphQLFieldConfig{
Type: userType,
Args: types.GraphQLFieldConfigArgumentMap{
"id": &types.GraphQLArgumentConfig{
Type: types.GraphQLString,
},
},
Resolve: func(p types.GQLFRParams) interface{} {
idQuery, isOK := p.Args["id"].(string)
if isOK {
return data[idQuery]
} else {
return nil
}
},
},
},
})

var schema, _ = types.NewGraphQLSchema(
types.GraphQLSchemaConfig{
Query: queryType,
},
)

func executeQuery(query string, schema types.GraphQLSchema) *types.GraphQLResult {
graphqlParams := gql.GraphqlParams{
Schema: schema,
RequestString: query,
}
resultChannel := make(chan *types.GraphQLResult)
go gql.Graphql(graphqlParams, resultChannel)
result := <-resultChannel
if len(result.Errors) > 0 {
fmt.Println("wrong result, unexpected errors: %v", result.Errors)
}
return result
}

func main() {
_ = importJsonDataFromFile("data.json", &data)

http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
result := executeQuery(r.URL.Query()["query"][0], schema)
json.NewEncoder(w).Encode(result)
})

fmt.Println("Now server is running on port 8080")
fmt.Println("Test with Get : curl -g \"http://localhost:8080/graphql?query={user(id:%221%22){name}}\"")
http.ListenAndServe(":8080", nil)
}

//Helper function to import json from file to map
func importJsonDataFromFile(fileName string, result interface{}) (isOK bool) {
isOK = true
content, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Print("Error:", err)
isOK = false
}
err = json.Unmarshal(content, result)
if err != nil {
isOK = false
fmt.Print("Error:", err)
}
return
}