MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
945 stars 278 forks source link

Question: Are GraphQL mutations supported? #338

Open chrisandrewca opened 4 years ago

chrisandrewca commented 4 years ago

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?

https://github.com/MONEI/Shopify-api-node/blob/fe7eee04ee6bf2cf5db57323dc389df9d2796b58/index.js#L225

mjsilva commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 1 year ago

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 commented 11 months ago

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 } } }; `