是否可以使用诸如graphql.js-https://github.com/f/graphql.js之类的非Apollo客户端连接到Apollo GraphQL服务器?
如果是这样,应该使用哪个端点?还是有另一种方法?
此操作失败,并显示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));
答案 0 :(得分:0)
当然,您可以使用任何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' }