import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
const client = new ApolloClient({
uri: '',
cache: new InMemoryCache(),
});
client
.query({
query: gql`
query {
hello
}
`,
})
.then((result) => console.log(result));
其中,
为后端 GraphQL 服务的 URL 地址。
2. 确认网络连接是否畅通,可以尝试使用 curl 或者浏览器等工具来访问后端 GraphQL 服务,并在控制台查看 HTTP 状态码和错误信息。
3. 如果是 HTTPS 请求,则需要确保正确的证书已经安装和配置。可以使用 https 模块来发送 HTTPS 请求:
const https = require('https');
https.get('', (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
}).on('error', (e) => {
console.error(e);
});
其中,
为后端 GraphQL 服务的 HTTPS URL 地址。
4. 如果后端服务无法通过 HTTPS 访问,则需要更改 Apollo Server 的配置,将 secure
参数设置为 false
,以允许使用 HTTP 协议访问服务:
const server = new ApolloServer({
typeDefs,
resolvers,
tracing: true,
playground: true,
introspection: true,
secure: false, // 此处需要注意!
});
server.listen().then(({ url }) => {
console.log(`� Server ready at ${url}`);
});