Solved

GQL Query won't use variables - React Remix

tajbowness
Shopify Partner
13 2 4

 

 

 

import { authenticate } from "../shopify.server";

export const fetchAllProducts = async (request) => {
    try {
        const { admin } = await authenticate.admin(request);

        // let hasNextPage = true;
        let afterCursor = null;
        let allProducts = [];

        for (let i = 0; i <= 7; i++) {
            const response = await admin.graphql(`#graphql
                query allProducts($first: Int!, $after: String) {
                    products(first: $first, after: $after, reverse: true) {
                        edges {
                            node {
                                id
                            }
                        }
                        pageInfo {
                            hasNextPage
                            startCursor
                            endCursor
                        }
                    }
                }
            `, {
                first: 10,
                after: afterCursor
            });

            const { data } = await response.json();

            const products = data.products.edges.map(edge => edge.node.id);
            allProducts = [...allProducts, ...products];

            // hasNextPage = data.products.pageInfo.hasNextPage;
            afterCursor = data.products.pageInfo.endCursor;
        }

        return allProducts;
    } catch (error) {
        console.error('Error fetching products:', error);
        return [];
    }
};

 

 

When making a GQL Query with variables, the variables won't be used. Have I done something wrong?

 

Mutations with variables haven't been a problem for me.


Server Logs:

Error fetching products: GraphqlQueryError: Variable $first of type Int! was provided invalid value

 

Accepted Solution (1)

tajbowness
Shopify Partner
13 2 4

This is an accepted solution.

Reply 1 (1)

tajbowness
Shopify Partner
13 2 4

This is an accepted solution.