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

Allow public access to the generated __{Operation}Input variables #358

Open
jfeng-dev opened this issue Oct 17, 2024 · 0 comments
Open

Allow public access to the generated __{Operation}Input variables #358

jfeng-dev opened this issue Oct 17, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@jfeng-dev
Copy link

jfeng-dev commented Oct 17, 2024

Is your feature request related to a problem? Please describe.

I'd like to hook into the Marshal/UnMarshal methods on the __{Operation}Input struct definition to facilitate unit testing.

We're using httpmock and making assertions on what is being sent in the body of the request and mocking the data to be returned.

Since the request is generated with custom json marshalers from genqlient, there doesn't seem to be a good way to test that the body of the request is as expected without either writing out a []byte{"json":"here"} definition or creating a duplicate struct type that marshals similarly.

Describe the solution you'd like

I'd like to add a configuration option to remove the prepended __ in the code here

For example, given something like this in generated.go

// __AddCustomInput is used internally by genqlient
type __AddCustomInput struct {
	ID           string     `json:"ID"`
	Title        string     `json:"title"`
	Quantity     int        `json:"quantity"`
	Price        MoneyInput `json:"price"`
}

I'd like to declare the following in a unit test

expectedReq := graphql.Request{
	OpName: "AddCustom",
	Query:  AddCustom_Operation,
	Variables: &__AddCustomInput{ // I would want this to be publicly accessible
		ID: ID,
		Title:        title,
		Quantity:     quantity,
		Price:        price,
	},
}

reqBytes, _ := json.Marshal(expectedReq) // I want this to marshal using the Marshal method on __AddCustomInput
httpMock.Handler.On(
	"other",
	"fields",
	"to",
	"match on",
	[]bytes{reqBytes}, // so that this matches what genqlient would do internally
).Return(httpmock.Response{
	Status: 200,
	Header: http.Header{},
	Body: []byte(expectedRes),
})

Describe alternatives you've considered

I tried using mockery and testing against the MakeRequest call, but it runs into the same issue with the private __{Operation}Input type

Additional context

Looking in the example integration tests, it doesn't seem like the request variables are being inspected as noted here and in the test here with resolver here

@jfeng-dev jfeng-dev added the enhancement New feature or request label Oct 17, 2024
@jfeng-dev jfeng-dev changed the title Allow public access to the generated __{model}Input variables Allow public access to the generated __{Operation}Input variables Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant