Skip to content

Commit 592be89

Browse files
committed
Support for specifying an API version for the GraphQL command
1 parent cadd0a6 commit 592be89

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

cmd/gql/gql.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func findQuery(c *cli.Context) (string, error) {
3838

3939
func queryAction(c *cli.Context) error {
4040
shop := c.String("shop")
41-
client := gql.NewClient(shop, cmd.LookupAccessToken(shop, c.String("access-token")))
41+
client := gql.NewClient(shop, cmd.LookupAccessToken(shop, c.String("access-token")), c.String("api-version"))
4242

4343
query, err := findQuery(c)
4444
if err != nil {
@@ -59,13 +59,21 @@ func queryAction(c *cli.Context) error {
5959
}
6060

6161
func init() {
62+
flags := []cli.Flag{
63+
&cli.StringFlag{
64+
Name: "api-version",
65+
Aliases: []string{"a"},
66+
Usage: "API version to use; default is a versionless call",
67+
},
68+
}
69+
6270
Cmd = cli.Command{
6371
Name: "graphql",
6472
Aliases: []string{"gql"},
6573
ArgsUsage: "[query-file.graphql]",
6674
Usage: "Run a GraphQL query against the Admin API",
6775
Description: "If query-file.graphql is not given query is read from stdin",
68-
Flags: cmd.Flags,
76+
Flags: append(cmd.Flags, flags...),
6977
Action: queryAction,
7078
}
7179
}

gql/client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ type Client struct {
1717
token string
1818
}
1919

20-
const endpoint = "https://%s.myshopify.com/admin/api/graphql.json"
20+
// We omit the "/" after API for the case where there's no version.
21+
const endpoint = "https://%s.myshopify.com/admin/api%s/graphql.json"
22+
23+
func NewClient(shop, token, version string) *Client {
24+
if len(version) > 0 {
25+
version = "/" + version
26+
}
2127

22-
func NewClient(shop, token string) *Client {
2328
// allow for NAME.myshopify.com or just NAME
2429
shop = strings.SplitN(shop, ".", 2)[0]
2530

26-
return &Client{endpoint: fmt.Sprintf(endpoint, shop), token: token}
31+
return &Client{endpoint: fmt.Sprintf(endpoint, shop, version), token: token}
2732
}
2833

2934
func (c *Client) Query(q string) (mxj.Map, error) {

gql/storefront/storefront.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const enableMutation = `
5454
`
5555

5656
func New(shop, token string) *Storefront {
57-
client := gql.NewClient(shop, token)
57+
client := gql.NewClient(shop, token, "")
5858
return &Storefront{client}
5959
}
6060

0 commit comments

Comments
 (0)