Skip to content

Commit

Permalink
Implement a GraphQL interface
Browse files Browse the repository at this point in the history
For now this simply wraps the machinebox/graphql library but it should
allow us to replace that module whenever we need to.
  • Loading branch information
mvantellingen committed Jul 7, 2020
1 parent 4ac3d8e commit 01ff0c7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
16 changes: 15 additions & 1 deletion commercetools/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ func (qi QueryInput) toParams() (values url.Values) {

// New creates a new client based on the provided Config.
func New(cfg *Config) *Client {
apiURL, err := cleanURL(getConfigValue(cfg.URL, "CTP_API_URL"))
if err != nil {
return nil
}

client := &Client{
projectKey: getConfigValue(cfg.ProjectKey, "CTP_PROJECT_KEY"),
url: getConfigValue(cfg.URL, "CTP_API_URL"),
url: apiURL,
httpClient: cfg.HTTPClient,
userAgent: GetUserAgent(cfg),
}
Expand Down Expand Up @@ -221,6 +226,15 @@ func (c *Client) doRequest(method string, endpoint string, params url.Values, da
}
}

func cleanURL(baseURL string) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return baseURL, err
}
u.Path = ""
return u.String(), nil
}

func serializeInput(input interface{}) (io.Reader, error) {
m, err := json.MarshalIndent(input, "", "\t")
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions commercetools/client_graphql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package commercetools

import (
"context"
"fmt"

"github.com/machinebox/graphql"
)

type GraphQLQuery struct {
client *graphql.Client
request *graphql.Request
}

// NewGraphQLQuery creates a new GraphQLQuery object which can be used to
// execute a GraphQL query
func (client *Client) NewGraphQLQuery(query string) *GraphQLQuery {
endpoint := fmt.Sprintf("%s/%s/graphql", client.url, client.projectKey)

gqlClient := graphql.NewClient(
endpoint,
graphql.WithHTTPClient(client.httpClient),
)

queryObject := GraphQLQuery{
client: gqlClient,
request: graphql.NewRequest(query),
}

return &queryObject
}

func (client *Client) NewGraphQLQueryMerchantCenter(query string) *GraphQLQuery {
endpoint := fmt.Sprintf("%s/%s/graphql", client.url, client.projectKey)

gqlClient := graphql.NewClient(
endpoint,
graphql.WithHTTPClient(client.httpClient),
)

queryObject := GraphQLQuery{
client: gqlClient,
request: graphql.NewRequest(query),
}

return &queryObject
}

// Bind variables to the GraphQL query
func (gql *GraphQLQuery) Bind(key string, value interface{}) {
gql.request.Var(key, value)
}

// Execute the GraphQL query
func (gql *GraphQLQuery) Execute(respData interface{}) error {
return gql.client.Run(context.TODO(), gql.request, &respData)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/dave/jennifer v1.4.0
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/machinebox/graphql v0.2.2
github.com/mitchellh/mapstructure v1.3.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkBZfo=
github.com/machinebox/graphql v0.2.2/go.mod h1:F+kbVMHuwrQ5tYgU9JXlnskM8nOaFxCAEolaQybkjWA=
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down

0 comments on commit 01ff0c7

Please sign in to comment.