问题描述
我想构建一个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
,以了解它的作用。