Skip to content

Instantly share code, notes, and snippets.

@kylebuildsstuff
Created February 13, 2020 22:03
Show Gist options
  • Save kylebuildsstuff/f34ea7b9b8e4ff3042c6afbf397bd638 to your computer and use it in GitHub Desktop.
Save kylebuildsstuff/f34ea7b9b8e4ff3042c6afbf397bd638 to your computer and use it in GitHub Desktop.

Revisions

  1. kylebuildsstuff created this gist Feb 13, 2020.
    59 changes: 59 additions & 0 deletions graphql-setup
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // cross-sell.graphql.ts

    export const crossSellQueryTypeDefinitions = `
    crossSell(crossSellId: Int): CrossSell
    productCrossSells(productId: String): [CrossSell!]
    `;

    export const crossSellTypeDefinitions = `
    type CrossSell {
    displaysProductPrice: Boolean
    displaysProductTitle: Boolean
    id: Int
    productId: String
    shopId: Int
    title: String
    type: String
    typeValue: String
    customCrossSellProductIds: [String!]
    }
    }
    `;

    export const buildCrossSellQueryResolvers = () => {
    return {
    crossSell: async (
    _: any,
    args: { crossSellId: number },
    ctx: AppContext,
    ): Promise<CrossSell> => {
    const { crossSellId } = args;

    const crossSell = (await ctx.state.dataLoaders.crossSell
    .load(crossSellId)
    .catch(error => {
    logError(error, ERROR_MESSAGE.CROSS_SELL_FETCH_FAILED, ctx);
    ctx.throw(500, getStatusText(500));
    })) as CrossSell;

    return crossSell;
    },

    export const buildCrossSellResolvers = () => {
    return {
    CrossSell: {
    id: async (
    root: CrossSell,
    args: any,
    ctx: AppContext,
    ): Promise<number> => {
    return root.id;
    },

    shopId: async (
    root: CrossSell,
    args: any,
    ctx: AppContext,
    ): Promise<number> => {
    return root.shopId;
    },