如何使用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。请注意,您首先需要parsevalidate查询。

我建议看一下the source of graphql-express,以了解它的作用。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...