#Using Shopify Graphql Admin API

1 messages · Page 1 of 1 (latest)

gaunt nest
#

In this situation you need to wrap your calls in runTask. Let me see if I have any useful examples to make this a bit easier. There are some patterns you can use to make it a bit less laborious.

#

You can use io.shopify.runTask to perform anything that SDK supports, but I have a feeing the SDK is different for GraphQL

#

This is how you do shopify.runTask and you can see what's available in auto-complete

#

This will make it much easier to wrap things in runTask for this use case:

async function shopifyTask<T, TResult extends Json<T> | void>(
  io: IO,
  key: IntegrationTaskKey,
  callback: (client: ReturnType<typeof createAdminApiClient>, task: IOTask) => Promise<TResult>,
  options?: RunTaskOptions,
  errorCallback?: RunTaskErrorCallback
): Promise<TResult> {
  return io.runTask<TResult>(
    key,
    (task, io) => {
      return callback(shopify, task);
    },
    {
      icon: "shopify",
      retry: retry.standardBackoff,
      ...(options ?? {}),
    },
    errorCallback
  );
}
#

You'll have to import a load of things from the @trigger.dev/sdk btw, but everything there should be public.

#

Complete example with the above function and using it with GraphQL:

const shopify = createAdminApiClient({
  storeDomain: process.env.storeURL!,
  accessToken: process.env.adminAppAccessToken!,
  apiVersion: "2024-01",
  retries: 1,
});

client.defineJob({
  id: "example",
  name: "example",
  version: "0.1.0",
  trigger: invokeTrigger(),
  run: async (payload, io, ctx) => {
    const result = await shopifyTask(io, "example", async (client, task) => {
      const operation = `
      query ProductQuery($id: ID!) {
        product(id: $id) {
          id
          title
          handle
        }
      }
    `;
      const { data, errors } = await client.request(operation, {
        variables: {
          id: "gid://shopify/Product/7608002183224",
        },
      });

      if (errors) {
        throw new Error(errors.message);
      }

      return data;
    });
  },
});

async function shopifyTask<T, TResult extends Json<T> | void>(
  io: IO,
  key: IntegrationTaskKey,
  callback: (client: ReturnType<typeof createAdminApiClient>, task: IOTask) => Promise<TResult>,
  options?: RunTaskOptions,
  errorCallback?: RunTaskErrorCallback
): Promise<TResult> {
  return io.runTask<TResult>(
    key,
    (task, io) => {
      return callback(shopify, task);
    },
    {
      icon: "shopify",
      retry: retry.standardBackoff,
      ...(options ?? {}),
    },
    errorCallback
  );
}
#

Hmm this Shopify SDK has crappy types though

#

So it's getting an any back

#

I'm pretty sure we chose REST because this is such a huge pain to deal with (I didn't create this integration).