Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,125 additions
and
16 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
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,141 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/graphql-go/graphql" | ||
"github.com/graphql-go/graphql/language/ast" | ||
) | ||
|
||
type CustomID struct { | ||
value string | ||
} | ||
|
||
func (id *CustomID) String() string { | ||
return id.value | ||
} | ||
|
||
func NewCustomID(v string) *CustomID { | ||
return &CustomID{value: v} | ||
} | ||
|
||
var CustomScalarType = graphql.NewScalar(graphql.ScalarConfig{ | ||
Name: "CustomScalarType", | ||
Description: "The `CustomScalarType` scalar type represents an ID Object.", | ||
// Serialize serializes `CustomID` to string. | ||
Serialize: func(value interface{}) interface{} { | ||
switch value := value.(type) { | ||
case CustomID: | ||
return value.String() | ||
case *CustomID: | ||
v := *value | ||
return v.String() | ||
default: | ||
return nil | ||
} | ||
}, | ||
// ParseValue parses GraphQL variables from `string` to `CustomID`. | ||
ParseValue: func(value interface{}) interface{} { | ||
switch value := value.(type) { | ||
case string: | ||
return NewCustomID(value) | ||
case *string: | ||
return NewCustomID(*value) | ||
default: | ||
return nil | ||
} | ||
}, | ||
// ParseLiteral parses GraphQL AST value to `CustomID`. | ||
ParseLiteral: func(valueAST ast.Value) interface{} { | ||
switch valueAST := valueAST.(type) { | ||
case *ast.StringValue: | ||
return NewCustomID(valueAST.Value) | ||
default: | ||
return nil | ||
} | ||
}, | ||
}) | ||
|
||
type Customer struct { | ||
ID *CustomID `json:"id"` | ||
} | ||
|
||
var CustomerType = graphql.NewObject(graphql.ObjectConfig{ | ||
Name: "Customer", | ||
Fields: graphql.Fields{ | ||
"id": &graphql.Field{ | ||
Type: CustomScalarType, | ||
}, | ||
}, | ||
}) | ||
|
||
func main() { | ||
schema, err := graphql.NewSchema(graphql.SchemaConfig{ | ||
Query: graphql.NewObject(graphql.ObjectConfig{ | ||
Name: "Query", | ||
Fields: graphql.Fields{ | ||
"customers": &graphql.Field{ | ||
Type: graphql.NewList(CustomerType), | ||
Args: graphql.FieldConfigArgument{ | ||
"id": &graphql.ArgumentConfig{ | ||
Type: CustomScalarType, | ||
}, | ||
}, | ||
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | ||
// id := p.Args["id"] | ||
// log.Printf("id from arguments: %+v", id) | ||
customers := []Customer{ | ||
Customer{ID: NewCustomID("fb278f2a4a13f")}, | ||
} | ||
return customers, nil | ||
}, | ||
}, | ||
}, | ||
}), | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
query := ` | ||
query { | ||
customers { | ||
id | ||
} | ||
} | ||
` | ||
/* | ||
queryWithVariable := ` | ||
query($id: CustomScalarType) { | ||
customers(id: $id) { | ||
id | ||
} | ||
} | ||
` | ||
*/ | ||
/* | ||
queryWithArgument := ` | ||
query { | ||
customers(id: "5b42ba57289") { | ||
id | ||
} | ||
} | ||
` | ||
*/ | ||
result := graphql.Do(graphql.Params{ | ||
Schema: schema, | ||
RequestString: query, | ||
VariableValues: map[string]interface{}{ | ||
"id": "5b42ba57289", | ||
}, | ||
}) | ||
if len(result.Errors) > 0 { | ||
log.Fatal(result) | ||
} | ||
b, err := json.Marshal(result) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(string(b)) | ||
} |
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,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/graphql-go/graphql" | ||
) | ||
|
||
type User struct { | ||
ID int `json:"id"` | ||
} | ||
|
||
var UserType = graphql.NewObject(graphql.ObjectConfig{ | ||
Name: "User", | ||
Fields: graphql.Fields{ | ||
"id": &graphql.Field{ | ||
Type: graphql.Int, | ||
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | ||
rootValue := p.Info.RootValue.(map[string]interface{}) | ||
if rootValue["data-from-parent"] == "ok" && | ||
rootValue["data-before-execution"] == "ok" { | ||
user := p.Source.(User) | ||
return user.ID, nil | ||
} | ||
return nil, nil | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
func main() { | ||
schema, err := graphql.NewSchema(graphql.SchemaConfig{ | ||
Query: graphql.NewObject(graphql.ObjectConfig{ | ||
Name: "Query", | ||
Fields: graphql.Fields{ | ||
"users": &graphql.Field{ | ||
Type: graphql.NewList(UserType), | ||
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | ||
rootValue := p.Info.RootValue.(map[string]interface{}) | ||
rootValue["data-from-parent"] = "ok" | ||
result := []User{ | ||
User{ID: 1}, | ||
} | ||
return result, nil | ||
|
||
}, | ||
}, | ||
}, | ||
}), | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ctx := context.WithValue(context.Background(), "currentUser", User{ID: 100}) | ||
// Instead of trying to modify context within a resolve function, use: | ||
// `graphql.Params.RootObject` is a mutable optional variable and available on | ||
// each resolve function via: `graphql.ResolveParams.Info.RootValue`. | ||
rootObject := map[string]interface{}{ | ||
"data-before-execution": "ok", | ||
} | ||
result := graphql.Do(graphql.Params{ | ||
Context: ctx, | ||
RequestString: "{ users { id } }", | ||
RootObject: rootObject, | ||
Schema: schema, | ||
}) | ||
b, err := json.Marshal(result) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("%s\n", string(b)) // {"data":{"users":[{"id":1}]}} | ||
} |
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
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
Oops, something went wrong.