httpexpect icon indicating copy to clipboard operation
httpexpect copied to clipboard

GraphQL?

Open frederikhors opened this issue 3 years ago • 7 comments

Is there a comfortable way to test GraphQL endpoints?

frederikhors avatar Mar 06 '21 09:03 frederikhors

Hi, not at all. Only regular http requests and assertions for http header and json body are available currently, plus websocket support.

gavv avatar Mar 06 '21 10:03 gavv

Do you think there might be in the future?

frederikhors avatar Mar 06 '21 11:03 frederikhors

Personally I don't have any plans on that. However probably this would be an eligible feature. I guess we could implement an alternative request builder for graphql and reuse existing payload matches.

gavv avatar Mar 07 '21 12:03 gavv

@gavv May i pick this up ? I'll start with submitting a short design proposal on the request builder.

gtourkas avatar Apr 08 '21 04:04 gtourkas

@gtourkas Sure, you're welcome!

gavv avatar May 10 '21 12:05 gavv

@gavv I've looked into this a bit.

We could use https://github.com/shurcooL/graphql, that will provide with the means of typed graphql queries and mutations in the form of go structs. An example of a query with constant arguments:

var q struct {
	Human struct {
		Name   graphql.String
		Height graphql.Float `graphql:"height(unit: METER)"`
	} `graphql:"human(id: \"1000\")"`
}

In addition, we could provide with a WithGraphQLQuery, a WithGraphQLMutation and a chained WithVariables, should there be variable arguments. An example of a query with variable arguments:

var q struct {
	Human struct {
		Name   graphql.String
		Height graphql.Float `graphql:"height(unit: $unit)"`
	} `graphql:"human(id: $id)"`
}

and the respective values:

variables := map[string]interface{}{
	"id":   graphql.ID(id),
	"unit": starwars.LengthUnit("METER"),
}

I don't see any need for additions in the assessment part, as the GraphQL queries return JSON. Overall it looks like a low-hanging fruit and I can have something ready in my fork in a few days. Please let me know if the above make sense to you.

UPDATE: The shurcool lib has been forked by Hasura (https://github.com/hasura/go-graphql-client), which added support for named operations. Looks like a better candidate imo.

gtourkas avatar Aug 15 '21 04:08 gtourkas

@gavv Did you have the chance to look into my suggestion above ?

gtourkas avatar Aug 31 '21 10:08 gtourkas