You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+49Lines changed: 49 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -22,8 +22,10 @@ For more information, see package [`github.com/shurcooL/githubv4`](https://githu
22
22
- [Custom scalar tag](#custom-scalar-tag)
23
23
- [Skip GraphQL field](#skip-graphql-field)
24
24
- [Inline Fragments](#inline-fragments)
25
+
- [Specify GraphQL type name](#specify-graphql-type-name)
25
26
- [Mutations](#mutations)
26
27
- [Mutations Without Fields](#mutations-without-fields)
28
+
- [Execute](#execute)
27
29
- [Subscription](#subscription)
28
30
- [Usage](#usage-1)
29
31
- [Subscribe](#subscribe)
@@ -308,6 +310,31 @@ fmt.Println(q.Hero.Height)
308
310
// 0
309
311
```
310
312
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
+
311
338
### Mutations
312
339
313
340
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)
408
435
// Created a review: .
409
436
```
410
437
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
0 commit comments