bold-commerce / go-shopify

Go client for the Shopify API
MIT License
330 stars 256 forks source link

Add GraphQL endpoint #228

Closed weirdian2k3 closed 1 year ago

weirdian2k3 commented 1 year ago

This is a re-introduction of an old PR by another person, trimmed down to just the GraphQL Client and a typo-fix https://github.com/bold-commerce/go-shopify/pull/118

I'm not sure why the original one got closed, but I've succeessfully used it for making mutations with queries

    req := `mutation webPixelCreate($webPixel: WebPixelInput!) {
        webPixelCreate(webPixel: $webPixel) {
            userErrors {
                code
                field
                message
            }
            webPixel {
                settings
                id
            }
        }
    }`

    settings, err := json.Marshal(map[string]interface{}{
        "accountID": fmt.Sprintf("%d", websiteID),
    })

    if err != nil {
        return err
    }

    variables := map[string]interface{}{
        "webPixel": map[string]interface{}{
            "settings": string(settings),
        },
    }

    var foo struct {
    }

    client.GraphQL.Query(req, &variables, &foo)
oliver006 commented 1 year ago

Thanks for the PR! I'm a bit busy this week but I'll try to review it in the next couple of days.

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (33479a2) 100.00% compared to head (24e796a) 100.00%.

:exclamation: Current head 24e796a differs from pull request most recent head 94d3d6b. Consider uploading reports for the commit 94d3d6b to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #228 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 44 45 +1 Lines 1930 1994 +64 ========================================= + Hits 1930 1994 +64 ``` | [Files Changed](https://app.codecov.io/gh/bold-commerce/go-shopify/pull/228?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bold+Commerce) | Coverage Δ | | |---|---|---| | [carrier.go](https://app.codecov.io/gh/bold-commerce/go-shopify/pull/228?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bold+Commerce#diff-Y2Fycmllci5nbw==) | `100.00% <ø> (ø)` | | | [options.go](https://app.codecov.io/gh/bold-commerce/go-shopify/pull/228?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bold+Commerce#diff-b3B0aW9ucy5nbw==) | `100.00% <ø> (ø)` | | | [goshopify.go](https://app.codecov.io/gh/bold-commerce/go-shopify/pull/228?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bold+Commerce#diff-Z29zaG9waWZ5Lmdv) | `100.00% <100.00%> (ø)` | | | [graphql.go](https://app.codecov.io/gh/bold-commerce/go-shopify/pull/228?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bold+Commerce#diff-Z3JhcGhxbC5nbw==) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

nickfthedev commented 1 year ago

@weirdian2k3 Hello Ian, Thank you for creating this endpoint. However, the example above is not working in my case. I get the error "unacceptable". Maybe you stumbled across a similar error?

func GraphQLTest(c echo.Context) error {
    // Get Access Token
    t, err := utils.GetAccessToken(c)
    if err != nil {
        fmt.Printf("Error: %s\n", err.Error())
        return c.String(500, "Token Error")
    }
    fmt.Printf("Token: %s\n", t.Token)
    // Endpoint Test
    client := goshopify.NewClient(utils.ShopifyApp, t.Shopname, t.Token)

    req := `mutation webPixelCreate($webPixel: WebPixelInput!) {
        webPixelCreate(webPixel: $webPixel) {
            userErrors {
                code
                field
                message
            }
            webPixel {
                settings
                id
            }
        }
    }`

    settings, err := json.Marshal(map[string]interface{}{
        "accountID": fmt.Sprintf("%d", 12121324),
    })

    if err != nil {
        fmt.Printf("Error: %v\n", err.Error())
        return c.String(500, " Error")
    }

    variables := map[string]interface{}{
        "webPixel": map[string]interface{}{
            "settings": string(settings),
        },
    }

    var foo struct {
    }

    err = client.GraphQL.Query(req, &variables, &foo)
    if err != nil {
        fmt.Printf("Error: %v\n", err.Error())
        return c.String(500, " Error")

    }
    return c.Render(http.StatusOK, "hello.html", map[string]interface{}{})
}
johannessteu commented 7 months ago

Same for me, even for simple examples like { shop { name } }} i do get a Not Acceptable response

johannessteu commented 7 months ago

@nickfthedev i think i found it. You should set an explicit version with goshopify.WithVersion("2024-04"). The graphql api does not allow a path myshopify.com/admin/api/graphql.json but myshopify.com/admin/api/2024-04/graphql.json