Skip to content

Commit

Permalink
Merge pull request #201 from sonatard/add-example
Browse files Browse the repository at this point in the history
Add examples
  • Loading branch information
Yamashou authored Feb 12, 2024
2 parents 049b0ac + aae0c76 commit 3f30080
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 0 deletions.
18 changes: 18 additions & 0 deletions example/autobind/.gqlgenc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
schema:
- ./schema/schema.graphql
model:
filename: ./model/models_gen.go
autobind:
- github.com/Yamashou/gqlgenc/example/autobind/model
client:
filename: ./gen/client.go
models:
DateTime:
model: github.com/99designs/gqlgen/graphql.Time
query:
- "./query/*.graphql"
generate:
clientV2: true
clientInterfaceName: "GithubGraphQLClient"
query: false
mutation: false
83 changes: 83 additions & 0 deletions example/autobind/gen/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions example/autobind/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"fmt"
"net/http"
"os"

"github.com/Yamashou/gqlgenc/client"
"github.com/Yamashou/gqlgenc/clientv2"
"github.com/Yamashou/gqlgenc/example/github/gen"
)

func main() {
// This example only read public repository. You don't need to select scopes.
token := os.Getenv("GITHUB_TOKEN")
ctx := context.Background()

githubClient := &gen.Client{
Client: clientv2.NewClient(http.DefaultClient, "https://api.github.com/graphql", nil,
func(ctx context.Context, req *http.Request, gqlInfo *clientv2.GQLRequestInfo, res interface{}, next clientv2.RequestInterceptorFunc) error {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))

return next(ctx, req, gqlInfo, res)
}),
}
getUser, err := githubClient.GetUser(ctx, 10, 10)
if err != nil {
if handledError, ok := err.(*client.ErrorResponse); ok {
fmt.Fprintf(os.Stderr, "handled error: %s\n", handledError.Error())
} else {
fmt.Fprintf(os.Stderr, "unhandled error: %s\n", err.Error())
}
os.Exit(1)
}

fmt.Println(*getUser.Viewer.Name, getUser.Viewer.Repositories.Nodes[0].Name)
for _, repository := range getUser.Viewer.Repositories.Nodes {
fmt.Println(repository.Name)
for _, language := range repository.Languages.Nodes {
fmt.Println(language.Name)
}

res, err := githubClient.GetNode(ctx, repository.ID)
if err != nil {
if handledError, ok := err.(*client.ErrorResponse); ok {
fmt.Fprintf(os.Stderr, "handled error: %s\n", handledError.Error())
} else {
fmt.Fprintf(os.Stderr, "unhandled error: %s\n", err.Error())
}
os.Exit(1)
}

fmt.Println(res.Node.ID, res.Node.Repository.Name)
}
}
6 changes: 6 additions & 0 deletions example/autobind/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

type Profile struct {
ID string `json:"id"`
Name string `json:"name"`
}
14 changes: 14 additions & 0 deletions example/autobind/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions example/autobind/query/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query GetUserProfileName {
user {
profile {
name
}
}
}
22 changes: 22 additions & 0 deletions example/autobind/schema/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
schema {
query: Query
mutation: Mutation
}

type Query {
user: User!
}

type Mutation {
dummy: String!
}

type User {
id: ID!
profile: Profile!
}

type Profile {
id: ID!
name: String!
}
16 changes: 16 additions & 0 deletions example/no-autobind/.gqlgenc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema:
- ./schema/schema.graphql
model:
filename: ./model/models_gen.go
client:
filename: ./gen/client.go
models:
DateTime:
model: github.com/99designs/gqlgen/graphql.Time
query:
- "./query/*.graphql"
generate:
clientV2: true
clientInterfaceName: "GithubGraphQLClient"
query: false
mutation: false
83 changes: 83 additions & 0 deletions example/no-autobind/gen/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions example/no-autobind/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"fmt"
"net/http"
"os"

"github.com/Yamashou/gqlgenc/client"
"github.com/Yamashou/gqlgenc/clientv2"
"github.com/Yamashou/gqlgenc/example/github/gen"
)

func main() {
// This example only read public repository. You don't need to select scopes.
token := os.Getenv("GITHUB_TOKEN")
ctx := context.Background()

githubClient := &gen.Client{
Client: clientv2.NewClient(http.DefaultClient, "https://api.github.com/graphql", nil,
func(ctx context.Context, req *http.Request, gqlInfo *clientv2.GQLRequestInfo, res interface{}, next clientv2.RequestInterceptorFunc) error {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))

return next(ctx, req, gqlInfo, res)
}),
}
getUser, err := githubClient.GetUser(ctx, 10, 10)
if err != nil {
if handledError, ok := err.(*client.ErrorResponse); ok {
fmt.Fprintf(os.Stderr, "handled error: %s\n", handledError.Error())
} else {
fmt.Fprintf(os.Stderr, "unhandled error: %s\n", err.Error())
}
os.Exit(1)
}

fmt.Println(*getUser.Viewer.Name, getUser.Viewer.Repositories.Nodes[0].Name)
for _, repository := range getUser.Viewer.Repositories.Nodes {
fmt.Println(repository.Name)
for _, language := range repository.Languages.Nodes {
fmt.Println(language.Name)
}

res, err := githubClient.GetNode(ctx, repository.ID)
if err != nil {
if handledError, ok := err.(*client.ErrorResponse); ok {
fmt.Fprintf(os.Stderr, "handled error: %s\n", handledError.Error())
} else {
fmt.Fprintf(os.Stderr, "unhandled error: %s\n", err.Error())
}
os.Exit(1)
}

fmt.Println(res.Node.ID, res.Node.Repository.Name)
}
}
19 changes: 19 additions & 0 deletions example/no-autobind/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions example/no-autobind/query/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query GetUserProfileName {
user {
profile {
name
}
}
}
Loading

0 comments on commit 3f30080

Please sign in to comment.