const axios = require('axios');
|
|
// 模拟小程序端的GraphQL请求
|
async function testMiniprogramGraphQL() {
|
console.log('=== 测试小程序端GraphQL访问 ===\n');
|
|
const baseUrl = 'http://localhost:8080/api/graphql';
|
|
// 测试1: 不需要认证的公开查询
|
console.log('1. 测试公开查询 (appConfig)');
|
try {
|
const response = await axios.post(baseUrl, {
|
query: `
|
query {
|
appConfig {
|
name
|
version
|
description
|
}
|
}
|
`
|
}, {
|
headers: {
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('✅ 公开查询成功');
|
console.log('响应数据:', JSON.stringify(response.data, null, 2));
|
} catch (error) {
|
console.log('❌ 公开查询失败');
|
console.log('状态码:', error.response?.status);
|
console.log('错误信息:', error.response?.data || error.message);
|
}
|
|
console.log('\n' + '='.repeat(50) + '\n');
|
|
// 测试2: 需要认证的查询(不提供token)
|
console.log('2. 测试需要认证的查询 (userProfile) - 无token');
|
try {
|
const response = await axios.post(baseUrl, {
|
query: `
|
query {
|
userProfile {
|
id
|
name
|
userType
|
}
|
}
|
`
|
}, {
|
headers: {
|
'Content-Type': 'application/json'
|
}
|
});
|
|
console.log('✅ 查询成功');
|
console.log('响应数据:', JSON.stringify(response.data, null, 2));
|
} catch (error) {
|
console.log('❌ 查询失败');
|
console.log('状态码:', error.response?.status);
|
console.log('错误信息:', error.response?.data || error.message);
|
}
|
|
console.log('\n' + '='.repeat(50) + '\n');
|
|
// 测试3: 使用无效token
|
console.log('3. 测试需要认证的查询 (userProfile) - 无效token');
|
try {
|
const response = await axios.post(baseUrl, {
|
query: `
|
query {
|
userProfile {
|
id
|
name
|
userType
|
}
|
}
|
`
|
}, {
|
headers: {
|
'Content-Type': 'application/json',
|
'Authorization': 'Bearer invalid-token'
|
}
|
});
|
|
console.log('✅ 查询成功');
|
console.log('响应数据:', JSON.stringify(response.data, null, 2));
|
} catch (error) {
|
console.log('❌ 查询失败');
|
console.log('状态码:', error.response?.status);
|
console.log('错误信息:', error.response?.data || error.message);
|
}
|
|
console.log('\n=== 测试完成 ===');
|
}
|
|
testMiniprogramGraphQL().catch(console.error);
|