Search code examples
javascriptreact-nativeshopifyshopify-appshopify-api

Shopify - Facing issue when creating customer using shopify api


im working on shopify project to develop the android app. Currently im facing issue when creating customer. im tried to solve it if someone know about this please help me out!

Create customer mutation query

const query= `mutation customerCreate($input: CustomerCreateInput!) {
      customerCreate(input: $input) {
        customer {
          acceptsMarketing
          email
          firstName
          lastName
          password
          phone
        }
        customerUserErrors {
          field
          message
          code
        }
        variables: {
          "input": {
            "acceptsMarketing": true,
            "email": "[email protected]",
            "firstName": "John",
            "lastName": "Smith",
            "password": "5hopify",
            "phone": "111111111111"
          }
        },
      }
    }`;

async function apiCall(query) {
return fetch('https://storename.myshopify.com/api/graphql.json', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/graphql',
    'Access-Control-Origin': '*',
    'X-Shopify-Storefront-Access-Token': token,
  },
  body: query,
})
  .then(response => response.json())
  .then(response => console.log('response: ', response))
  .catch(error => console.log('error: ', error.message));

}

facing the below error

{"errors": [{"locations": [Array], "message": "Parse error on \"{\" (LCURLY) at [16, 20]"}]}

Solution

  • Use the following query. its worked for me

    mutation  {
      customerCreate(
        input: {
          firstName: "dave",
          lastName: "smith",
          email: "[email protected]",
          password: "12345"
          acceptsMarketing: true,
          
            }
      ) {
        customer {
          id
          firstName
          lastName
          email
        }
        userErrors {
          field
          message
        }
        customer {
          id
          
        }
      }
    }