hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
396 stars 91 forks source link

introduce ExecRaw and change the output type of Raw methods #44

Closed hgiasac closed 2 years ago

hgiasac commented 2 years ago

This PR adds ExecRaw method that returns a raw json message.

query := `query{something(where: { foo: { _eq: "bar" }}){id}}`
var res struct {
    Somethings []Something `json:"something"`
}

raw, err := client.ExecRaw(ctx, query, map[string]any{}) 
if err != nil {
    panic(err)
}

err = json.Unmarshal(raw, &res)

Breaking change: currently QueryRaw, MutateRaw and Subscribe methods return *json.RawMessage. This output type is redundant to be decoded. The output type should be changed to []byte.

var subscription struct {
    Me struct {
        Name graphql.String
    }
}

subscriptionId, err := client.Subscribe(&query, nil, func(dataValue []byte, errValue error) error {
    if errValue != nil {
        // handle error
        // if returns error, it will failback to `onError` event
        return nil
    }
    data := query{}
    err := json.Unmarshal(dataValue, &data)

    fmt.Println(query.Me.Name)

    // Output: Luke Skywalker
    return nil
})

if err != nil {
    // Handle error.
}

This also removes canonical/vanity github.com/shurcooL/graphql import path