-
Hello, is there a way I can create an operation and send it manually via reqwest as a multipart request? I have a mutation that uses graphql multipart request spec for file uploads, and I don't quite understand how I could do that. The field that requires it in my mutation: https://docs.saleor.io/docs/3.x/api-reference/miscellaneous/scalars/upload let category_create_operation: cynic::Operation<
gql_queries::CreateCategory,
gql_queries::CreateCategoryVariables,
> = gql_queries::CreateCategory::build(gql_queries::CreateCategoryVariables {
input: gql_queries::CategoryInput {
description: category_description,
name: Some(category_cp.name.as_str()),
slug: Some(category_cp.slug.as_str()),
seo: None,
background_image: None, //This is the Upload field
background_image_alt: Some(category_cp.name.as_str()),
metadata: Some(vec![MetadataInput {
key: "old_id",
value: &category_old_id,
}]),
private_metadata: None,
},
parent_id: category_parent_id,
});
let create_cat_response = client
.post(GQL_Endpoint)
.header("Authorization", &jwt)
.run_graphql(category_create_operation)
.await
.expect("Failed creating category"); I also looked around for how to submit multipart mutations in general, but it's just not clicking in my head. Any tips appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
This isn't something the built in HTTP client integrations support, but that shouldn't prevent you from implementing it yourself.
I've never specifically done a multipart GraphQL request myself, but from skimming that article you linked I think you can probably create the GraphQL part with something like
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
for future readers, the result can then be parsed from the response text as such: let result: GraphQlResponse<MyMutation> = serde_json::from_str(&req.text().await?)?; |
Beta Was this translation helpful? Give feedback.
That error looks a lot like a JSON decoding error.
If you look at the docs for CategoryInput
description
is a JSONString, which I assume should be a string containing JSON. You can see from your request dump thatdescription
is being set to"lol"
which isn't valid JSON.You probably want to make it valid JSON, I think that will fix your issue.
I see you're doing this:
So I assume you've defined something like
struct Jsonstring(String)
. You could either useserde_json::to_string
to populate that string with JSON, or hardcode the JSON by hand.A slightly better (but definitely more effort) solution…