forked from r0busta/go-shopify-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
83 lines (66 loc) · 2.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package shopify
import (
"os"
"github.com/r0busta/graphql"
log "github.com/sirupsen/logrus"
graphqlclient "github.com/navana-tech/go-shopify-graphql/v6/graphql"
)
const (
defaultShopifyAPIVersion = "2022-04"
)
type Client struct {
gql graphql.GraphQL
Product ProductService
Variant VariantService
Inventory InventoryService
Collection CollectionService
Order OrderService
Fulfillment FulfillmentService
Location LocationService
Metafield MetafieldService
BulkOperation BulkOperationService
}
type Option func(shopClient *Client)
func WithGraphQLClient(gql graphql.GraphQL) Option {
return func(c *Client) {
c.gql = gql
}
}
func NewDefaultClient(opts ...Option) *Client {
apiKey := os.Getenv("STORE_API_KEY")
password := os.Getenv("STORE_PASSWORD")
storeName := os.Getenv("STORE_NAME")
if apiKey == "" || password == "" || storeName == "" {
log.Fatalln("Shopify app API Key and/or Password and/or Store Name not set")
}
return NewClient(apiKey, password, storeName, opts...)
}
func NewClient(apiKey string, password string, storeName string, opts ...Option) *Client {
c := &Client{}
for _, opt := range opts {
opt(c)
}
if c.gql == nil {
c.gql = newShopifyGraphQLClient(apiKey, password, storeName)
}
c.Product = &ProductServiceOp{client: c}
c.Variant = &VariantServiceOp{client: c}
c.Inventory = &InventoryServiceOp{client: c}
c.Collection = &CollectionServiceOp{client: c}
c.Order = &OrderServiceOp{client: c}
c.Fulfillment = &FulfillmentServiceOp{client: c}
c.Location = &LocationServiceOp{client: c}
c.Metafield = &MetafieldServiceOp{client: c}
c.BulkOperation = &BulkOperationServiceOp{client: c}
return c
}
func newShopifyGraphQLClient(apiKey string, password string, storeName string) *graphql.Client {
opts := []graphqlclient.Option{
graphqlclient.WithVersion(defaultShopifyAPIVersion),
graphqlclient.WithPrivateAppAuth(apiKey, password),
}
return graphqlclient.NewClient(storeName, opts...)
}
func (c *Client) GraphQLClient() graphql.GraphQL {
return c.gql
}