0
votes

I am using node.js in my application, with shopify-api-node (v3.2.0), to authenticate customer login along with other features if shopify. As per shopify documentation (https://shopify.dev/docs/storefront-api/reference/mutation/customeraccesstokencreate) I am using GraphQL to access shopify API. My code looks something like this below :-

const Shopify = require('shopify-api-node');

const shopify = new Shopify({
  shopName: process.env.SHOPIFY_DOMAIN_NAME,
  apiKey: process.env.SHOPIFY_API_KEY,
  password: process.env.SHOPIFY_API_KEY_PASSWORD
});

  const query = `mutation {
    customerAccessTokenCreate (input: {
      email: "[email protected]",
      password: "password123"
     }
    ) 
    {
      customerAccessToken {
        accessToken
        expiresAt
      }
      customerUserErrors {
        code
        field
        message
      }
    }
  }`;

  shopify
    .graphql(query)
    .then((output) => {
      console.log(output);
    })
    .catch((err) => {
      console.error(err)
    });

After this I am getting below error :-

Error: Field 'customerAccessTokenCreate' doesn't exist on type 'Mutation'
    at got.then (/Users/admin/Documents/Code/shopify-node-app/node_modules/shopify-api-node/index.js:239:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  locations: [ { line: 2, column: 5 } ],
  path: [ 'mutation', 'customerAccessTokenCreate' ],
  extensions:
   { code: 'undefinedField',
     typeName: 'Mutation',
     fieldName: 'customerAccessTokenCreate' }

Even I am getting the same thing from postman itself.

Any help would be appreciated.

1

1 Answers

0
votes

There are two types of GraphQL:

While they seems similar the strorefront is much more limited but can be used on the front-end, while the admin one is more rich in method and functionality but can't be used safely on the font-end.

The documentation and the method you are trying to make is referring to the Storefront API, but the package you are using is for the Admin GraphQL API.

You can create a storefront access token via the storefrontAccessToken method if you want to make storefront request but the Admin API GraphQL allows for more customization.

So you need to make sure you are using the proper API.

If you plan to use the storefront API, you shouldn't use NodeJS and just create a private app ( from Admin -> APP -> Private App) which will provide you a Store Front Access Token (if you enable it at the bottom and select the proper scopes) that can be used directly on the front-end.

If you plan to use the Admin API, you will need to create a public app and host it, then you can use NodeJS and pass the information via a Proxy in Shopify.

Summary

You are making a request to the Storefront API, while using a library for the Admin API.