[英]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.