Skip to content

Commit

Permalink
Use net/http instead of graphql-go-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelnguyen committed Oct 29, 2015
1 parent 8bd7328 commit e856222
Showing 1 changed file with 52 additions and 44 deletions.
96 changes: 52 additions & 44 deletions examples/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import (
"io/ioutil"
"net/http"

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

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
Expand Down Expand Up @@ -39,37 +46,60 @@ var userType = types.NewGraphQLObjectType(
- 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,
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
}
},
},
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,
},
)

type User struct {
Id string `json:"id"`
Name string `json:"name"`
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
Expand All @@ -87,25 +117,3 @@ func importJsonDataFromFile(fileName string, result interface{}) (isOK bool) {
}
return
}

var data map[string]User

func main() {
_ = importJsonDataFromFile("data.json", &data)
// create a graphl-go HTTP handler with our previously defined schema
// and we also set it to return pretty JSON output
h := gqlhandler.New(&gqlhandler.Config{
Schema: &schema,
Pretty: true,
})

// serve a GraphQL endpoint at `/graphql`
http.Handle("/graphql", h)

fmt.Println("Now server is running on port 8080")
fmt.Println("Test with Post : curl -XPOST http://localhost:8080/graphql -H 'Content-Type: application/graphql' -d 'query Root{ user(id:\"1\"){name} }'")
fmt.Println("Test with Get : curl -g \"http://localhost:8080/graphql?query={user(id:%221%22){name}}\"")
// and serve!
http.ListenAndServe(":8080", nil)

}

0 comments on commit e856222

Please # to comment.