Add GraphQL Extensions (#184)
This enables accessing the Extensions field, as defined in the response format: https://spec.graphql.org/October2021/#sec-Response-Format Extensions can be enabled using configuration option use_extensions. This will change the return parameters of the generated client functions. Making it a breaking change if enabled. Since extensions are untyped as defined in the spec, the Client will return an interface of type map[string]interface{}.
This commit is contained in:
+296
-177
@@ -2995,6 +2995,30 @@ func (v *queryWithVariablesUser) GetName() string { return v.Name }
|
||||
// GetLuckyNumber returns queryWithVariablesUser.LuckyNumber, and is useful for accessing the field via an interface.
|
||||
func (v *queryWithVariablesUser) GetLuckyNumber() int { return v.LuckyNumber }
|
||||
|
||||
// simpleQueryExtMeUser includes the requested fields of the GraphQL type User.
|
||||
type simpleQueryExtMeUser struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
LuckyNumber int `json:"luckyNumber"`
|
||||
}
|
||||
|
||||
// GetId returns simpleQueryExtMeUser.Id, and is useful for accessing the field via an interface.
|
||||
func (v *simpleQueryExtMeUser) GetId() string { return v.Id }
|
||||
|
||||
// GetName returns simpleQueryExtMeUser.Name, and is useful for accessing the field via an interface.
|
||||
func (v *simpleQueryExtMeUser) GetName() string { return v.Name }
|
||||
|
||||
// GetLuckyNumber returns simpleQueryExtMeUser.LuckyNumber, and is useful for accessing the field via an interface.
|
||||
func (v *simpleQueryExtMeUser) GetLuckyNumber() int { return v.LuckyNumber }
|
||||
|
||||
// simpleQueryExtResponse is returned by simpleQueryExt on success.
|
||||
type simpleQueryExtResponse struct {
|
||||
Me simpleQueryExtMeUser `json:"me"`
|
||||
}
|
||||
|
||||
// GetMe returns simpleQueryExtResponse.Me, and is useful for accessing the field via an interface.
|
||||
func (v *simpleQueryExtResponse) GetMe() simpleQueryExtMeUser { return v.Me }
|
||||
|
||||
// simpleQueryMeUser includes the requested fields of the GraphQL type User.
|
||||
type simpleQueryMeUser struct {
|
||||
Id string `json:"id"`
|
||||
@@ -3022,14 +3046,10 @@ func (v *simpleQueryResponse) GetMe() simpleQueryMeUser { return v.Me }
|
||||
func failingQuery(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
) (*failingQueryResponse, error) {
|
||||
var err error
|
||||
|
||||
var retval failingQueryResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"failingQuery",
|
||||
`
|
||||
) (*failingQueryResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "failingQuery",
|
||||
Query: `
|
||||
query failingQuery {
|
||||
fail
|
||||
me {
|
||||
@@ -3037,27 +3057,29 @@ query failingQuery {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
nil,
|
||||
}
|
||||
var err error
|
||||
|
||||
var data failingQueryResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithCustomMarshal(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
date time.Time,
|
||||
) (*queryWithCustomMarshalResponse, error) {
|
||||
__input := __queryWithCustomMarshalInput{
|
||||
Date: date,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithCustomMarshalResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithCustomMarshal",
|
||||
`
|
||||
) (*queryWithCustomMarshalResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithCustomMarshal",
|
||||
Query: `
|
||||
query queryWithCustomMarshal ($date: Date!) {
|
||||
usersBornOn(date: $date) {
|
||||
id
|
||||
@@ -3066,10 +3088,22 @@ query queryWithCustomMarshal ($date: Date!) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithCustomMarshalInput{
|
||||
Date: date,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithCustomMarshalResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithCustomMarshalOptional(
|
||||
@@ -3077,18 +3111,10 @@ func queryWithCustomMarshalOptional(
|
||||
client graphql.Client,
|
||||
date *time.Time,
|
||||
id *string,
|
||||
) (*queryWithCustomMarshalOptionalResponse, error) {
|
||||
__input := __queryWithCustomMarshalOptionalInput{
|
||||
Date: date,
|
||||
Id: id,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithCustomMarshalOptionalResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithCustomMarshalOptional",
|
||||
`
|
||||
) (*queryWithCustomMarshalOptionalResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithCustomMarshalOptional",
|
||||
Query: `
|
||||
query queryWithCustomMarshalOptional ($date: Date, $id: ID) {
|
||||
userSearch(birthdate: $date, id: $id) {
|
||||
id
|
||||
@@ -3097,27 +3123,33 @@ query queryWithCustomMarshalOptional ($date: Date, $id: ID) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithCustomMarshalOptionalInput{
|
||||
Date: date,
|
||||
Id: id,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithCustomMarshalOptionalResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithCustomMarshalSlice(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
dates []time.Time,
|
||||
) (*queryWithCustomMarshalSliceResponse, error) {
|
||||
__input := __queryWithCustomMarshalSliceInput{
|
||||
Dates: dates,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithCustomMarshalSliceResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithCustomMarshalSlice",
|
||||
`
|
||||
) (*queryWithCustomMarshalSliceResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithCustomMarshalSlice",
|
||||
Query: `
|
||||
query queryWithCustomMarshalSlice ($dates: [Date!]!) {
|
||||
usersBornOnDates(dates: $dates) {
|
||||
id
|
||||
@@ -3126,27 +3158,32 @@ query queryWithCustomMarshalSlice ($dates: [Date!]!) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithCustomMarshalSliceInput{
|
||||
Dates: dates,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithCustomMarshalSliceResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithFlatten(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
ids []string,
|
||||
) (*QueryFragment, error) {
|
||||
__input := __queryWithFlattenInput{
|
||||
Ids: ids,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval QueryFragment
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithFlatten",
|
||||
`
|
||||
) (*QueryFragment, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithFlatten",
|
||||
Query: `
|
||||
query queryWithFlatten ($ids: [ID!]!) {
|
||||
... QueryFragment
|
||||
}
|
||||
@@ -3189,27 +3226,32 @@ fragment FriendsFields on User {
|
||||
name
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithFlattenInput{
|
||||
Ids: ids,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data QueryFragment
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithFragments(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
ids []string,
|
||||
) (*queryWithFragmentsResponse, error) {
|
||||
__input := __queryWithFragmentsInput{
|
||||
Ids: ids,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithFragmentsResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithFragments",
|
||||
`
|
||||
) (*queryWithFragmentsResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithFragments",
|
||||
Query: `
|
||||
query queryWithFragments ($ids: [ID!]!) {
|
||||
beings(ids: $ids) {
|
||||
__typename
|
||||
@@ -3246,27 +3288,32 @@ query queryWithFragments ($ids: [ID!]!) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithFragmentsInput{
|
||||
Ids: ids,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithFragmentsResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithInterfaceListField(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
ids []string,
|
||||
) (*queryWithInterfaceListFieldResponse, error) {
|
||||
__input := __queryWithInterfaceListFieldInput{
|
||||
Ids: ids,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithInterfaceListFieldResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithInterfaceListField",
|
||||
`
|
||||
) (*queryWithInterfaceListFieldResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithInterfaceListField",
|
||||
Query: `
|
||||
query queryWithInterfaceListField ($ids: [ID!]!) {
|
||||
beings(ids: $ids) {
|
||||
__typename
|
||||
@@ -3275,27 +3322,32 @@ query queryWithInterfaceListField ($ids: [ID!]!) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithInterfaceListFieldInput{
|
||||
Ids: ids,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithInterfaceListFieldResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithInterfaceListPointerField(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
ids []string,
|
||||
) (*queryWithInterfaceListPointerFieldResponse, error) {
|
||||
__input := __queryWithInterfaceListPointerFieldInput{
|
||||
Ids: ids,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithInterfaceListPointerFieldResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithInterfaceListPointerField",
|
||||
`
|
||||
) (*queryWithInterfaceListPointerFieldResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithInterfaceListPointerField",
|
||||
Query: `
|
||||
query queryWithInterfaceListPointerField ($ids: [ID!]!) {
|
||||
beings(ids: $ids) {
|
||||
__typename
|
||||
@@ -3304,27 +3356,32 @@ query queryWithInterfaceListPointerField ($ids: [ID!]!) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithInterfaceListPointerFieldInput{
|
||||
Ids: ids,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithInterfaceListPointerFieldResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithInterfaceNoFragments(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
id string,
|
||||
) (*queryWithInterfaceNoFragmentsResponse, error) {
|
||||
__input := __queryWithInterfaceNoFragmentsInput{
|
||||
Id: id,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithInterfaceNoFragmentsResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithInterfaceNoFragments",
|
||||
`
|
||||
) (*queryWithInterfaceNoFragmentsResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithInterfaceNoFragments",
|
||||
Query: `
|
||||
query queryWithInterfaceNoFragments ($id: ID!) {
|
||||
being(id: $id) {
|
||||
__typename
|
||||
@@ -3337,27 +3394,32 @@ query queryWithInterfaceNoFragments ($id: ID!) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithInterfaceNoFragmentsInput{
|
||||
Id: id,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithInterfaceNoFragmentsResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithNamedFragments(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
ids []string,
|
||||
) (*queryWithNamedFragmentsResponse, error) {
|
||||
__input := __queryWithNamedFragmentsInput{
|
||||
Ids: ids,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithNamedFragmentsResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithNamedFragments",
|
||||
`
|
||||
) (*queryWithNamedFragmentsResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithNamedFragments",
|
||||
Query: `
|
||||
query queryWithNamedFragments ($ids: [ID!]!) {
|
||||
beings(ids: $ids) {
|
||||
__typename
|
||||
@@ -3394,27 +3456,32 @@ fragment MoreUserFields on User {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithNamedFragmentsInput{
|
||||
Ids: ids,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithNamedFragmentsResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithOmitempty(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
id string,
|
||||
) (*queryWithOmitemptyResponse, error) {
|
||||
__input := __queryWithOmitemptyInput{
|
||||
Id: id,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithOmitemptyResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithOmitempty",
|
||||
`
|
||||
) (*queryWithOmitemptyResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithOmitempty",
|
||||
Query: `
|
||||
query queryWithOmitempty ($id: ID) {
|
||||
user(id: $id) {
|
||||
id
|
||||
@@ -3423,27 +3490,32 @@ query queryWithOmitempty ($id: ID) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithOmitemptyInput{
|
||||
Id: id,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithOmitemptyResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func queryWithVariables(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
id string,
|
||||
) (*queryWithVariablesResponse, error) {
|
||||
__input := __queryWithVariablesInput{
|
||||
Id: id,
|
||||
}
|
||||
var err error
|
||||
|
||||
var retval queryWithVariablesResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"queryWithVariables",
|
||||
`
|
||||
) (*queryWithVariablesResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "queryWithVariables",
|
||||
Query: `
|
||||
query queryWithVariables ($id: ID!) {
|
||||
user(id: $id) {
|
||||
id
|
||||
@@ -3452,23 +3524,31 @@ query queryWithVariables ($id: ID!) {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
&__input,
|
||||
Variables: &__queryWithVariablesInput{
|
||||
Id: id,
|
||||
},
|
||||
}
|
||||
var err error
|
||||
|
||||
var data queryWithVariablesResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func simpleQuery(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
) (*simpleQueryResponse, error) {
|
||||
var err error
|
||||
|
||||
var retval simpleQueryResponse
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
"simpleQuery",
|
||||
`
|
||||
) (*simpleQueryResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "simpleQuery",
|
||||
Query: `
|
||||
query simpleQuery {
|
||||
me {
|
||||
id
|
||||
@@ -3477,8 +3557,47 @@ query simpleQuery {
|
||||
}
|
||||
}
|
||||
`,
|
||||
&retval,
|
||||
nil,
|
||||
}
|
||||
var err error
|
||||
|
||||
var data simpleQueryResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
return &retval, err
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
func simpleQueryExt(
|
||||
ctx context.Context,
|
||||
client graphql.Client,
|
||||
) (*simpleQueryExtResponse, map[string]interface{}, error) {
|
||||
req := &graphql.Request{
|
||||
OpName: "simpleQueryExt",
|
||||
Query: `
|
||||
query simpleQueryExt {
|
||||
me {
|
||||
id
|
||||
name
|
||||
luckyNumber
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
||||
var err error
|
||||
|
||||
var data simpleQueryExtResponse
|
||||
resp := &graphql.Response{Data: &data}
|
||||
|
||||
err = client.MakeRequest(
|
||||
ctx,
|
||||
req,
|
||||
resp,
|
||||
)
|
||||
|
||||
return &data, resp.Extensions, err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ schema: schema.graphql
|
||||
operations: "*_test.go"
|
||||
generated: generated.go
|
||||
allow_broken_features: true
|
||||
use_extensions: true
|
||||
bindings:
|
||||
Date:
|
||||
type: time.Time
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestSimpleQuery(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := simpleQuery(ctx, client)
|
||||
resp, _, err := simpleQuery(ctx, client)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "1", resp.Me.Id)
|
||||
@@ -44,7 +44,7 @@ func TestServerError(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := failingQuery(ctx, client)
|
||||
resp, _, err := failingQuery(ctx, client)
|
||||
// As long as we get some response back, we should still return a full
|
||||
// response -- and indeed in this case it should even have another field
|
||||
// (which didn't err) set.
|
||||
@@ -57,7 +57,7 @@ func TestNetworkError(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
client := newRoundtripClient(t, "https://nothing.invalid/graphql")
|
||||
|
||||
resp, err := failingQuery(ctx, client)
|
||||
resp, _, err := failingQuery(ctx, client)
|
||||
// As we guarantee in the README, even on network error you always get a
|
||||
// non-nil response; this is so you can write e.g.
|
||||
// resp, err := failingQuery(ctx)
|
||||
@@ -82,19 +82,34 @@ func TestVariables(t *testing.T) {
|
||||
// worry about it.
|
||||
client := graphql.NewClient(server.URL, http.DefaultClient)
|
||||
|
||||
resp, err := queryWithVariables(ctx, client, "2")
|
||||
resp, _, err := queryWithVariables(ctx, client, "2")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "2", resp.User.Id)
|
||||
assert.Equal(t, "Raven", resp.User.Name)
|
||||
assert.Equal(t, -1, resp.User.LuckyNumber)
|
||||
|
||||
resp, err = queryWithVariables(ctx, client, "374892379482379")
|
||||
resp, _, err = queryWithVariables(ctx, client, "374892379482379")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Zero(t, resp.User)
|
||||
}
|
||||
|
||||
func TestExtensions(t *testing.T) {
|
||||
_ = `# @genqlient
|
||||
query simpleQueryExt { me { id name luckyNumber } }`
|
||||
|
||||
ctx := context.Background()
|
||||
server := server.RunServer()
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
_, extensions, err := simpleQueryExt(ctx, client)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, extensions)
|
||||
assert.Equal(t, extensions["foobar"], "test")
|
||||
}
|
||||
|
||||
func TestOmitempty(t *testing.T) {
|
||||
_ = `# @genqlient(omitempty: true)
|
||||
query queryWithOmitempty($id: ID) {
|
||||
@@ -106,7 +121,7 @@ func TestOmitempty(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithOmitempty(ctx, client, "2")
|
||||
resp, _, err := queryWithOmitempty(ctx, client, "2")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "2", resp.User.Id)
|
||||
@@ -114,7 +129,7 @@ func TestOmitempty(t *testing.T) {
|
||||
assert.Equal(t, -1, resp.User.LuckyNumber)
|
||||
|
||||
// should return default user, not the user with ID ""
|
||||
resp, err = queryWithOmitempty(ctx, client, "")
|
||||
resp, _, err = queryWithOmitempty(ctx, client, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "1", resp.User.Id)
|
||||
@@ -133,7 +148,7 @@ func TestCustomMarshal(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithCustomMarshal(ctx, client,
|
||||
resp, _, err := queryWithCustomMarshal(ctx, client,
|
||||
time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC))
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -145,7 +160,7 @@ func TestCustomMarshal(t *testing.T) {
|
||||
time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC),
|
||||
user.Birthdate)
|
||||
|
||||
resp, err = queryWithCustomMarshal(ctx, client,
|
||||
resp, _, err = queryWithCustomMarshal(ctx, client,
|
||||
time.Date(2021, time.January, 1, 12, 34, 56, 789, time.UTC))
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, resp.UsersBornOn, 0)
|
||||
@@ -162,7 +177,7 @@ func TestCustomMarshalSlice(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithCustomMarshalSlice(ctx, client,
|
||||
resp, _, err := queryWithCustomMarshalSlice(ctx, client,
|
||||
[]time.Time{time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC)})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -174,7 +189,7 @@ func TestCustomMarshalSlice(t *testing.T) {
|
||||
time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC),
|
||||
user.Birthdate)
|
||||
|
||||
resp, err = queryWithCustomMarshalSlice(ctx, client,
|
||||
resp, _, err = queryWithCustomMarshalSlice(ctx, client,
|
||||
[]time.Time{time.Date(2021, time.January, 1, 12, 34, 56, 789, time.UTC)})
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, resp.UsersBornOnDates, 0)
|
||||
@@ -197,7 +212,7 @@ func TestCustomMarshalOptional(t *testing.T) {
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
date := time.Date(2025, time.January, 1, 12, 34, 56, 789, time.UTC)
|
||||
resp, err := queryWithCustomMarshalOptional(ctx, client, &date, nil)
|
||||
resp, _, err := queryWithCustomMarshalOptional(ctx, client, &date, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Len(t, resp.UserSearch, 1)
|
||||
@@ -209,7 +224,7 @@ func TestCustomMarshalOptional(t *testing.T) {
|
||||
user.Birthdate)
|
||||
|
||||
id := "2"
|
||||
resp, err = queryWithCustomMarshalOptional(ctx, client, nil, &id)
|
||||
resp, _, err = queryWithCustomMarshalOptional(ctx, client, nil, &id)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, resp.UserSearch, 1)
|
||||
user = resp.UserSearch[0]
|
||||
@@ -230,7 +245,7 @@ func TestInterfaceNoFragments(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithInterfaceNoFragments(ctx, client, "1")
|
||||
resp, _, err := queryWithInterfaceNoFragments(ctx, client, "1")
|
||||
require.NoError(t, err)
|
||||
|
||||
// We should get the following response:
|
||||
@@ -250,7 +265,7 @@ func TestInterfaceNoFragments(t *testing.T) {
|
||||
assert.Equal(t, "1", user.Id)
|
||||
assert.Equal(t, "Yours Truly", user.Name)
|
||||
|
||||
resp, err = queryWithInterfaceNoFragments(ctx, client, "3")
|
||||
resp, _, err = queryWithInterfaceNoFragments(ctx, client, "3")
|
||||
require.NoError(t, err)
|
||||
|
||||
// We should get the following response:
|
||||
@@ -269,7 +284,7 @@ func TestInterfaceNoFragments(t *testing.T) {
|
||||
assert.Equal(t, "3", animal.Id)
|
||||
assert.Equal(t, "Fido", animal.Name)
|
||||
|
||||
resp, err = queryWithInterfaceNoFragments(ctx, client, "4757233945723")
|
||||
resp, _, err = queryWithInterfaceNoFragments(ctx, client, "4757233945723")
|
||||
require.NoError(t, err)
|
||||
|
||||
// We should get the following response:
|
||||
@@ -293,7 +308,7 @@ func TestInterfaceListField(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithInterfaceListField(ctx, client,
|
||||
resp, _, err := queryWithInterfaceListField(ctx, client,
|
||||
[]string{"1", "3", "12847394823"})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -340,7 +355,7 @@ func TestInterfaceListPointerField(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithInterfaceListPointerField(ctx, client,
|
||||
resp, _, err := queryWithInterfaceListPointerField(ctx, client,
|
||||
[]string{"1", "3", "12847394823"})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -394,7 +409,7 @@ func TestFragments(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithFragments(ctx, client, []string{"1", "3", "12847394823"})
|
||||
resp, _, err := queryWithFragments(ctx, client, []string{"1", "3", "12847394823"})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, resp.Beings, 3)
|
||||
@@ -487,7 +502,7 @@ func TestNamedFragments(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithNamedFragments(ctx, client, []string{"1", "3", "12847394823"})
|
||||
resp, _, err := queryWithNamedFragments(ctx, client, []string{"1", "3", "12847394823"})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, resp.Beings, 3)
|
||||
@@ -616,7 +631,7 @@ func TestFlatten(t *testing.T) {
|
||||
defer server.Close()
|
||||
client := newRoundtripClient(t, server.URL)
|
||||
|
||||
resp, err := queryWithFlatten(ctx, client, []string{"1", "3", "12847394823"})
|
||||
resp, _, err := queryWithFlatten(ctx, client, []string{"1", "3", "12847394823"})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, resp.Beings, 3)
|
||||
|
||||
@@ -88,16 +88,16 @@ func (c *roundtripClient) roundtripResponse(resp interface{}) {
|
||||
assert.Equal(c.t, string(body), string(bodyAgain))
|
||||
}
|
||||
|
||||
func (c *roundtripClient) MakeRequest(ctx context.Context, opName, query string, retval, variables interface{}) error {
|
||||
func (c *roundtripClient) MakeRequest(ctx context.Context, req *graphql.Request, resp *graphql.Response) error {
|
||||
// TODO(benkraft): Also check the variables round-trip. This is a bit less
|
||||
// important since most of the code is the same (and input types are
|
||||
// strictly simpler), and a bit hard to do because when asserting about
|
||||
// structs we need to worry about things like equality of time.Time values.
|
||||
err := c.wrapped.MakeRequest(ctx, opName, query, retval, variables)
|
||||
err := c.wrapped.MakeRequest(ctx, req, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.roundtripResponse(retval)
|
||||
c.roundtripResponse(resp.Data)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
"github.com/99designs/gqlgen/graphql/handler"
|
||||
"github.com/99designs/gqlgen/graphql/handler/transport"
|
||||
)
|
||||
@@ -131,6 +132,10 @@ func (r *queryResolver) Fail(ctx context.Context) (*bool, error) {
|
||||
func RunServer() *httptest.Server {
|
||||
gqlgenServer := handler.New(NewExecutableSchema(Config{Resolvers: &resolver{}}))
|
||||
gqlgenServer.AddTransport(transport.POST{})
|
||||
gqlgenServer.AroundResponses(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
|
||||
graphql.RegisterExtension(ctx, "foobar", "test")
|
||||
return next(ctx)
|
||||
})
|
||||
return httptest.NewServer(gqlgenServer)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user