Skip to content

Commit

Permalink
Merge pull request #21 from thanhliem89dn/master
Browse files Browse the repository at this point in the history
Create basic example follow http://graphql.org/docs/getting-started/
  • Loading branch information
chris-ramon committed Oct 30, 2015
2 parents 8c4c840 + e856222 commit 3b4b43f
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
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
}

0 comments on commit 3b4b43f

Please sign in to comment.