hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
395 stars 91 forks source link

Keep getting 406 Not Acceptable on my Shopify query #136

Closed Nikola-Milovic closed 3 months ago

Nikola-Milovic commented 3 months ago

Hey! I am not sure this is an issue with the library per say, but I've been trying to get it work but helplessly. Any ideas on how to approach debugging this would be greatly appreciated, everything seems fine on my end. I tried adding more headers like Accept and such, but that didn't help.

From the shopify docs

406 Not Acceptable
The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

And the query, I am trying to get working, docs. It's a bit tricky to debug since you need access token to run the queries but I can provide one temporarily if needed for debugging.

Here's what I am getting

=== RUN   TestActivities/TestFetchProducts
 "{products(first: 100){edges{cursor,node{bodyHtml,createdAt,customProductType,description,descriptionHtml,descriptionPlainSummary,handle,id,onlineStorePreviewUrl,onlineStoreUrl,productType,requiresSellingPlan,status,storefrontId,tags,templateSuffix,title,totalInventory,totalVariants,tracksInventory,updatedAt,vendor,featuredImage{altText,height,width,originalSrc,src,transformedSrc},isGiftCard,publishedAt}},pageInfo{endCursor,hasNextPage,hasPreviousPage,startCursor}}}"

 <nil>    activity_test.go:50:
                Error Trace:    /shopify/activity/activity_test.go:50
                Error:          Failed to fetch
                Test:           TestActivities/TestFetchProducts
                Messages:       failed to execute graphql request, Message: 406 Not Acceptable; body: "", Locations: [], Extensions: map[code:request_error internal:map[request:map[body:{"query":"{products(first: 100){edges{cursor,node{bodyHtml,createdAt,customProductType,description,descriptionHtml,descriptionPlainSummary,handle,id,onlineStorePreviewUrl,onlineStoreUrl,productType,requiresSellingPlan,status,storefrontId,tags,templateSuffix,title,totalInventory,totalVariants,tracksInventory,updatedAt,vendor,featuredImage{altText,height,width,originalSrc,src,transformedSrc},isGiftCard,publishedAt}},pageInfo{endCursor,hasNextPage,hasPreviousPage,startCursor}}}"}
                                 headers:map[Accept:[*/*] Content-Type:[application/json] X-Shopify-Access-Token:[shpua_9a9efxxxxxxf6]]]]], Path: []
--- FAIL: TestActivities (0.29s)
    --- FAIL: TestActivities/TestFetchProducts (0.28s)
FAIL
FAIL    seolitic/internal/shopify/activity      0.303s
FAIL

But if I copy the exact same query that's being printed out, I get the response in postman.

screenshot of postman

The code itself

func getGraphQLClient(shopIdentifier, accessToken string) *graphql.Client {
    reqMod := graphql.RequestModifier(
        func(r *http.Request) {
            r.Header.Add("X-Shopify-Access-Token", accessToken)
        })
    c := graphql.NewClient(fmt.Sprintf("https://%s/admin/api/2024-4/graphql.json", shopIdentifier), nil).
        WithRequestModifier(reqMod).
        WithDebug(os.Getenv("DEBUG") == "true")
    return c
}

     type ProductsResponse struct {
        Products struct {
            Edges []struct {
                Cursor string         `graphql:"cursor"`
                Node   domain.Product `graphql:"node"`
            } `graphql:"edges"`
            PageInfo struct {
                EndCursor       string `graphql:"endCursor"`
                HasNextPage     bool   `graphql:"hasNextPage"`
                HasPreviousPage bool   `graphql:"hasPreviousPage"`
                StartCursor     string `graphql:"startCursor"`
            } `graphql:"pageInfo"`
        } `graphql:"products(first: 100)"`
    }

    client := getGraphQLClient(params.ShopIdentifier, params.AccessToken)

    var query ProductsResponse

    q, err := graphql.ConstructQuery(&query, nil)
    fmt.Printf("\n\n %q \n\n %v", q, err)

    if err := client.Query(ctx, &query, nil); err != nil {
        return nil, fmt.Errorf("failed to execute graphql request, %w", err)
    }
hgiasac commented 3 months ago

I see the URL in the docs is https://your-development-store.myshopify.com/admin/api/2024-04/graphql.json, but your URL is https://%s/admin/api/2024-4/graphql.json. Maybe you miss the zero in 2024-04?

Nikola-Milovic commented 3 months ago

Good bless your eyesight, thank you! Didn't even think about looking in the URL