Shopify / shopify-app-template-node

MIT License
867 stars 391 forks source link

GraphQL api query by tag not working #1248

Open masterBrog opened 1 year ago

masterBrog commented 1 year ago

Issue summary

When i make a GraphQL query to retrieve orders with a query filter on tag, it returns no results. Using the same query in the Shopify GraphQL app in the store returns results. Removing the query:"tag:Ship" from query results in orders being returned.

My code was adapted from the QR Code example app - https://shopify.dev/docs/apps/getting-started/build-app-example My code adds a new endpoint middleware to express and makes a simple graphql query -

import express from "express";
import shopify from "../shopify.js";

const ORDERS_QUERY = `
query {
  orders(first:100, query:"tag:Ship") {
    edges {
      node {
        id
        name
        tags
      }
    }
  }
}
  `;

export default function applyOrdersApiEndpoints(app) {
  app.use(express.json());

  app.get("/api/weekorders", async (req, res) => {

    const client = new shopify.api.clients.Graphql({
      session: res.locals.shopify.session,
    });

    const orders = await client.query({
      data: {
        query: ORDERS_QUERY
      },
    });

    res.send(orders.body.data);
  });
}

app setup in shopify.js (unchanged from QR Code example app):

import { LATEST_API_VERSION } from "@shopify/shopify-api";
import { shopifyApp } from "@shopify/shopify-app-express";
import { LogSeverity } from "@shopify/shopify-api"
import { SQLiteSessionStorage } from "@shopify/shopify-app-session-storage-sqlite";
import { restResources } from "@shopify/shopify-api/rest/admin/2023-01";
import sqlite3 from "sqlite3";
import { join } from "path";
import { QRCodesDB } from "./qr-codes-db.js";

const database = new sqlite3.Database(join(process.cwd(), "database.sqlite"));
// Initialize SQLite DB
QRCodesDB.db = database;
QRCodesDB.init();
const shopify = shopifyApp({
  api: {
    apiVersion: LATEST_API_VERSION,
    restResources,
    billing: undefined, // or replace with billingConfig above to enable example billing
    logger: {
      level: LogSeverity.Debug,
      httpRequests: true, // if the error seems to be related to requests
    }
  },
  auth: {
    path: "/api/auth",
    callbackPath: "/api/auth/callback",
  },
  webhooks: {
    path: "/api/webhooks",
  },
  sessionStorage: new SQLiteSessionStorage(database),
});

export default shopify;
023-03-17 23:28:31 | backend  | [shopify-api/INFO] version 6.2.0, environment Node v18.15.0
2023-03-17 23:28:48 | backend  | [shopify-app/INFO] Running ensureInstalledOnShop
2023-03-17 23:28:48 | backend  | [shopify-app/DEBUG] Checking if shop has installed the app | {shop: anna-cake-couture.myshopify.com}
2023-03-17 23:28:48 | backend  | [shopify-app/INFO] App is installed and ready to load | {shop: anna-cake-couture.myshopify.com}
2023-03-17 23:28:52 | backend  | [shopify-app/INFO] Running validateAuthenticatedSession
2023-03-17 23:28:52 | backend  | [shopify-api/DEBUG] App is embedded, looking for session id in JWT payload | {isOnline: false}
2023-03-17 23:28:52 | backend  | [shopify-api/DEBUG] Found valid JWT payload | {shop: anna-cake-couture.myshopify.com, isOnline: false}
2023-03-17 23:28:52 | backend  | [shopify-app/DEBUG] Request session found and loaded | {shop: anna-cake-couture.myshopify.com}
2023-03-17 23:28:52 | backend  | [shopify-app/DEBUG] Request session exists and is active | {shop: anna-cake-couture.myshopify.com}
2023-03-17 23:28:52 | backend  | [shopify-api/DEBUG] Making HTTP request  -  POST https://anna-cake-couture.myshopify.com/admin/api/2023-01/graphql.json  -  Headers:      
                                 {"X-Shopify-Access-Token":["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"],"User-Agent":["Shopify Express Library v1.2.2 | Shopify API Library v6.2.0 | Node 
                                 v18.15.0"],"Content-Type":["application/graphql"],"Content-Length":["26"]}  -  Body: "\n{\n  shop {\n    name\n  }\n}"
2023-03-17 23:28:53 | backend  | [shopify-api/DEBUG] Completed HTTP request, received 200 OK
2023-03-17 23:28:53 | backend  | [shopify-app/INFO] Request session has a valid access token | {shop: anna-cake-couture.myshopify.com}
2023-03-17 23:28:53 | backend  | [shopify-api/DEBUG] Making HTTP request  -  POST https://anna-cake-couture.myshopify.com/admin/api/2023-01/graphql.json  -  Headers:      
                                 {"X-Shopify-Access-Token":["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"],"User-Agent":["Shopify Express Library v1.2.2 | Shopify API Library v6.2.0 | Node 
                                 v18.15.0"],"Content-Type":["application/json"],"Content-Length":["159"]}  -  Body: "{\"query\":\"\\nquery {\\n  orders(first:100, query:\\\"tag:Ship\\\") {\\n    edges 
                                 {\\n      node {\\n        id\\n        name\\n        tags\\n      }\\n    }\\n  }\\n}\\n  \"}"
2023-03-17 23:28:53 | backend  | [shopify-api/DEBUG] Completed HTTP request, received 200 OK

Expected behavior

The GraphQL query should return orders tagged with 'Ship'

Actual behavior

No results are returned

Steps to reproduce the problem

Run the query above using the GraphQL client as shown above