简体   繁体   中英

How to invoke a query using graphql-tools

I want to build a GraphQL API. However, I do not want to use express or connect, which cuts me off from using express-graphql . My schema is generated using the makeExecutableSchema method from graphql-tools . That means I need to somehow manually invoke the query on the schema.

Basically, I need something like this:

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));

Also, I need to show GraphiQL if the request comes from a browser.

Is something like this possible?

The magic function you're looking for is named execute . Notice that you first need to parse and validate the query.

I would suggest to take a look at the source of graphql-express to understand what it does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM