[英]How to invoke a query using graphql-tools
我想构建一个 GraphQL API。 但是,我不想使用 express 或 connect,这使我无法使用express-graphql
。 我的架构是使用graphql-tools
的makeExecutableSchema
方法生成的。 这意味着我需要以某种方式手动调用架构上的查询。
基本上,我需要这样的东西:
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
查询。
我建议看一下graphql-express
的来源以了解它的作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.