skip to Main Content

I’ve never used GraphQL before so I am really lacking knowledge on how to go about this. I’m wanting to update product meta fields on Shopify and it appears this is the only way. What I’ve done so far is really fumbling…

My JSON is:

{
  "input": {
    "id": "gid://shopify/Product/749521178847",
    "metafields": [
      {
        "id": "gid://shopify/Metafield/2223333",
        "value": "Training Grounds"
      }
    ]
  }
}

I’ve minified this to:

{"input":{"id":"gid://shopify/Product/749521178847","metafields":[{"id":"gid://shopify/Metafield/2223333","value":"The Training Grounds"}]}}

And am then using an HTTP request to:

https://MYSTORE.myshopify.com/api/2021-10/graphql.json?query={"input":{"id":"gid://shopify/Product/749521178847","metafields":[{"id":"gid://shopify/Metafield/2223333","value":"The Training Grounds"}]}}

I get the error:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

I don’t know if any of this is correct. If it is, I don’t know if ?query= is the right variable to pass it through on.

3

Answers


  1. I recommend you start using Postman, thunder client, or similar to write your graphql queries first, you will learn a lot about how graphql works and the error msgs will be a lot more useful.

    To easily connect with Shopify on this stage, go to a store and create a private app, now you can use this for authenticating your API calls.

    After that the Shopify graphql works on POST, you can’t write your request on GET mode.

    Login or Signup to reply.
  2. It needs to be a POST and you are missing type of operation mutation in this case and what it is.
    Postman has https://www.postman.com/lively-moon-541169/workspace/purego-apis/example/16545848-bf0d1589-09b1-4ec6-ba63-a65a56b500eb examples of how to do the calls which can help you.
    Also you can check GraphiQL app on shopify to test all the queries before making the programmatic queries

    Login or Signup to reply.
  3. Updating an existing metafield:

    mutation {
      metafieldsSet(metafields: [
        {namespace: "YOURNAMESPACE", ownerId: "gid://shopify/Customer/CUSTOMER_ID", type: "single_line_text_field", key: "YOURKEY", value: "THIS IS NEW VALUE"}
    
      ]) {
        metafields {
          key
          value
        }
        userErrors {
          field
          message
        }
      }
    }
    

    Creating new metafield:

    mutation {
      customerUpdate(input: {
        id: "gid://shopify/Customer/CUSTOMER_ID", 
        metafields: [
          {key: "newkey", value: "some value", type: "single_line_text_field", namespace: "some namespace"}, 
        ]
      }) {
        userErrors {
          field
          message
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search