Некоторые из запросов в пакете: Многочастные запросы для загрузки файла.
На данный момент моя структура данных следующая:
Файл конфигурации Json:
Код: Выделить всё
{
"operationName": "addPlaneRpgType",
"query": "mutation addPlaneRpgType($rpgId: UUID!) { createHasRpgType(hasRpgTypeInput: { rpgId: $rpgId rpgTypeName: \"PLANE\" }) {rpgType { name} }}",
"variables": []
},
{
"operationName": "create3D1File3D",
"query": "mutation create3D1File3D($file3d1: Upload!, $userVaultId: UUID!) { createFile3D(input: { file: $file3d1 vaultId: $userVaultId }) {id }}",
"variables": [
{
"name" : "file3d1",
"value" : "3d_sample_1.stl",
"type" : "File"
}
]
}
Код: Выделить всё
[JsonSerializable(typeof(GraphQLQueryData))]
internal class GraphQLQueryData
{
[JsonProperty("query")]
public string Query { get; set; }
[JsonProperty("operationName")]
public string OperationName { get; set; }
[JsonProperty("variables")]
public IEnumerable Variables { get; set; }
public GraphQLQueryData(string operationName, string query, List variables)
{
OperationName = operationName;
Query = query;
Variables = variables;
}
}
[JsonSerializable(typeof(GraphQLVariable))]
internal class GraphQLVariable
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
public GraphQLVariable(string variableName, string variableValue, string variableType)
{
Name = variableName;
Value = variableValue;
Type = variableType;
}
}
Код: Выделить всё
public async Task RunMultipartGraphQLQuery(EnvironmentEnum environment, List graphQLQueriesData)
{
using MultipartFormDataContent form = [];
Dictionary map = [];
List operations = [];
int variableNumber = 0;
foreach (GraphQLQueryData graphQLQueryData in graphQLQueriesData)
{
JObject queryParameterVariable = [];
foreach (GraphQLVariable graphQLVariable in graphQLQueryData.Variables)
{
if (graphQLVariable.Type != "File")
{
queryParameterVariable.Add(graphQLVariable.Name, graphQLVariable.Value);
continue;
}
map[variableNumber.ToString()] = [$"variables.{graphQLVariable.Name}"];
queryParameterVariable.Add(graphQLVariable.Name, null);
variableNumber++;
}
operations.Add(
new()
{
{ "operationName", graphQLQueryData.OperationName },
{ "query", graphQLQueryData.Query },
{ "variables", queryParameterVariable }
}
);
}
form.Headers.Add("GraphQL-Preflight", "1");
// Adding the operation property containing a list of all the queries and their variables operations : [ {query: "", variables : {"file1" : null}, etc...} ]
form.Add(new StringContent(JsonConvert.SerializeObject(operations), Encoding.UTF8, "application/json"), "operations");
// Adding the mapping between the variables and the file number map: {"0", ["variables.file1"], "1" : ["variables.file2"], etc...}
form.Add(new StringContent(JsonConvert.SerializeObject(map), Encoding.UTF8, "application/json"), "map");
// Adding each file to the multipartform matching it with with the variable number
variableNumber = 0;
foreach (GraphQLQueryData graphQLQueryData in graphQLQueriesData)
{
foreach (GraphQLVariable graphQLVariable in graphQLQueryData.Variables)
{
string filePath = Path.Combine("GraphqlSet", environment.ToDescriptionString(), "FileSample", graphQLVariable.Value);
StreamContent fileContent =
new(new FileStream(filePath, FileMode.Open, FileAccess.Read));
form.Add(fileContent, variableNumber.ToString(), graphQLVariable.Value);
variableNumber++;
}
}
await _client.PostAsync("/graphql", form);
}
Я совершенно запутался в поведении HotChocolate Batching и Multipart.
Надеюсь, вы мне поможете.
Спасибо заранее
Подробнее здесь: https://stackoverflow.com/questions/792 ... tchocolate
Мобильная версия