-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from thanhliem89dn/master
Create basic example follow http://graphql.org/docs/getting-started/
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |