如何解决如何使用graphql-tools调用查询
我想构建一个GraphQL API。但是,我不想使用express或connect,这使我无法使用express-graphql
。我的架构是使用makeExecutableSchema
中的graphql-tools
方法生成的。这意味着我需要以某种方式手动调用模式上的查询。
基本上,我需要这样的东西:
const { makeExecutableSchema,magicFunction /*This should invoke the query*/ } = require('graphql-tools');
const typeDefs = `
type Query {
hello: String
}
`;
const resolvers = {
Query: () => 'hello world!'
}
const schema = makeExecutableSchema({
typeDefs,resolvers
});
// Run the query:
const query = `{
hello
}`;
console.log('returned:',magicFunction(query,schema));
此外,如果请求来自浏览器,我需要显示GraphiQL。
这样可能吗?
解决方法
您要查找的魔术函数名为execute
。请注意,您首先需要parse
和validate
查询。
我建议看一下the source of graphql-express
,以了解它的作用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。