Query customer by id with js-buy-sdk

Solved
Highlighted
New Member
2 0 0

Hi,

I am trying to use the js-buy-sdk to query the orders for a customer. I have tried the following

import UnoptimizedClient from "shopify-buy/index.unoptimized.umd";

const unoptimizedClient = UnoptimizedClient.buildClient({
  domain: 'my-domain',
  storefrontAccessToken: 'myaccesstoken',
});

const productsQuery = unoptimizedClient.graphQLClient.query((root) => {
  root.addConnection('customer', {args: {id: 'gid:\\/\\/shopify\\/Customer\\/2226381979747'}}, (customer) => {
    customer.add('id');
    customer.add('displayName');
    customer.addConnection('orders', { args: { first: 5 } }, (order) => {
      order.add('id');
      order.add('name');
    });
  });
});

unoptimizedClient.graphQLClient.send(productsQuery).then(({model, data}) => {
  console.log('orders', model, data);
});


This results in the error: Uncaught (in promise) Error: No field of name "pageInfo" found on type "Customer" in schema

The following graphQL query is working for me in Insomnia

query {
  customer(id: "gid:\/\/shopify\/Customer\/2226381979747") {
    id,
    displayName,
    orders (first: 5) {
      edges {
        node {
          id,
          name
        }
      }
    }
  }
}


I tried removing the orders part of the query but I get the same error. Anyone know what I'm doing wrong?

 

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1040 139 163

This is an accepted solution.

Hey @thomaswong,

 

To access orders via the Storefront API, you'll first need to create a customer access token:

mutation customerAccessTokenCreate($input: CustomerAccessTokenCreateInput!) {
  customerAccessTokenCreate(input: $input) {
    customerAccessToken {
      accessToken
      expiresAt
    }
    customerUserErrors {
      code
      field
      message
    }
  }
}

{
  "input": {
    "email": "...",
    "password": "..."
  }
}


Then, once you have the token:

{
  customer (customerAccessToken: "..."){
    orders (first:5) {
      edges {
        node {
          id
        }
      }
    }
  }
}

The following graphQL query is working for me in Insomnia

Could you be hitting the Admin GraphQL API (/admin/api/2020-01/graphql.json) in Insomnia, instead of the Storefront API (/api/graphql.json)?


0 Likes
Highlighted
New Member
2 0 0

Ah yes I was hitting "/admin/api/graphql.json" from Insomnia. I think we ended up just pulling this feature out of the app but if it comes up again I'll try your solution. thanks! If this really is the issue though it would be great if the js-buy-sdk gave a relevant error message.

0 Likes