A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I am using the latest Remix template (3.3.2) and trying to make a Admin GraphQL request after receiving a webhook. However the graphql request returns a 401 - GraphQL Client: Unauthorized.
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";
export const action = async ({ request }: ActionFunctionArgs) => {
const { payload, admin } = await authenticate.webhook(request);
if (admin) {
try {
const response = await admin.graphql(
`#graphql
query getDiscountData($id: ID!) {
discountNode(id: $id) {
id
}
}
`,
{
variables: {
id: payload.admin_graphql_api_id
}
}
);
const data = await response.json();
console.log(data);
} catch (error: any) {
const errorObj = await error.json()
console.log(errorObj.errors)
}
}
return new Response();
};
I'm unsure where I went wrong.
Did you setup the correct API access scopes? Log in to your partner dashboard, go to the app section and select the app you are working on. Then head down to the API access section and check the configured scopes. It should look something like this:
I currently have read_discounts access scope as the only scope (which I believe is the only one I need for that call) and confirmed it by looking in the app in my partner dashboard.
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";
export const action = async ({ request }: ActionFunctionArgs) => {
const { payload, admin } = await authenticate.webhook(request);
if (admin) {
try {
const response = await admin.graphql(
`#graphql
query getDiscountData($id: ID!) {
discountNode(id: $id) {
id
discount {
... on DiscountCodeBasic {
title
}
... on DiscountCodeBxgy {
title
}
... on DiscountCodeFreeShipping {
title
}
... on DiscountAutomaticApp {
title
}
... on DiscountAutomaticBasic {
title
}
... on DiscountAutomaticBxgy {
title
}
... on DiscountAutomaticFreeShipping {
title
}
}
}
}
`,
{
variables: {
id: payload.admin_graphql_api_id
}
}
);
const data = await response.json();
console.log(data);
} catch (error: any) {
const errorObj = await error.json()
console.log(errorObj.errors)
}
}
return new Response();
};
Even if you use the above code do you still get the same error? Also, you can always try to use shopify app deploy and see if that helps.
The last thing you can do is verify that the appropriate authorization headers are included when making the GraphQL request. The admin.graphql() client should add these automatically if configured correctly. However, you might need to ensure the access token has not expired or is valid.