如何从Node.js/Express服务器调用GraphQL API? [英] How to call GraphQL API from Node.js/Express server?
本文介绍了如何从Node.js/Express服务器调用GraphQL API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最近为我的Express服务器实现了一个架构和一些解析器.我通过/graphql
成功地测试了它们,现在我想调用从REST API访问时实现的查询,如下所示:
I recently implemented a schema and some resolvers for my Express server. I tested them successfully through /graphql
and now I would like to call the queries I implemented when accessing from a REST API, like so:
//[...]
//schema and root correctly implemented and working
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
//I start the server
app.listen(port, () => {
console.log('We are live on ' + port);
});
//one of many GET handlers
app.get("/mdc/all/:param", function(req, res) {
//call one of the (parametrized) queries here
//respond with the JSON result
});
如何在GET处理程序中调用用GraphQL定义的查询?如何将参数传递给他们?
How can I call the queries I defined with GraphQL inside my GET handlers? How do I pass parameters to them?
谢谢!
推荐答案
我使用Apollo,并通过POST从前端直接在/graphql
上执行查询.
I used Apollo and performed queries directly on /graphql
through POST from the front-end.
这篇关于如何从Node.js/Express服务器调用GraphQL API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文