// 使用内置的fetch API (Node.js 18+)
|
|
const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql';
|
|
// 生成唯一的测试数据
|
const timestamp = Date.now();
|
const uniquePhone = `1380000${timestamp.toString().slice(-4)}`;
|
const uniqueAvatarMediaId = `avatar_${timestamp}`;
|
const uniqueAttachmentMediaIds = [`attachment_${timestamp}_1`, `attachment_${timestamp}_2`];
|
|
async function testMediaApiIntegration() {
|
console.log('开始测试媒体API集成...');
|
|
try {
|
// 1. 提交活动报名(包含avatarMediaId和attachmentMediaIds)
|
console.log('1. 提交活动报名...');
|
const registrationMutation = `
|
mutation {
|
submitActivityRegistration(input: {
|
activityId: 1,
|
playerInfo: {
|
name: "测试用户${timestamp}",
|
phone: "${uniquePhone}",
|
gender: 1,
|
birthDate: "1990-01-01",
|
avatarMediaId: "${uniqueAvatarMediaId}"
|
},
|
regionId: 1,
|
projectName: "测试项目",
|
description: "测试描述",
|
attachmentMediaIds: ${JSON.stringify(uniqueAttachmentMediaIds)}
|
}) {
|
success
|
message
|
registrationId
|
playerId
|
userId
|
}
|
}
|
`;
|
|
const registrationResponse = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: registrationMutation
|
})
|
});
|
|
const registrationResult = await registrationResponse.json();
|
console.log('报名结果:', JSON.stringify(registrationResult, null, 2));
|
|
if (registrationResult.errors) {
|
console.error('报名失败:', registrationResult.errors);
|
return;
|
}
|
|
const result = registrationResult.data.submitActivityRegistration;
|
if (!result.success) {
|
console.error('报名失败:', result.message);
|
return;
|
}
|
|
console.log(`报名成功! 报名ID: ${result.registrationId}, 选手ID: ${result.playerId}`);
|
|
// 2. 查询选手关联的媒体记录(头像)
|
console.log('\\n2. 查询选手头像媒体记录...');
|
const playerMediaQuery = `
|
query {
|
medias(targetType: 1, targetId: ${result.playerId}) {
|
id
|
name
|
path
|
fileSize
|
mediaType
|
targetType
|
targetId
|
}
|
}
|
`;
|
|
const playerMediaResponse = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: playerMediaQuery
|
})
|
});
|
|
const playerMediaResult = await playerMediaResponse.json();
|
console.log('选手媒体记录:', JSON.stringify(playerMediaResult, null, 2));
|
|
// 3. 查询活动报名关联的媒体记录(附件)
|
console.log('\\n3. 查询活动报名附件媒体记录...');
|
const attachmentMediaQuery = `
|
query {
|
medias(targetType: 2, targetId: ${result.registrationId}) {
|
id
|
name
|
path
|
fileSize
|
mediaType
|
targetType
|
targetId
|
}
|
}
|
`;
|
|
const attachmentMediaResponse = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: attachmentMediaQuery
|
})
|
});
|
|
const attachmentMediaResult = await attachmentMediaResponse.json();
|
console.log('附件媒体记录:', JSON.stringify(attachmentMediaResult, null, 2));
|
|
console.log('\\n测试完成!请检查后端日志确认媒体记录保存情况。');
|
|
} catch (error) {
|
console.error('测试过程中发生错误:', error);
|
}
|
}
|
|
testMediaApiIntegration();
|