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

Further flush out ability to auto-bind to functions and types. #558

Closed
wants to merge 4 commits into from

Conversation

alancnet
Copy link

@alancnet alancnet commented Jul 28, 2020

Hello, graphql-go authors.

I began this PR because I encountered a bug in BindFields where the name given to a type would be the JSON field name, which resulted in conflicts. I was using that function to automatically bind functions, and create types, so that the resolver functions themselves could be reused as regular exported functions.

I migrated that code into this project as well, as I feel it offers a significant improvement to dev experience.

type GreetingInput struct {
	Name string `json:"name"`
}

func Greeting(input GreetingInput) string {
	return fmt.Sprintf("Hello %s", input.Name)
}
...
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{
	"greeting": graphql.Bind(Greeting),
}}

or almost any manner of function along these lines:

func MyFunction(ctx context.Context, input MyFunctionInput) (output MyFunctionOutput, error) ...
func MyFunction(ctx *context.Context, input *MyFunctionInput) (output *MyFunctionOutput, error) ...
func MyFunction(ctx context.Context) (output MyFunctionOutput, error) ...
func MyFunction(input MyFunctionInput) (output MyFunctionOutput, error) ...
func MyFunction(input MyFunctionInput) (output MyFunctionOutput) ...
func MyFunction() (output MyFunctionOutput) ...
func MyFunction() (output string) ...

or constant

rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{
	"version": graphql.Bind(1.1),
}}

Cheers!

-Alan 🤖

@alancnet alancnet changed the title Further flush out ability to auto-bind to functions and types. [WIP] Further flush out ability to auto-bind to functions and types. Jul 28, 2020
@coveralls
Copy link

coveralls commented Jul 28, 2020

Coverage Status

Coverage decreased (-0.03%) to 92.344% when pulling 336c4d6 on teamjobot:master into 9441c49 on graphql-go:master.

@alancnet alancnet changed the title [WIP] Further flush out ability to auto-bind to functions and types. Further flush out ability to auto-bind to functions and types. Jul 28, 2020
@alancnet alancnet closed this Jul 28, 2020
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.

2 participants