在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):shurcooL/graphql开源软件地址(OpenSource Url):https://github.com/shurcooL/graphql开源编程语言(OpenSource Language):Go 100.0%开源软件介绍(OpenSource Introduction):graphqlPackage For more information, see package Installation
go get -u github.com/shurcooL/graphql UsageConstruct a GraphQL client, specifying the GraphQL server URL. Then, you can use it to make GraphQL queries and mutations. client := graphql.NewClient("https://example.com/graphql", nil)
// Use client... AuthenticationSome GraphQL servers may require authentication. The import "golang.org/x/oauth2"
func main() {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GRAPHQL_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient("https://example.com/graphql", httpClient)
// Use client... Simple QueryTo make a GraphQL query, you need to define a corresponding Go type. For example, to make the following GraphQL query: query {
me {
name
}
} You can define this variable: var query struct {
Me struct {
Name graphql.String
}
} Then call err := client.Query(context.Background(), &query, nil)
if err != nil {
// Handle error.
}
fmt.Println(query.Me.Name)
// Output: Luke Skywalker Arguments and VariablesOften, you'll want to specify arguments on some fields. You can use the For example, to make the following GraphQL query: {
human(id: "1000") {
name
height(unit: METER)
}
} You can define this variable: var q struct {
Human struct {
Name graphql.String
Height graphql.Float `graphql:"height(unit: METER)"`
} `graphql:"human(id: \"1000\")"`
} Then call err := client.Query(context.Background(), &q, nil)
if err != nil {
// Handle error.
}
fmt.Println(q.Human.Name)
fmt.Println(q.Human.Height)
// Output:
// Luke Skywalker
// 1.72 However, that'll only work if the arguments are constant and known in advance. Otherwise, you will need to make use of variables. Replace the constants in the struct field tag with variable names: var q struct {
Human struct {
Name graphql.String
Height graphql.Float `graphql:"height(unit: $unit)"`
} `graphql:"human(id: $id)"`
} Then, define a variables := map[string]interface{}{
"id": graphql.ID(id),
"unit": starwars.LengthUnit("METER"),
} Finally, call err := client.Query(context.Background(), &q, variables)
if err != nil {
// Handle error.
} Inline FragmentsSome GraphQL queries contain inline fragments. You can use the For example, to make the following GraphQL query: {
hero(episode: "JEDI") {
name
... on Droid {
primaryFunction
}
... on Human {
height
}
}
} You can define this variable: var q struct {
Hero struct {
Name graphql.String
Droid struct {
PrimaryFunction graphql.String
} `graphql:"... on Droid"`
Human struct {
Height graphql.Float
} `graphql:"... on Human"`
} `graphql:"hero(episode: \"JEDI\")"`
} Alternatively, you can define the struct types corresponding to inline fragments, and use them as embedded fields in your query: type (
DroidFragment struct {
PrimaryFunction graphql.String
}
HumanFragment struct {
Height graphql.Float
}
)
var q struct {
Hero struct {
Name graphql.String
DroidFragment `graphql:"... on Droid"`
HumanFragment `graphql:"... on Human"`
} `graphql:"hero(episode: \"JEDI\")"`
} Then call err := client.Query(context.Background(), &q, nil)
if err != nil {
// Handle error.
}
fmt.Println(q.Hero.Name)
fmt.Println(q.Hero.PrimaryFunction)
fmt.Println(q.Hero.Height)
// Output:
// R2-D2
// Astromech
// 0 MutationsMutations often require information that you can only find out by performing a query first. Let's suppose you've already done that. For example, to make the following GraphQL mutation: mutation($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
variables {
"ep": "JEDI",
"review": {
"stars": 5,
"commentary": "This is a great movie!"
}
} You can define: var m struct {
CreateReview struct {
Stars graphql.Int
Commentary graphql.String
} `graphql:"createReview(episode: $ep, review: $review)"`
}
variables := map[string]interface{}{
"ep": starwars.Episode("JEDI"),
"review": starwars.ReviewInput{
Stars: graphql.Int(5),
Commentary: graphql.String("This is a great movie!"),
},
} Then call err := client.Mutate(context.Background(), &m, variables)
if err != nil {
// Handle error.
}
fmt.Printf("Created a %v star review: %v\n", m.CreateReview.Stars, m.CreateReview.Commentary)
// Output:
// Created a 5 star review: This is a great movie! Directories
License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论