UpdateProductWithNewMedia It works in GraphQl Interface, but doesn't Work in c# routine

UpdateProductWithNewMedia It works in GraphQl Interface, but doesn't Work in c# routine

CarloB
Tourist
13 0 2

Hello

I've this mutation

 

mutation UpdateProductWithNewMedia($input: ProductInput!, $media: [CreateMediaInput!]) {
  productUpdate(input: $input, media: $media) {
    userErrors {
      field
      message
    }
  }
}

With this variables

 

{
  "input": {
    "id": "gid://shopify/Product/2022520586310",
    "descriptionHtml": "\n\nCaratteristiche Principali:\nNew 20% extra power**\n3x* Capture\n*Cattura 3 tipi di residui: polvere, peli/capelli e allergeni (provenienti da acari, ma anche da cani e gatti)\n**blocca il 20% in più di polvere rispetto allo Swiffer duster precedente\n\nPreparazione e Uso\nNon utilizzare su superfici calde.\n\nAvvertenze di sicurezza:\nTenere lontano dai bambini.\nA.I.S.E.\nwww.cleanright.eu\n\n\nAssistenza clienti:\n Indirizzo produttore : Procter & Gamble S.r.l.\nViale Giorgio Ribotta, 11\n00144 Roma\nRestituire a : www.swiffer.it\n800 420 430\nProcter & Gamble S.r.l.\nViale Giorgio Ribotta, 11\n00144 Roma\n",
    "metafields": [
      {
        "id": "gid://shopify/Metafield/36423331447107",
        "key": "updatedate",
        "namespace": "custom",
        "value": "2023-05-10",
        "type": "date"
      }
    ],
    "productType": "Piumino Spolvero",
    "title": "Swiffer Duster XXL Starter Kit Catturapolvere (1 Manico + 2 Piumini per spolverare)",
    "vendor": "Procter & Gamble srl",
    "variants": [
      {
        "barcode": "5410076291106",
        "sku": "10384",
        "weight": 167,
        "weightUnit": "GRAMS",
        "compareAtPrice": 0
      }
    ]
  },
  "media": [
    {
      "alt": "Swiffer Duster XXL Starter Kit Catturapolvere (1 Manico + 2 Piumini per spolverare)",
      "originalSource": "URL",
      "mediaContentType": "IMAGE"
    },
    {
      "alt": "Swiffer Duster XXL Starter Kit Catturapolvere (1 Manico + 2 Piumini per spolverare)",
      "originalSource": "URL",
      "mediaContentType": "IMAGE"
    }
  ]
}

Both the mutation and Variable are the Query and Variable Fields in a very simple c# routine

 

 

var graphQLClient = new GraphQLHttpClient(graphQLEndpoint, new NewtonsoftJsonSerializer());
graphQLClient.HttpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", accessToken);
var mutation = new GraphQLRequest();
mutation.Query = "THE QUERY ABOVE";
mutation.Variables= "THE VARIABLE ABOVE";
var response = graphQLClient.SendMutationAsync<dynamic>(mutation).Result;

If I execute the GraphQl mutation with the shopify interface using the exact value returned by debuging every field, it works, but If I execute the c# routine return me some errors:

Field 'productUpdate' doesn't accept argument 'media'

and

Variable $media is declared by UpdateProductWithNewMedia but not used

 

But I've take the mutation from shopify official documentation 

https://shopify.dev/docs/api/admin-graphql/2023-10/mutations/productUpdate#examples-Add_new_media_to...
It's make me crazy

 

 

Replies 5 (5)

CarloB
Tourist
13 0 2

Obviusly both query and variables are passed as OBJECT not as a simple screen. In the same way of the mutation "CreateProductWithNewMedia" that works well

SBD_
Shopify Staff
1831 273 418

Hey @CarloB 

 

I'm out of my depth with C# - will leave this for any other C# folks.

 

I quickly checked with GPT which said make sure the variables are an object (not a string) but you've noted you're already doing this. FWIW it provided some sample code:

// Define the input
var input = new {
    id = "gid://shopify/Product/2022520586310",
    descriptionHtml = "your description here",
    metafields = new [] {
        new {
            id = "gid://shopify/Metafield/36423331447107",
            key = "updatedate",
            namespace = "custom",
            value = "2023-05-10",
            type = "date"
        }
    },
    // Continue defining other parts of the input object
};

// Define media
var media = new[] {
    new { 
        alt = "Swiffer Duster XXL Starter Kit Catturapolvere (1 Manico + 2 Piumini per spolverare)",
        originalSource = "URL",
        mediaContentType = "IMAGE"
    },
    new {
        alt = "Swiffer Duster XXL Starter Kit Catturapolvere (1 Manico + 2 Piumini per spolverare)",
        originalSource = "URL",
        mediaContentType = "IMAGE"
    }
};

// Build the request
var mutation = new GraphQLRequest {
    Query = @"
    mutation UpdateProductWithNewMedia($input: ProductInput!, $media: [CreateMediaInput!]) {
        productUpdate(input: $input, media: $media) {
            userErrors {
                field
                message
            }
        }
    }",
    Variables = new { input, media }
};

var response = await graphQLClient.SendMutationAsync<dynamic>(mutation);


Let me know if you do find the issue.

Scott | Developer Advocate @ Shopify 

CarloB
Tourist
13 0 2

Hi SBD_
Thank you for your reply but your corde (very similar to mine) in response object return:

Variable $media is declared by UpdateProductWithNewMedia but not used

and 

Field 'productUpdate' doesn't accept argument 'media'


CarloB
Tourist
13 0 2

I'm STUPID!
the problem is in the api version!!!!


string graphQLEndpoint = "https://provabernava.myshopify.com/admin/api/2023-10/graphql.json";

 

SBD_
Shopify Staff
1831 273 418

Oh wow. Glad you found the issue!

Scott | Developer Advocate @ Shopify