From 65ac0d672475763f45011463a6cbce70e36529c0 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 8 Aug 2023 16:30:13 +0200 Subject: [PATCH 1/6] Updated repo. Added additional options to override http client, changed setting of options --- apollostudio/client.go | 116 ++++++++++++++++++ {pkg/apollostudio => apollostudio}/errors.go | 4 +- {pkg/apollostudio => apollostudio}/get.go | 4 +- .../querytypes.go | 0 {pkg/apollostudio => apollostudio}/remove.go | 6 +- {pkg/apollostudio => apollostudio}/submit.go | 10 +- .../apollostudio => apollostudio}/validate.go | 8 +- cmd/debug/main.go | 105 ---------------- go.mod | 10 +- go.sum | 48 ++++++-- pkg/apollostudio/client.go | 53 -------- pkg/apollostudio/transport.go | 12 -- 12 files changed, 174 insertions(+), 202 deletions(-) create mode 100644 apollostudio/client.go rename {pkg/apollostudio => apollostudio}/errors.go (92%) rename {pkg/apollostudio => apollostudio}/get.go (96%) rename {pkg/apollostudio => apollostudio}/querytypes.go (100%) rename {pkg/apollostudio => apollostudio}/remove.go (88%) rename {pkg/apollostudio => apollostudio}/submit.go (87%) rename {pkg/apollostudio => apollostudio}/validate.go (97%) delete mode 100644 cmd/debug/main.go delete mode 100644 pkg/apollostudio/client.go delete mode 100644 pkg/apollostudio/transport.go diff --git a/apollostudio/client.go b/apollostudio/client.go new file mode 100644 index 0000000..2897394 --- /dev/null +++ b/apollostudio/client.go @@ -0,0 +1,116 @@ +package apollostudio + +import ( + "errors" + "net/http" + "strings" + + "github.com/hasura/go-graphql-client" +) + +var defaultUrl = "https://graphql.api.apollographql.com/api/graphql" + +type clientSettings struct { + httpClient *http.Client + debug bool + url string + requestModifier graphql.RequestModifier +} + +type Client struct { + gqlClient *graphql.Client + GraphRef GraphRef +} + +type ClientOpt func(client *clientSettings) + +// WithHttpClient allows you to set a custom http client for fine-grained control of traffic +func WithHttpClient(httpClient *http.Client) ClientOpt { + return func(settings *clientSettings) { + settings.httpClient = httpClient + } +} + +// WithDebug allows you to run the client in debug mode, which will return internal error details +func WithDebug(debug bool) ClientOpt { + return func(settings *clientSettings) { + settings.debug = debug + } +} + +// WithUrl allows you to overrule the default url +func WithUrl(url string) ClientOpt { + return func(settings *clientSettings) { + settings.url = url + } +} + +// WithRequestModifier allows you to modify the request. Note that the API key is also set by ApiKeyRequestModifier, +// so make sure to also call this method if you want to further modify the request +func WithRequestModifier(modifier graphql.RequestModifier) ClientOpt { + return func(settings *clientSettings) { + settings.requestModifier = modifier + } +} + +type GraphRef string + +func ValidateGraphRef(graphRef string) error { + if graphRef == "" { + return errors.New("graph ref is required") + } + + p := strings.Split(graphRef, "@") + + if len(p) < 2 { + return errors.New("missing variant in graph ref") + } + + return nil +} + +func (g *GraphRef) getGraphId() string { + p := strings.Split(string(*g), "@") + + return p[0] +} + +func (g *GraphRef) getVariant() string { + + p := strings.Split(string(*g), "@") + + return p[1] +} + +func NewClient(apiKey string, graphRef string, opts ...ClientOpt) (*Client, error) { + settings := &clientSettings{ + httpClient: http.DefaultClient, + debug: false, + url: defaultUrl, + requestModifier: ApiKeyRequestModifier(apiKey), + } + + for _, opt := range opts { + opt(settings) + } + + err := ValidateGraphRef(graphRef) + if err != nil { + return nil, err + } + + gqlClient := graphql.NewClient(settings.url, settings.httpClient) + gqlClient = gqlClient.WithDebug(settings.debug) + gqlClient = gqlClient.WithRequestModifier(settings.requestModifier) + + return &Client{ + gqlClient: gqlClient, + GraphRef: GraphRef(graphRef), + }, nil +} + +func ApiKeyRequestModifier(apiKey string) graphql.RequestModifier { + return func(req *http.Request) { + req.Header.Add("x-api-key", apiKey) + } +} diff --git a/pkg/apollostudio/errors.go b/apollostudio/errors.go similarity index 92% rename from pkg/apollostudio/errors.go rename to apollostudio/errors.go index 8b79706..f1464f8 100644 --- a/pkg/apollostudio/errors.go +++ b/apollostudio/errors.go @@ -1,6 +1,7 @@ package apollostudio import ( + "errors" "fmt" "strings" ) @@ -30,6 +31,7 @@ func (e *OperationError) Error() string { // an error, but the subgraph will still be created. As a result, the // federation schema remains unaffected, while the subgraph coexists with errors. func IsOperationError(err error) bool { - _, ok := err.(*OperationError) + var operationError *OperationError + ok := errors.As(err, &operationError) return ok } diff --git a/pkg/apollostudio/get.go b/apollostudio/get.go similarity index 96% rename from pkg/apollostudio/get.go rename to apollostudio/get.go index 5c77fc2..cd6b776 100644 --- a/pkg/apollostudio/get.go +++ b/apollostudio/get.go @@ -62,9 +62,9 @@ func (c *Client) GetSubGraph(ctx context.Context, name string) (*SubGraphResult, var query Query vars := map[string]interface{}{ - "graph_id": graphql.ID(c.GraphID), + "graph_id": graphql.ID(c.GraphRef.getGraphId()), "subgraph": graphql.ID(name), - "variant": graphql.String(c.Variant), + "variant": c.GraphRef.getVariant(), } err := c.gqlClient.Query(ctx, &query, vars) diff --git a/pkg/apollostudio/querytypes.go b/apollostudio/querytypes.go similarity index 100% rename from pkg/apollostudio/querytypes.go rename to apollostudio/querytypes.go diff --git a/pkg/apollostudio/remove.go b/apollostudio/remove.go similarity index 88% rename from pkg/apollostudio/remove.go rename to apollostudio/remove.go index 82930c3..c7711e2 100644 --- a/pkg/apollostudio/remove.go +++ b/apollostudio/remove.go @@ -19,9 +19,9 @@ func (c *Client) RemoveSubGraph(ctx context.Context, name string) error { var mutation Mutation vars := map[string]interface{}{ - "graph_id": graphql.ID(c.GraphID), - "subgraph": graphql.String(name), - "variant": graphql.String(c.Variant), + "graph_id": graphql.ID(c.GraphRef.getGraphId()), + "subgraph": name, + "variant": c.GraphRef.getVariant(), } err := c.gqlClient.Mutate(ctx, &mutation, vars) diff --git a/pkg/apollostudio/submit.go b/apollostudio/submit.go similarity index 87% rename from pkg/apollostudio/submit.go rename to apollostudio/submit.go index 9591999..795abd7 100644 --- a/pkg/apollostudio/submit.go +++ b/apollostudio/submit.go @@ -33,17 +33,17 @@ func (c *Client) SubmitSubGraph(ctx context.Context, opts *SubmitOptions) (*Subm var mutation Mutation vars := map[string]interface{}{ - "graph_id": graphql.ID(c.GraphID), - "subgraph": graphql.String(opts.SubGraphName), - "variant": graphql.String(c.Variant), - "revision": graphql.String(""), + "graph_id": graphql.ID(c.GraphRef.getGraphId()), + "subgraph": opts.SubGraphName, + "variant": c.GraphRef.getVariant(), + "revision": "", "schema": PartialSchemaInput{ Sdl: string(opts.SubGraphSchema), }, "git_context": GitContextInput{}, // URL is necessary if sub graph does not exist and is created // during the submission - "url": graphql.String(opts.SubGraphURL), + "url": opts.SubGraphURL, } err := c.gqlClient.Mutate(ctx, &mutation, vars) diff --git a/pkg/apollostudio/validate.go b/apollostudio/validate.go similarity index 97% rename from pkg/apollostudio/validate.go rename to apollostudio/validate.go index 95bb21a..a9e02d8 100644 --- a/pkg/apollostudio/validate.go +++ b/apollostudio/validate.go @@ -174,12 +174,12 @@ func (c *Client) submitSubgraphCheck(ctx context.Context, opts *ValidateOptions) var mutation Mutation vars := map[string]interface{}{ - "graph_id": graphql.ID(c.GraphID), - "variant": graphql.String(c.Variant), + "graph_id": graphql.ID(c.GraphRef.getGraphId()), + "variant": c.GraphRef.getVariant(), "input": SubgraphCheckAsyncInput{ Config: HistoricQueryParametersInput{}, GitContext: GitContextInput{}, - GraphRef: c.GraphRef, + GraphRef: string(c.GraphRef), IsSandbox: false, ProposedSchema: string(opts.SubGraphSchema), SubgraphName: opts.SubGraphName, @@ -218,7 +218,7 @@ func (c *Client) checkWorkflow(ctx context.Context, workflowId string) ( } vars := map[string]interface{}{ - "graph_id": graphql.ID(c.GraphID), + "graph_id": graphql.ID(c.GraphRef.getGraphId()), "workflowId": graphql.ID(workflowId), } diff --git a/cmd/debug/main.go b/cmd/debug/main.go deleted file mode 100644 index 2c30dfb..0000000 --- a/cmd/debug/main.go +++ /dev/null @@ -1,105 +0,0 @@ -package main - -import ( - "context" - "errors" - "fmt" - "log" - "os" - - "github.com/joho/godotenv" - "github.com/labd/apollostudio-go-sdk/pkg/apollostudio" -) - -func handleErr(err error) { - var opErr *apollostudio.OperationError - if errors.As(err, &opErr) { - fmt.Println(opErr.Error()) - os.Exit(1) - } - log.Fatal(err) -} - -func main() { - godotenv.Load() - - apiKey := os.Getenv("APOLLO_API_KEY") - graphRef := os.Getenv("APOLLO_GRAPH_REF") - subGraphSchema := os.Getenv("APOLLO_SUB_GRAPH_SCHEMA") - subGraphName := os.Getenv("APOLLO_SUB_GRAPH_NAME") - subGraphURL := os.Getenv("APOLLO_SUB_GRAPH_URL") - - ctx := context.Background() - client, err := apollostudio.NewClient( - apollostudio.ClientOpts{ - APIKey: apiKey, - GraphRef: graphRef, - }, - ) - - if err != nil { - handleErr(err) - } - - b, err := client.GetLatestSchemaBuild(ctx) - - if err != nil { - handleErr(err) - } - - for _, v := range b.Result.BuildFailure.ErrorMessages { - fmt.Println(v.Code, v.Message) - } - - vr, err := client.ValidateSubGraph( - ctx, &apollostudio.ValidateOptions{ - SubGraphSchema: []byte(subGraphSchema), - SubGraphName: subGraphName, - }, - ) - - if err != nil { - handleErr(err) - } - - if !vr.IsValid() { - fmt.Println("schema is not valid") - fmt.Println(vr.Errors()) - return - } - - fmt.Println("schema validated") - fmt.Println(vr.Changes()) - - sr, err := client.SubmitSubGraph( - ctx, &apollostudio.SubmitOptions{ - SubGraphName: subGraphName, - SubGraphSchema: []byte(subGraphSchema), - SubGraphURL: subGraphURL, - }, - ) - - if err != nil { - handleErr(err) - } - - fmt.Println("schema submitted") - fmt.Println(sr) - - rr, err := client.GetSubGraph(ctx, subGraphName) - - if err != nil { - handleErr(err) - } - - fmt.Println("schema read") - fmt.Println(rr.Revision) - - err = client.RemoveSubGraph(ctx, subGraphName) - - if err != nil { - handleErr(err) - } - - fmt.Println("schema deleted") -} diff --git a/go.mod b/go.mod index a1e0717..a7405c9 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,12 @@ module github.com/labd/apollostudio-go-sdk -go 1.19 +go 1.20 -require github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 +require github.com/hasura/go-graphql-client v0.9.3 require ( github.com/google/uuid v1.3.0 // indirect - github.com/hasura/go-graphql-client v0.8.1 // indirect - github.com/joho/godotenv v1.4.0 // indirect - github.com/klauspost/compress v1.15.12 // indirect - golang.org/x/net v0.2.0 // indirect + github.com/klauspost/compress v1.16.7 // indirect + golang.org/x/sys v0.11.0 // indirect nhooyr.io/websocket v1.8.7 // indirect ) diff --git a/go.sum b/go.sum index 7ca4917..ecccaf7 100644 --- a/go.sum +++ b/go.sum @@ -1,61 +1,87 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= +github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/graph-gophers/graphql-go v1.4.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os= +github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc= +github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os= +github.com/graph-gophers/graphql-transport-ws v0.0.2 h1:DbmSkbIGzj8SvHei6n8Mh9eLQin8PtA8xY9eCzjRpvo= github.com/graph-gophers/graphql-transport-ws v0.0.2/go.mod h1:5BVKvFzOd2BalVIBFfnfmHjpJi/MZ5rOj8G55mXvZ8g= -github.com/hasura/go-graphql-client v0.8.1 h1:yU4888urgkW4L47cs+QQDXl3YfVaNraUqym5qsJ41Ms= -github.com/hasura/go-graphql-client v0.8.1/go.mod h1:NVifIwv+YFIUYGLQ7SM2/vBbzS/9rFP4vmIf/vf/zXM= -github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= -github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/hasura/go-graphql-client v0.9.3 h1:Xi3fqa2t9q4nJ2jM2AU8nB6qeAoMpbcYDiOSBnNAN1E= +github.com/hasura/go-graphql-client v0.9.3/go.mod h1:AarJlxO1I59MPqU/TC7gQP0BMFgPEqUTt5LYPvykasw= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= -github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM= -github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 h1:B1PEwpArrNp4dkQrfxh/abbBAOZBVp0ds+fBEOUOqOc= -github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI= go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs= -golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU= -golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= diff --git a/pkg/apollostudio/client.go b/pkg/apollostudio/client.go deleted file mode 100644 index 1e69c7f..0000000 --- a/pkg/apollostudio/client.go +++ /dev/null @@ -1,53 +0,0 @@ -package apollostudio - -import ( - "errors" - "net/http" - "strings" - - "github.com/hasura/go-graphql-client" -) - -type Client struct { - httpClient *http.Client - gqlClient *graphql.Client - key string - GraphRef string - GraphID string - Variant string -} - -type ClientOpts struct { - APIKey string - GraphRef string -} - -func NewClient(opts ClientOpts) (*Client, error) { - httpClient := http.Client{ - Transport: &headerTransport{ - APIKey: opts.APIKey, - }, - } - - gqlClient := graphql.NewClient( - "https://graphql.api.apollographql.com/api/graphql", - &httpClient, - ) - - if opts.GraphRef == "" { - return nil, errors.New("graph ref is required") - } - - p := strings.Split(opts.GraphRef, "@") - if len(p) < 2 { - return nil, errors.New("missing variant in graph ref") - } - - return &Client{ - gqlClient: gqlClient, - httpClient: &httpClient, - GraphRef: opts.GraphRef, - GraphID: p[0], - Variant: p[1], - }, nil -} diff --git a/pkg/apollostudio/transport.go b/pkg/apollostudio/transport.go deleted file mode 100644 index 0aa38f1..0000000 --- a/pkg/apollostudio/transport.go +++ /dev/null @@ -1,12 +0,0 @@ -package apollostudio - -import "net/http" - -type headerTransport struct { - APIKey string -} - -func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) { - req.Header.Add("x-api-key", t.APIKey) - return http.DefaultTransport.RoundTrip(req) -} From 353d734667b71d9689bdcb7ecc4db14b1e5bdcca Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Wed, 9 Aug 2023 09:51:49 +0200 Subject: [PATCH 2/6] Added tests and pipeline --- .github/workflows/go-test.yml | 28 ++++++++++++++++++++++++++++ .gitignore | 3 ++- README.md | 4 ++-- apollostudio/client.go | 16 ++++++++-------- apollostudio/client_test.go | 33 +++++++++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 10 ++++++++++ 7 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/go-test.yml create mode 100644 apollostudio/client_test.go diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml new file mode 100644 index 0000000..61cdbfe --- /dev/null +++ b/.github/workflows/go-test.yml @@ -0,0 +1,28 @@ +name: Go Tests + +on: [ push ] + +jobs: + + test: + runs-on: ubuntu-latest + strategy: + max-parallel: 4 + matrix: + go-version: [ '1.20' ] + steps: + - uses: actions/checkout@v3 + + - name: Set up Go ${{ matrix.go-version }} + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + + - name: Install dependencies + run: go get ./... + + - name: Run tests + run: go test -race -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... ./... + + - name: Upload to codecov + uses: codecov/codecov-action@v1.0.6 diff --git a/.gitignore b/.gitignore index 451fb35..d29de80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env -.idea \ No newline at end of file +.idea +coverage.txt diff --git a/README.md b/README.md index 9411054..48a56b2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Amplience GO SDK +# Apollo Studio GO SDK Go SDK for [Apollo Studio](https://studio.apollographql.com/). @@ -19,4 +19,4 @@ go build ./cmd/debug ## Contributing -Apollo Studio GraphQL explorer can be found [here](https://studio.apollographql.com/public/apollo-platform/variant/main/explorer). \ No newline at end of file +Apollo Studio GraphQL explorer can be found [here](https://studio.apollographql.com/public/apollo-platform/variant/main/explorer). diff --git a/apollostudio/client.go b/apollostudio/client.go index 2897394..4c69865 100644 --- a/apollostudio/client.go +++ b/apollostudio/client.go @@ -2,6 +2,7 @@ package apollostudio import ( "errors" + "fmt" "net/http" "strings" @@ -55,18 +56,18 @@ func WithRequestModifier(modifier graphql.RequestModifier) ClientOpt { type GraphRef string -func ValidateGraphRef(graphRef string) error { +func isValidGraphRef(graphRef string) bool { if graphRef == "" { - return errors.New("graph ref is required") + return false } p := strings.Split(graphRef, "@") - if len(p) < 2 { - return errors.New("missing variant in graph ref") + if len(p) != 2 { + return false } - return nil + return true } func (g *GraphRef) getGraphId() string { @@ -94,9 +95,8 @@ func NewClient(apiKey string, graphRef string, opts ...ClientOpt) (*Client, erro opt(settings) } - err := ValidateGraphRef(graphRef) - if err != nil { - return nil, err + if !isValidGraphRef(graphRef) { + return nil, errors.New(fmt.Sprintf("Provided invalid graph ref. Expected ref in format *@*, received %s", graphRef)) } gqlClient := graphql.NewClient(settings.url, settings.httpClient) diff --git a/apollostudio/client_test.go b/apollostudio/client_test.go new file mode 100644 index 0000000..14f7553 --- /dev/null +++ b/apollostudio/client_test.go @@ -0,0 +1,33 @@ +package apollostudio + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestValidateGraphRef(t *testing.T) { + tests := []struct { + ref string + valid bool + }{ + {ref: "foo@bar", valid: true}, + {ref: "foobar", valid: false}, + } + + for _, test := range tests { + valid := isValidGraphRef(test.ref) + assert.Equal(t, test.valid, valid) + } +} + +func TestGraphRefGetGraphId(t *testing.T) { + grapRef := GraphRef("foo@bar") + + assert.Equal(t, "foo", grapRef.getGraphId()) +} + +func TestGraphRefGetVariant(t *testing.T) { + grapRef := GraphRef("foo@bar") + + assert.Equal(t, "bar", grapRef.getVariant()) +} diff --git a/go.mod b/go.mod index a7405c9..2488f88 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,13 @@ go 1.20 require github.com/hasura/go-graphql-client v0.9.3 require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/uuid v1.3.0 // indirect github.com/klauspost/compress v1.16.7 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect golang.org/x/sys v0.11.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect nhooyr.io/websocket v1.8.7 // indirect ) diff --git a/go.sum b/go.sum index ecccaf7..122c6ab 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= @@ -51,12 +52,19 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= @@ -84,5 +92,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= From 722f0a74a6d6243e9821a5d433cf81b4fc462ca5 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Wed, 30 Aug 2023 14:22:58 +0200 Subject: [PATCH 3/6] updated dependencies --- go.mod | 9 +++++---- go.sum | 37 +++++++------------------------------ 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index 2488f88..54067fb 100644 --- a/go.mod +++ b/go.mod @@ -2,15 +2,16 @@ module github.com/labd/apollostudio-go-sdk go 1.20 -require github.com/hasura/go-graphql-client v0.9.3 +require ( + github.com/hasura/go-graphql-client v0.10.0 + github.com/stretchr/testify v1.8.4 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.3.1 // indirect github.com/klauspost/compress v1.16.7 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.5.0 // indirect - github.com/stretchr/testify v1.8.4 // indirect golang.org/x/sys v0.11.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect nhooyr.io/websocket v1.8.7 // indirect diff --git a/go.sum b/go.sum index 122c6ab..2ffc6e0 100644 --- a/go.sum +++ b/go.sum @@ -5,9 +5,6 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= -github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= @@ -24,20 +21,17 @@ github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/E github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= -github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= +github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= +github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc= -github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os= github.com/graph-gophers/graphql-transport-ws v0.0.2 h1:DbmSkbIGzj8SvHei6n8Mh9eLQin8PtA8xY9eCzjRpvo= -github.com/graph-gophers/graphql-transport-ws v0.0.2/go.mod h1:5BVKvFzOd2BalVIBFfnfmHjpJi/MZ5rOj8G55mXvZ8g= -github.com/hasura/go-graphql-client v0.9.3 h1:Xi3fqa2t9q4nJ2jM2AU8nB6qeAoMpbcYDiOSBnNAN1E= -github.com/hasura/go-graphql-client v0.9.3/go.mod h1:AarJlxO1I59MPqU/TC7gQP0BMFgPEqUTt5LYPvykasw= +github.com/hasura/go-graphql-client v0.10.0 h1:eQm/ap/rqxMG6yAGe6J+FkXu1VqJ9p21E63vz0A7zLQ= +github.com/hasura/go-graphql-client v0.10.0/go.mod h1:z9UPkMmCBMuJjvBEtdE6F+oTR2r15AcjirVNq/8P+Ig= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= @@ -51,47 +45,30 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= -go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI= -go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs= -golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= From 63e958ca50a87400e56a2f911e594062e000506d Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Wed, 30 Aug 2023 14:40:52 +0200 Subject: [PATCH 4/6] Added metafiles for changie, updated readme --- .changes/header.tpl.md | 6 ++++ .changes/unreleased/.gitkeep | 0 .changie.yaml | 26 ++++++++++++++++ .github/workflows/go-test.yml | 28 ------------------ .github/workflows/tests.yaml | 48 ++++++++++++++++++++++++++++++ CHANGELOG.md | 9 ++++++ LICENSE | 21 +++++++++++++ README.md | 56 ++++++++++++++++++++++++++++------- apollostudio/client_test.go | 8 ++--- 9 files changed, 159 insertions(+), 43 deletions(-) create mode 100644 .changes/header.tpl.md create mode 100644 .changes/unreleased/.gitkeep create mode 100644 .changie.yaml delete mode 100644 .github/workflows/go-test.yml create mode 100644 .github/workflows/tests.yaml create mode 100644 CHANGELOG.md create mode 100644 LICENSE diff --git a/.changes/header.tpl.md b/.changes/header.tpl.md new file mode 100644 index 0000000..df8faa7 --- /dev/null +++ b/.changes/header.tpl.md @@ -0,0 +1,6 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), +and is generated by [Changie](https://github.com/miniscruff/changie). diff --git a/.changes/unreleased/.gitkeep b/.changes/unreleased/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/.changie.yaml b/.changie.yaml new file mode 100644 index 0000000..906d495 --- /dev/null +++ b/.changie.yaml @@ -0,0 +1,26 @@ +changesDir: .changes +unreleasedDir: unreleased +headerPath: header.tpl.md +changelogPath: CHANGELOG.md +versionExt: md +versionFormat: '## {{.Version}} - {{.Time.Format "2006-01-02"}}' +kindFormat: '### {{.Kind}}' +changeFormat: '* {{.Body}}' +kinds: +- label: Added + auto: minor +- label: Changed + auto: major +- label: Deprecated + auto: minor +- label: Removed + auto: major +- label: Fixed + auto: patch +- label: Security + auto: patch +newlines: + afterChangelogHeader: 1 + beforeChangelogVersion: 1 + endOfVersion: 1 +envPrefix: CHANGIE_ diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml deleted file mode 100644 index 61cdbfe..0000000 --- a/.github/workflows/go-test.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Go Tests - -on: [ push ] - -jobs: - - test: - runs-on: ubuntu-latest - strategy: - max-parallel: 4 - matrix: - go-version: [ '1.20' ] - steps: - - uses: actions/checkout@v3 - - - name: Set up Go ${{ matrix.go-version }} - uses: actions/setup-go@v4 - with: - go-version: ${{ matrix.go-version }} - - - name: Install dependencies - run: go get ./... - - - name: Run tests - run: go test -race -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... ./... - - - name: Upload to codecov - uses: codecov/codecov-action@v1.0.6 diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 0000000..9781f1c --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,48 @@ +name: Run Tests + +on: [ push ] + +jobs: + + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up Go 1.20 + uses: actions/setup-go@v4 + with: + go-version: "1.20" + + - name: golangci-lint + continue-on-error: true + uses: golangci/golangci-lint-action@v3 + with: + args: --issues-exit-code=0 --timeout=5m + + - name: Run tests + run: go test -race -coverprofile=coverage.out -covermode=atomic -coverpkg=./... -v ./... + + - name: Upload to codecov + uses: codecov/codecov-action@v3 + with: + verbose: true + + changie: + runs-on: ubuntu-latest + needs: test + if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' + permissions: + contents: write + pull-requests: write + actions: write + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Prepare release + uses: labd/changie-release-action@v0.3.0 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5ca9d33 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), +and is generated by [Changie](https://github.com/miniscruff/changie). + + +No releases yet, this file will be updated when generating your first release. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..78f0f94 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Lab Digital + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 48a56b2..2dd8fd8 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,54 @@ -# Apollo Studio GO SDK +# Apollo Studio Go SDK Go SDK for [Apollo Studio](https://studio.apollographql.com/). -## Development +## Installation -To test the API, run `cmd/debug/main.go` file with your own Apollo studio graph credentials. +```bash +go get github.com/labd/apollostudio-go-sdk +``` + +## Usage + +```go +package main + +import ( + "context" + "github.com/labd/apollostudio-go-sdk/apollostudio" +) + +func main() { + key := "your-api-key" + ref := "your-schema-reference" + client, err := apollostudio.NewClient(key, ref) + if err != nil { + panic(err) + } + + _, _ := client.SubmitSubGraph( + context.Background(), + &apollostudio.SubmitOptions{ + SubGraphSchema: []byte("schema { query: Query } type Query { hello: String }"), + SubGraphName: "my-subgraph", + SubGraphURL: "https://my-subgraph.com/graphql", + }, + ) +} ``` -export APOLLO_API_KEY=your_api_key -export APOLLO_GRAPH_REF=your_graph_ref -export APOLLO_SUB_GRAPH_SCHEMA=your_sub_graph_schema -export APOLLO_SUB_GRAPH_NAME=your_sub_graph_name -export APOLLO_SUB_GRAPH_URL=your_sub_graph_url - -go build ./cmd/debug -./debug -h + +The client allows for several additional options to be set, which can extend its functionality. + +```go +var clientOpts = []apollostudio.ClientOpt{ + apollostudio.WithHttpClient(http.DefaultClient), + apollostudio.WithDebug(true), + apollostudio.WithUrl("https://studio.apollographql.com/api/graphql"), +} + +client, err := apollostudio.NewClient(key, ref, clientOpts...) + ``` ## Contributing diff --git a/apollostudio/client_test.go b/apollostudio/client_test.go index 14f7553..c98cae4 100644 --- a/apollostudio/client_test.go +++ b/apollostudio/client_test.go @@ -21,13 +21,13 @@ func TestValidateGraphRef(t *testing.T) { } func TestGraphRefGetGraphId(t *testing.T) { - grapRef := GraphRef("foo@bar") + graphRef := GraphRef("foo@bar") - assert.Equal(t, "foo", grapRef.getGraphId()) + assert.Equal(t, "foo", graphRef.getGraphId()) } func TestGraphRefGetVariant(t *testing.T) { - grapRef := GraphRef("foo@bar") + graphRef := GraphRef("foo@bar") - assert.Equal(t, "bar", grapRef.getVariant()) + assert.Equal(t, "bar", graphRef.getVariant()) } From 80385611bae86e1b841df62b988f16c3f26e906b Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Wed, 30 Aug 2023 14:43:40 +0200 Subject: [PATCH 5/6] Added changie file --- .changes/unreleased/Added-20230830-144259.yaml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changes/unreleased/Added-20230830-144259.yaml diff --git a/.changes/unreleased/Added-20230830-144259.yaml b/.changes/unreleased/Added-20230830-144259.yaml new file mode 100644 index 0000000..3c04f50 --- /dev/null +++ b/.changes/unreleased/Added-20230830-144259.yaml @@ -0,0 +1,3 @@ +kind: Added +body: Added additional client options to allow overriding of default http client +time: 2023-08-30T14:42:59.707696257+02:00 From 2e71e38292dbb5dd04a8e14c5f5d9e24aa660ed0 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Wed, 30 Aug 2023 14:58:24 +0200 Subject: [PATCH 6/6] Added v1.0.0 changelog --- .changes/v1.0.0.md | 3 +++ CHANGELOG.md | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changes/v1.0.0.md diff --git a/.changes/v1.0.0.md b/.changes/v1.0.0.md new file mode 100644 index 0000000..e4e558b --- /dev/null +++ b/.changes/v1.0.0.md @@ -0,0 +1,3 @@ +## v1.0.0 - 2023-05-04 +### Changed +* rename from go-apollostudio-sdk to standard format apollostudio-go-sdk diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ca9d33..579c5b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,4 +6,6 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), and is generated by [Changie](https://github.com/miniscruff/changie). -No releases yet, this file will be updated when generating your first release. +## 1.0.0 - 2023-05-04 +### Changed +* rename from go-apollostudio-sdk to standard format apollostudio-go-sdk