Skip to content

Commit d4051ab

Browse files
authored
docs: update README for v0.7.0 features (shurcooL#34)
* update docs for v0.7.0 features * fix markdown table of content
1 parent 413dfb6 commit d4051ab

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ For more information, see package [`github.com/shurcooL/githubv4`](https://githu
2222
- [Custom scalar tag](#custom-scalar-tag)
2323
- [Skip GraphQL field](#skip-graphql-field)
2424
- [Inline Fragments](#inline-fragments)
25+
- [Specify GraphQL type name](#specify-graphql-type-name)
2526
- [Mutations](#mutations)
2627
- [Mutations Without Fields](#mutations-without-fields)
28+
- [Execute](#execute)
2729
- [Subscription](#subscription)
2830
- [Usage](#usage-1)
2931
- [Subscribe](#subscribe)
@@ -308,6 +310,31 @@ fmt.Println(q.Hero.Height)
308310
// 0
309311
```
310312
313+
### Specify GraphQL type name
314+
315+
The GraphQL type is automatically inferred from Go type by reflection. However, it's cumbersome in some use cases, e.g lowercase names. In Go, a type name with a first lowercase letter is considered private. If we need to reuse it for other packages, there are 2 approaches: type alias or implement `GetGraphQLType` method.
316+
317+
```go
318+
type UserReviewInput struct {
319+
Review String
320+
UserID String
321+
}
322+
323+
// type alias
324+
type user_review_input UserReviewInput
325+
// or implement GetGraphQLType method
326+
func (u UserReviewInput) GetGraphQLType() string { return "user_review_input" }
327+
328+
variables := map[string]interface{}{
329+
"input": UserReviewInput{}
330+
}
331+
332+
//query arguments without GetGraphQLType() defined
333+
//($input: UserReviewInput!)
334+
//query arguments with GetGraphQLType() defined:w
335+
//($input: user_review_input!)
336+
```
337+
311338
### Mutations
312339
313340
Mutations often require information that you can only find out by performing a query first. Let's suppose you've already done that.
@@ -408,6 +435,28 @@ fmt.Printf("Created a review: %s.\n", m.CreateReview)
408435
// Created a review: .
409436
```
410437
438+
### Execute
439+
440+
The `Exec` function allows you to executing pre-built queries. While using reflection to build queries is convenient as you get some resemblance of type safety, it gets very cumbersome when you need to create queries semi-dynamically. For instance, imagine you are building a CLI tool to query data from a graphql endpoint and you want users to be able to narrow down the query by passing cli flags or something.
441+
442+
```Go
443+
// filters would be built dynamically somehow from the command line flags
444+
filters := []string{
445+
`fieldA: {subfieldA: {_eq: "a"}}`,
446+
`fieldB: {_eq: "b"}`,
447+
...
448+
}
449+
450+
query := "query{something(where: {" + strings.Join(filters, ", ") + "}){id}}"
451+
res := struct {
452+
Somethings []Something
453+
}{}
454+
455+
if err := client.Exec(ctx, query, &res, map[string]any{}); err != nil {
456+
panic(err)
457+
}
458+
```
459+
411460
### Subscription
412461
413462
#### Usage

0 commit comments

Comments
 (0)