bold-commerce / go-shopify

Go client for the Shopify API
MIT License
328 stars 255 forks source link

GraphQL Endpoint #237

Closed nickfthedev closed 1 year ago

nickfthedev commented 1 year ago

Hello there! I saw that there is an GraphQL endpoint in the source code but i can't find out how to use it.

Maybe you can help?

oliver006 commented 1 year ago

The PR that introduced the GraphQL endpoint (thx @weirdian2k3 !) has an example: https://github.com/bold-commerce/go-shopify/pull/228

nickfthedev commented 1 year ago

@oliver006 Thank you

@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?

I did some research on this: https://shopify.dev/docs/api/usage/response-codes

According to this the response code 406 means the following: The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

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{}{})
}
weirdian2k3 commented 1 year ago

Sorry for taking so long to get back to you. One thing I forgot to note is that you need to specify a newer version to use graphql, the default for this library isn't high enough

Something like

client := goshopifyNewClient("shop-name", "token", goshopify.WithVersion("2023-07"))
nickfthedev commented 1 year ago

Sorry for taking so long to get back to you. One thing I forgot to note is that you need to specify a newer version to use graphql, the default for this library isn't high enough

Something like

client := goshopifyNewClient("shop-name", "token", goshopify.WithVersion("2023-07"))

Hello Ian,

Thank you very much for your fast reply! I will try it out as soon as possible

nickfthedev commented 1 year ago

It works! That was the thing I was missing. Thank you so much!