Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Are GraphQL mutations supported? #338

Open
chrisandrewca opened this issue Jan 24, 2020 · 7 comments
Open

Question: Are GraphQL mutations supported? #338

chrisandrewca opened this issue Jan 24, 2020 · 7 comments

Comments

@chrisandrewca
Copy link

chrisandrewca commented Jan 24, 2020

I was trying a simple mutation the other day to update a collections descriptionHtml. I could only get it to work with fetch. I noticed that body assumes query. Could that be the issue or is it normal to wrap query around mutation?

body: json ? JSON.stringify({ query: data, variables }) : data

@mjsilva
Copy link

mjsilva commented Feb 21, 2020

Hi @chrisandrewca

this is how I implemented mutations:

    const query = gql.mutation({
      operation: operation,
      variables: variables,
      fields: [
        {product: ['id']}, {userErrors: ['field', 'message']},
      ],
    })

    // shopifyAdapter?.query is shopify.graphql(query, variables)
    const promise = this.shopifyAdapter?.query(query.query, query.variables)
    const response = await promise

@TAnas0
Copy link

TAnas0 commented Apr 4, 2020

@mjsilva Thanks for the hack

Could we get a look into this from a maintainer please to support it directly?
I found myself in need of it to use the Shopify-bulk-opertions

@tomredman
Copy link

I've had luck with this:

const shopify = new Shopify({
    shopName: shop,
    accessToken: accessToken
  });

  const productMutation = `mutation productCreate($input: ProductInput!) {
    productCreate(input: $input) {
      product {
        id
      }
      userErrors {
        field
        message
      }
    }
  }`;

  const variables = {
    "input": {
      "title": params.title,
      "descriptionHtml": params.description,
      "variants": [{
        "price": params.price,
        "barcode": params.barcode,
        "requiresShipping": false,
        "taxable": true
      }],
      "published": true,
    }
  };

  try {
    let response = await shopify.graphql(productMutation, variables);
    return response;
  }
  catch(error) {
    console.log(error.message);
  }

@TAnas0
Copy link

TAnas0 commented May 13, 2020

Thanks @tomredman

Just noting that this doesn't work with the Shopify Bulk API. This is what I tried:

    const bulkImportMutation = `mutation {
      bulkOperationRunQuery(
        query: """
          {
            products {
              edges {
                node {
                  id
                  title
                }
              }
            }
          }
        """
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }`


    const shopify = new Shopify({
      shopName: storeName,
      accessToken
    })

    try {
      let response = await shopify.graphql(bulkImportMutation, {})
      console.log(response)
    } catch (e) {
      console.error(e)
    }

What I get is an error saying:

Error: Field 'bulkOperationRunQuery' doesn't exist on type 'Mutation'
    at /home/jon/Gitlab/Jaiqo/Loyaly/merge/node_modules/shopify-api-node/index.js:240:19
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async ShopifyStoreProvider.bulkImportStore (/home/jon/Gitlab/Jaiqo/Loyaly/merge/packages/api/dist/index.js:261:28)
    at async AsyncFunction.bulkImportStore (/home/jon/Gitlab/Jaiqo/Loyaly/merge/packages/api/dist/index.js:391:33)
    at async typeResolvers.<computed> (/home/jon/Gitlab/Jaiqo/Loyaly/merge/node_modules/@graphql-modules/core/dist/index.cjs.js:1148:46)
    at async middleware (/home/jon/Gitlab/Jaiqo/Loyaly/merge/packages/api/node_modules/graphql-shield/dist/generator.js:29:24) {
  locations: [ { line: 2, column: 7 } ],
  path: [ 'mutation', 'bulkOperationRunQuery' ],
  extensions: {
    code: 'undefinedField',
    typeName: 'Mutation',
    fieldName: 'bulkOperationRunQuery'
  },
  response: PassThrough {
.......
......

I tried the same mutation using the Shopify Graphiql app and it launches a job sucessfully.

I think I'll fall back to a simple POST request directly to the endpoint.

@jordanfinners
Copy link

I came across this, and there was a few things I found.
The library uses the oldest supported version of the API which caught me out, so I'd specify the version you want to use.
The variables need to have a quoted key, for example.

{
    "id": "an ID"
}

My mutations worked after that :)

@IonicaBizau
Copy link

I can confirm that setting the apiVersion to "2023-04" fixed the problem in my case too (Error: Field 'companyCreate' doesn't exist on type 'Mutation'). 🎉

@c0ndi
Copy link

c0ndi commented Sep 21, 2023

Screenshot 2023-09-21 at 15 08 15

Im having the same issue with productCreate mutation, using Next.js eCommerce template and trying to create product having error like this

`
const variables = {
input: {
title: "title",
descriptionHtml: "title",
published: true,
}
};

const query = mutation productCreate($input: ProductInput!) { productCreate(input: $input) { product { id } userErrors { field message } } };
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants