Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Create New Customer With Metafields Using the Storefront API With Graphql

Create New Customer With Metafields Using the Storefront API With Graphql

Mark63
Visitor
3 0 0

Hi guys. Is there anybody here who could show me or direct me to a resource that shows examples on how to create a new customer using the Storefront API with Graphql? Basically, I just need to follow this example from the documentation but attach metafields input to the Graphql mutation below:

mutation {
  customerCreate(input: {
    email: "user@example",
    password: "2r7yS#afhi2$@Fd7f24fjkdfs"
  }) {
    userErrors {
      field
      message
    }
    customer {
      id
    }
  }
}

I tried the following but it producted an error in the result.

mutation {
  customerCreate(input: {
    email: "d8431358@nwytg.net",
    password: "2r7yS#afhi2$@Fd7f24fjkdfs"
    metafields: [
      {
        key: "testfield"
        value: "newvalue"
        value_type: "string"
        namespace: "global"
      }
    ]    
  }) {
    userErrors {
      field
      message
    }
    customer {
      id
    }
  }
}

 

Replies 6 (6)

Agile_Media_Ven
Shopify Partner
20 0 13

Mark -

The following GraphQL mutation works for me. It creates a new Customer with two metafields.

I don't believe password is a valid field for the Customer object.

Hope this helps,
James Sun
Agile Media Ventures, LLC

mutation {
  customerCreate(input: {
    email: "user@example.com",
    metafields: [{
      namespace: "global",
      key: "key1",
      value: "value1",
      valueType: STRING
    }, {
      namespace: "global",
      key: "key2",
      value: "value2",
      valueType: STRING
    }]
  }) {
    userErrors {
      field
      message
    }
    customer {
      id
    }
  }
}

 

Mark63
Visitor
3 0 0

Hi James,

Thank you for taking time to reply to my question. Yes, password is a valid field for the CustomerCreateInput (see attached image). 

The solution you provided just adds additional customer input but it's basically the same with what I used above. As expected, it's not working. See the attached image for that Graphql request results.

I hope somebody could shed some light on this.

Mark Vincent Verallo
Web Developer/Programmer

Agile_Media_Ven
Shopify Partner
20 0 13

Mark -

That's interesting. I'm using the Altair GraphQL client pointing to a test store I created yesterday, and password is not a valid field for CustomerInput!. See attached.

James

Mark63
Visitor
3 0 0

James-

With this client https://electronjs.org/apps/graphiql I tried creating a customer account and it was successful with password input.

By the way, I'm using the Storefront API:
https://help.shopify.com/en/api/custom-storefronts/storefront-api/reference/mutation/customercreate

And not the GraphQl Admin API:https://help.shopify.com/en/api/graphql-admin-api/reference/mutation/customercreate#input-fields

Mark

 

ggsandro
Shopify Partner
3 0 0

Hi Mark,

 

Have you solved your issue? How? I have the same at the moment, for some reason, Shopify returns me "InputObject 'CustomerCreateInput' doesn't accept argument 'metafields'". But as I see from your example it should be accessible, right?

 

 

 

KarlOffenberger
Shopify Partner
1873 184 903

@ggsandro GraphQL is wonderful in the way that it provides you an up to date schema. The schema is your truth. Forum posts can be and mostly are outdated. APIs and their fields, request inputs, responses etc. evolve over time. Even the documentation can fall behind. In this case, the documentation clearly does not document any metafield field when creating a customer via customerCreate mutation and the schema confirms much the same.

 

I suggest getting proper GraphQL tooling with schema introspection and auto-complete such as in Insomnia or GraphQL Playground - both tools sync the schema and you always know where you're at and what you're working with.

 

Best wishes!