change to admin api to fetch YMM options

This commit is contained in:
Chloe
2024-07-09 11:49:38 +07:00
parent eb9b6a9e59
commit 5ef8470526
10 changed files with 222 additions and 87 deletions

View File

@@ -34,6 +34,7 @@ import {
} from './mutations/cart';
import { createFileMutation, createStageUploads } from './mutations/file';
import { updateOrderMetafieldsMutation } from './mutations/order';
import { getMetaobjectReferencesQuery } from './queries/admin/metaobjects';
import { getCartQuery } from './queries/cart';
import {
getCollectionProductsQuery,
@@ -926,6 +927,34 @@ export async function getAllMetaobjects(type: string) {
return allMetaobjects;
}
export async function getMetaobjectReferences(
id: string,
after?: string
): Promise<{ references: Metaobject[]; pageInfo: PageInfo | null }> {
const res = await shopifyAdminFetch<{
variables: {
id: string;
after?: string;
};
data: { metaobject: ShopifyMetaobject };
}>({
query: getMetaobjectReferencesQuery,
variables: { id, after }
});
const metaobject = res.body.data.metaobject;
if (!metaobject || !metaobject.referencedBy) {
return { references: [], pageInfo: null };
}
const references = removeEdgesAndNodes(metaobject.referencedBy).map(
({ referencer }) => referencer
);
const pageInfo = metaobject.referencedBy.pageInfo;
return { references: reshapeMetaobjects(references), pageInfo };
}
export async function getMetaobjectsByIds(ids: string[]) {
if (!ids.length) return [];

View File

@@ -0,0 +1,30 @@
export const getMetaobjectReferencesQuery = /* GraphQL */ `
query getMetaobjectReferences($id: ID!, $after: String) {
metaobject(id: $id) {
id
referencedBy(first: 20, after: $after) {
edges {
node {
key
referencer {
... on Metaobject {
id
type
fields {
key
value
}
}
}
}
}
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
}
}
}
}
`;

View File

@@ -390,6 +390,7 @@ export type ShopifyMetaobject = {
image?: Image;
};
}>;
referencedBy?: Connection<{ referencer: ShopifyMetaobject }> & { pageInfo: PageInfo };
};
export type ShopifyMetafield = {