Ad

GraphQL: Is It Possible To Search Nested Field?

- 1 answer

I am specifically using the shopify graphql admin api to query orders.

I want to do a search for a nested related field.

Below is my query.

export const orderHistoryQuery = gql`
  query Order($productsFirst: Int!, $productsAfter: String, $filterQuery: String) {
    orders(first: $productsFirst, after: $productsAfter, reverse: true, query:$filterQuery) {
      edges {
        cursor
        node {
          id
          name
          customer {
            id
            metafields(first: 10) {
              edges {
                node {
                  id
                  key
                  value
                  namespace
                }
                cursor
              }
            }
          }
          totalPriceSet {
            shopMoney {
              amount
              currencyCode
            }
          }
          subtotalPriceSet {
            shopMoney {
              amount
              currencyCode
            }
          }
          totalRefundedSet {
            shopMoney {
              amount
              currencyCode
            }
          }
          currencyCode
          email
          phone
          processedAt
          totalShippingPriceSet {
            shopMoney {
              amount
              currencyCode
            }
          }
          totalTaxSet {
            shopMoney {
              amount
              currencyCode
            }
          }
          shippingAddress {
            firstName
            lastName
            address1
            address2
            city
            province
            zip
            country
          }
          billingAddress {
            firstName
            lastName
            address1
            address2
            city
            province
            zip
            country
          }
          customAttributes {
            key
            value
          }
        }
      }
    }
  }
`;

I want to query metafields or ANYTHING really but it doesn't seem like it's supported. I am not sure if I just have the wrong query syntax or if it's not supported. The shopify search syntax documenation doesn't really help and this is where my knowledge of graphql falls apart.

Is it possible to do this in graphql? I also tried adding metafields(id: $whateverID) which is not supported by their setup.

Ad

Answer

Unfortunately, Shopify doesn't support query filters on metafields. The best way to figure this out is by using a graphql explorer like GraphiQL. Shopify dashboard has this built in if you go to Apps > Shopify GraphiQL App.

Using GraphiQL you can see that:

  1. Customers query doesn't have metafields supported: customers query
  2. Orders query doesn't have customers or metafields supported: orders query
  3. And metafields on customers doesn't have a query param: metafields params

I think your options are to either query by what you can and filter after you get the results or use a customer tag and query by tag.

Ad
source: stackoverflow.com
Ad