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

Stub generation #537

Merged
merged 2 commits into from
Feb 7, 2019
Merged

Stub generation #537

merged 2 commits into from
Feb 7, 2019

Conversation

vektah
Copy link
Collaborator

@vektah vektah commented Feb 7, 2019

The tests on code generation are getting very long, relying on a large object defined at the bottom of a ~1000 line file.

This adds a codegen plugin that generates a Stub that fits the resolver interface, for the generated server it looks like this:

type Stub struct {
	QueryResolver struct {
		User                   func(ctx context.Context, id int) (User, error)
		NullableArg            func(ctx context.Context, arg *int) (*string, error)
		DirectiveArg           func(ctx context.Context, arg string) (*string, error)
		DirectiveNullableArg   func(ctx context.Context, arg *int, arg2 *int) (*string, error)
		// lots of other unimportant fields removed
	}
	SubscriptionResolver struct {
		Updated     func(ctx context.Context) (<-chan string, error)
		InitPayload func(ctx context.Context) (<-chan string, error)
	}
	UserResolver struct {
		Friends func(ctx context.Context, obj *User) ([]User, error)
	}
}

which now allows each test to easily define the one or two resolvers it expects to hit simply:

resolvers := &Stub{}
resolvers.QueryResolver.Valid = func(ctx context.Context) (s string, e error) {
	return "Ok", nil
}

resolvers.QueryResolver.ErrorBubble = func(ctx context.Context) (i *Error, e error) {
	return &Error{ID: "E1234"}, nil
}

srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
c := client.New(srv.URL)

And everything else will panic.

@vektah vektah merged commit 48a7e07 into next Feb 7, 2019
@vektah vektah deleted the stub-generation branch February 7, 2019 10:04
cgxxv pushed a commit to cgxxv/gqlgen that referenced this pull request Mar 25, 2022
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