javascriptnode.jsapigraphqlgraphql-tools

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?


Solution

  • 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.