0

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?

Jake
  • 2,090
  • 2
  • 11
  • 24
  • "*I do not want to use express or connect*" - what else are you using? – Bergi Sep 06 '20 at 16:46
  • @Bergi I'm building my own framework, [eon.js](https://github.com/eon-web/eon) and I want to build grahpql support into it. Under the hood, eon uses the core http module. – Jake Sep 06 '20 at 16:48

1 Answers1

0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • If I see that correctly, that function is from graphql-js... will it work with schemas generated by graphql-tools? – Jake Sep 06 '20 at 16:38
  • Is there a way to figure display graphql when possible too? – Jake Sep 06 '20 at 16:42
  • @Jake Yes. Unfortunately I couldn't find it explicitly stated anywhere, but `graphql-js` is the core library that all (most?) of the JS graphql implementations depend on - `graphql-tools` is just providing helper functions for manipulating the data structures of `graphql-js`, and [depends on it](https://github.com/ardatan/graphql-tools/blob/4b606273f53f76029f09e31eb9468c548ca9c647/package.json#L39). – Bergi Sep 06 '20 at 16:45
  • from looking at the `express-graphql` source you sent me, I'm seeing that there's a lot of additional code involved with handling graphql queries... Is there no more straight-forward way to do this? – Jake Sep 06 '20 at 17:01
  • @Jake Most of these are configurable noops used as hooks by express-graphql. If you drop all the stuff about options, prettyprinting, formatting, graphiql, and extensions, there's not much that is left. – Bergi Sep 06 '20 at 17:04