-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
4ac3d8e
commit 01ff0c7
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters