使用备用 GraphQL 客户端连接到 Apollo Server



是否可以使用非Apollo客户端(如graphql(连接到Apollo GraphQL服务器.js - https://github.com/f/graphql.js?

如果是这样,应该使用什么端点?还是有另一种方法?

此操作失败,并显示HTTP 500服务器错误:

const graph = graphql('http://localhost:3013/graphql', {
method: 'POST' // POST by default.
});
const res = graph(`query getQuestions {
questions {
id,
question
}
}
`);
res().
then((result) => console.log(result))
.catch((err) => console.log(err));

当然,你可以使用任何 GraphQL 客户端,只要客户端遵循 GraphQL 规范。

例如

server.ts

import { ApolloServer, gql } from 'apollo-server';
import graphql from 'graphql.js';
const typeDefs = gql`
type Query {
_: String
}
`;
const resolvers = {
Query: {
_: () => 'Hello',
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(async ({ url }) => {
console.log(`Apollo server is listening on ${url}graphql`);
const graph = graphql(`${url}graphql`, { asJSON: true });
const helloQuery = graph(`
query {
_
}
`);
const actual = await helloQuery();
console.log('actual: ', actual);
server.stop();
});

输出:

Apollo server is listening on http://localhost:4000/graphql
actual:  { _: 'Hello' }

最新更新