Codex Assistant
昨天 58d9f460b2f8c34430285115e2557d18333c5cab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);