Releases: hasura/go-graphql-client
v0.5.1
v0.5.0
- allow building queries and mutations from ordered maps ([][2]string) (#20)
You might need to make multiple mutations in single query. It's not very convenient with structs, so you can use ordered map [][2]interface{}
instead.
For example, to make the following GraphQL mutation:
mutation($login1: String!, $login2: String!, $login3: String!) {
createUser(login: $login1) { login }
createUser(login: $login2) { login }
createUser(login: $login3) { login }
}
variables {
"login1": "grihabor",
"login2": "diman",
"login3": "indigo"
}
You can define:
type CreateUser struct {
Login graphql.String
}
m := [][2]interface{}{
{"createUser(login: $login1)", &CreateUser{}},
{"createUser(login: $login2)", &CreateUser{}},
{"createUser(login: $login3)", &CreateUser{}},
}
variables := map[string]interface{}{
"login1": graphql.String("grihabor"),
"login2": graphql.String("diman"),
"login3": graphql.String("indigo"),
}
v0.4.0
There are extensible parts in the GraphQL query. We shouldn't required them in the method. To make it flexible, we can abstract these options as optional arguments that follow this interface.
type Option interface {
Type() OptionType
String() string
}
client.Query(ctx context.Context, q interface{}, variables map[string]interface{}, options ...Option) error
Currently we support 2 option types: operation_name
and operation_directive
. The operation name option is built-in because it is unique. We can use the option directly with OperationName
// query MyQuery {
// ...
// }
client.Query(ctx, &q, variables, graphql.OperationName("MyQuery"))
In contrast, operation directive is various and customizable on different GraphQL servers. There isn't any built-in directive in the library. You need to define yourself. For example:
// define @cached directive for Hasura queries
// https://hasura.io/docs/latest/graphql/cloud/response-caching.html#enable-caching
type cachedDirective struct {
ttl int
}
func (cd cachedDirective) Type() OptionType {
// operation_directive
return graphql.OptionTypeOperationDirective
}
func (cd cachedDirective) String() string {
if cd.ttl <= 0 {
return "@cached"
}
return fmt.Sprintf("@cached(ttl: %d)", cd.ttl)
}
// query MyQuery @cached {
// ...
// }
client.Query(ctx, &q, variables, graphql.OperationName("MyQuery"), cachedDirective{})
v0.3.0
This release includes bug fixes, improvements and documentation updates
- introduce
SubscribeRaw
and wrap all subscriptions map access with lock to avoid race condition (#4) (thank @digitalcrab) - allow clients to receive all GraphQL errors from server (#8) (thank @totalys)
- support raw JSON field with
json.RawMessage
type (#14) (thank @kwapik) - expose
WebsocketHandler
and custom WebSocket client documentation (#15) - misc: automation test with Github Action
v0.2.0
In the case we developers want to decode JSON response ourself. Moreover, the default UnmarshalGraphQL
function isn't ideal with complicated nested interfaces
func (c *Client) QueryRaw(ctx context.Context, q interface{}, variables map[string]interface{}) (*json.RawMessage, error)
func (c *Client) MutateRaw(ctx context.Context, q interface{}, variables map[string]interface{}) (*json.RawMessage, error)
func (c *Client) NamedQueryRaw(ctx context.Context, name string, q interface{}, variables map[string]interface{}) (*json.RawMessage, error)
func (c *Client) NamedMutateRaw(ctx context.Context, name string, q interface{}, variables map[string]interface{}) (*json.RawMessage, error)