Shopify / shopify-app-template-remix

371 stars 149 forks source link

GraphQL client is missing from shopifyApp instance #402

Open gyotov opened 1 year ago

gyotov commented 1 year ago

Issue summary

This guide uses the session to interact with the admin graphql, but such is missing from the shopifyApp instance imported from @shopify/shopify-app-remix/server. shopify.clients and shopify.clients.Graphql are undefined.

This is needed in order to interact with the GraphQL Admin API without the request, as mentioned here, since there are situations, where we need to take actions based on some delayed job, where there is no request.

Expected behavior

shopify.clients.Graphql should return a Graphql client, ready to be used, like it does, when shopify is instantiated from import { shopifyApi } from '@shopify/shopify-api'.

Actual behavior

shopify.clients.Graphql is missing.

Steps to reproduce the problem

  1. Open app\shopify.server.js
  2. Try to access shopify.clients.Graphql, as described here
  3. Observe that it is missing
paulomarg commented 1 year ago

Hi, thank you for reporting this. Currently, our examples are not covering the Remix way of using the GraphQL client, we'll look into improving them to make it easier to set up your Remix app!

In the meantime, here is a working example of how you would use the GraphQL client in the Remix template: https://shopify.dev/docs/api/shopify-app-remix/v2/apis/admin-api#example-graphql.

If you need to make requests from e.g. a background job, you can use this to get an unauthenticated context:

const {admin} = shopify.unauthenticated.admin(shop);

and then you can use the admin.graphql function like you would in other requests. Please note that this function does not run any validations, so you should not rely on user input for the shop!

Hope this helps!

gyotov commented 1 year ago

@paulomarg Thank you for the response, I've used exactly the same approach that you've described, but it took me some time to figure it out. I think this should be well documented, as it is the only way to use GraphQL API, in some queued job context, where you typically don't have a request object.