Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

GQL Query won't use variables - React Remix

Solved

GQL Query won't use variables - React Remix

tajbowness
Shopify Partner
26 2 8

 

 

 

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
26 2 8

This is an accepted solution.

Reply 1 (1)

tajbowness
Shopify Partner
26 2 8

This is an accepted solution.