feat: adopt createMessage upload api (#6596)

This commit is contained in:
pengx17
2024-04-18 06:55:29 +00:00
parent 10653eccbc
commit 5fc56a20ac
6 changed files with 63 additions and 46 deletions

View File

@@ -23,7 +23,7 @@ function getExportedName(def) {
* @type {import('@graphql-codegen/plugin-helpers').CodegenPlugin}
*/
module.exports = {
plugin: (_schema, documents, { output }) => {
plugin: (schema, documents, { output }) => {
const nameLocationMap = new Map();
const locationSourceMap = new Map(
documents
@@ -133,12 +133,24 @@ module.exports = {
const { variableDefinitions } = def;
if (variableDefinitions) {
return variableDefinitions.some(variableDefinition => {
if (
variableDefinition?.type?.type?.name?.value === 'Upload'
) {
return true;
}
return false;
const varType = variableDefinition?.type?.type?.name?.value;
const checkContainFile = type => {
if (schema.getType(type)?.name === 'Upload') return true;
const typeDef = schema.getType(type);
const fields = typeDef.getFields?.();
if (!fields || !fields) return false;
for (let field of Object.values(fields)) {
let type = field.type;
while (type.ofType) {
type = type.ofType;
}
if (type.name === 'Upload') {
return true;
}
}
return false;
};
return varType ? checkContainFile(varType) : false;
});
} else {
return false;

View File

@@ -115,17 +115,26 @@ export function transformToForm(body: RequestBody) {
if (body.operationName) {
gqlBody.name = body.operationName;
}
const map: Record<string, [string]> = {};
const map: Record<string, string[]> = {};
const files: File[] = [];
if (body.variables) {
let i = 0;
Object.entries(body.variables).forEach(([key, value]) => {
const buildMap = (key: string, value: any) => {
if (value instanceof File) {
map['0'] = [`variables.${key}`];
map['' + i] = [key];
files[i] = value;
i++;
} else if (Array.isArray(value)) {
value.forEach((v, index) => {
buildMap(`${key}.${index}`, v);
});
} else if (isObject(value)) {
Object.entries(value).forEach(([k, v]) => {
buildMap(`${key}.${k}`, v);
});
}
});
};
buildMap('variables', body.variables);
}
form.set('operations', JSON.stringify(gqlBody));

View File

@@ -109,7 +109,7 @@ export const createCopilotMessageMutation = {
id: 'createCopilotMessageMutation' as const,
operationName: 'createCopilotMessage',
definitionName: 'createCopilotMessage',
containsFile: false,
containsFile: true,
query: `
mutation createCopilotMessage($options: CreateChatMessageInput!) {
createCopilotMessage(options: $options)