httpexpect
httpexpect copied to clipboard
GraphQL?
Is there a comfortable way to test GraphQL endpoints?
Hi, not at all. Only regular http requests and assertions for http header and json body are available currently, plus websocket support.
Do you think there might be in the future?
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 May i pick this up ? I'll start with submitting a short design proposal on the request builder.
@gtourkas Sure, you're welcome!
@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.
@gavv Did you have the chance to look into my suggestion above ?