簡體   English   中英

如何使用 graphql-tools 調用查詢

[英]How to invoke a query using graphql-tools

我想構建一個 GraphQL API。 但是,我不想使用 express 或 connect,這使我無法使用express-graphql 我的架構是使用graphql-toolsmakeExecutableSchema方法生成的。 這意味着我需要以某種方式手動調用架構上的查詢。

基本上,我需要這樣的東西:

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查詢。

我建議看一下graphql-express的來源以了解它的作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM