-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
56 lines (48 loc) · 2.04 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package mondoogql
import (
"context"
"github.com/shurcooL/graphql"
"go.mondoo.com/mondoo-go/internal"
"go.mondoo.com/mondoo-go/internal/http"
"go.mondoo.com/mondoo-go/option"
)
// Client is a Mondoo GraphQL API client.
type Client struct {
client *graphql.Client
}
// NewClient creates a new Mondoo GraphQL API client
func NewClient(opts ...option.ClientOption) (*Client, error) {
// NOTE: we need to prepend options, so we don't override user-specified options
opts = append([]option.ClientOption{option.WithDefaultEndpoint(), option.WithUserAgent("mondoo-graphql-client/" + internal.Version)}, opts...)
httpClient, endpoint, err := http.NewHttpClient(opts...)
if err != nil {
return nil, err
}
c := &Client{}
c.client = graphql.NewClient(endpoint, httpClient)
return c, nil
}
// Query executes a single GraphQL query request, populating the response into q. It supports
// two arguments for the query and variables.
// - q should be a pointer to struct that corresponds to the Mondoo GraphQL schema.
// - variables should be a map of string to arbitrary Go type that corresponds to the GraphQL variables.
func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
return c.client.Query(ctx, q, variables)
}
// Mutate executes a single GraphQL mutation request, populating the response into m. It supports
// three arguments for the mutation, input and variables.
// - m should be a pointer to struct that corresponds to the Mondoo GraphQL schema.
// - input should be a map of string to arbitrary Go type that corresponds to the GraphQL input.
// - variables should be a map of string to arbitrary Go type that corresponds to the GraphQL variables.
func (c *Client) Mutate(ctx context.Context, m interface{}, input Input, variables map[string]interface{}) error {
if input != nil {
if variables == nil {
variables = map[string]interface{}{"input": input}
} else {
variables["input"] = input
}
}
return c.client.Mutate(ctx, m, variables)
}