Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce dynamic GraphQL Builder #86

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

hgiasac
Copy link

@hgiasac hgiasac commented Apr 8, 2023

Introduce the Builder structure that wraps the naive [][2]interface{} type. It helps construct multiple queries to a request that needs to be conditionally added.

You might need to dynamically multiple queries or mutations in a single request. It isn't convenient with static structures. Builder helps us construct many queries flexibly.

For example, to make the following GraphQL mutation:

query($userId: String!, $disabled: Boolean!, $limit: Int!) {
	userByPk(userId: $userId) { id name }
	groups(disabled: $disabled) { id user_permissions }
	topUsers: users(limit: $limit) { id name }
}

# variables {
# 	"userId": "1",
# 	"disabled": false,
# 	"limit": 5
# }

You can define:

type User struct {
	ID string
	Name string
}

var groups []struct {
	ID string
	Permissions []string `graphql:"user_permissions"`
}

var userByPk User
var topUsers []User

builder := graphql.NewBuilder().
	Query("userByPk(userId: $userId)", &userByPk).
	Query("groups(disabled: $disabled)", &groups).
	Query("topUsers: users(limit: $limit)", &topUsers).
	Variables(map[string]interface{}{
		"userId": 1,
		"disabled": false,
		"limit": 5,
	})

query, variables, err := builder.Build()
if err != nil {
	return err
}

err = client.Query(context.Background(), query, variables)
if err != nil {
	return err
}

// or use Query / Mutate shortcut methods
err = builder.Query(client)
if err != nil {
	return err
}

@hgiasac hgiasac changed the title feat: introduce QueryBuilder feat: introduce dynamic GraphQL Builder Apr 15, 2023
@github-actions
Copy link

github-actions bot commented May 3, 2023

Code Coverage

Package Line Rate Health
github.com/hasura/go-graphql-client 68%
github.com/hasura/go-graphql-client/ident 100%
github.com/hasura/go-graphql-client/pkg/jsonutil 86%
Summary 73% (1539 / 2114)

Minimum allowed line rate is 60%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant