I’m currently trying to query GraphQL Admin API in my public Node + React Shopify app.
When I query GraphQL, I have no idea if I’m querying Admin API or Storefront API, and I have no idea where I can specify which one to query… I’m trying to get a list of the last 10 recent orders, but I get a 400 bad request error saying, "Cannot query field “orders” on type “Query” which probably means I’m querying Storefront API instead of Admin API.
I understand the frontend of my app cannot query Admin API. So, please check my code snippets below - my question is… Am I querying GraphQL from the frontend or the backend? To me, it looks like I’m sending the query to my app’s backend which uses Apollo and to actually perform the query. Am I wrong?
This is the code in my react component:
const app = useAppBridge();
const fetch = userLoggedInFetch(app);
const ORDERS_QUERY = gql`
query getOrders {
orders(first:5) {
edges {
node {
id
}
}
}
}
`;
const { loading, error, data, refetch } = useQuery(ORDERS_QUERY);
if (loading) {
console.log('loading...');
return
This is my Apollo setup in App.jsx:
```javascript
function MyProvider({ children }) {
const app = useAppBridge();
const client = new ApolloClient({
cache: new InMemoryCache(),
link: new HttpLink({
credentials: "include",
fetch: userLoggedInFetch(app),
}),
});
return ;
}
All pages are wrapped in the Apollo Provider. As I saw in Apollo Client documentation, by default, GraphQL queries are posted to the /graphql endpoint.
On the backend, in my Node/Express server setup file (/server/index.js), this is the config for the /graphql endpoint:
app.post("/graphql", verifyRequest(app), async (req, res) => {
try {
const response = await Shopify.Utils.graphqlProxy(req, res);
res.status(200).send(response.body);
} catch (error) {
res.status(500).send(error.message);
}
});