post.js
const { gql } = require('apollo-server-express')
const Posts = gql`
type Query {
totalPosts: Int!
}
`;
module.exports = {
Posts,
}
server.js
const { ApolloServer } = require('apollo-server-express');
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core');
const express = require('express');
const http = require('http');
const {mergeTypeDefs, mergeResolvers} = require('@graphql-tools/merge');
const {loadFilesSync} = require("@graphql-tools/load-files");
const path = require('path');
const {makeExecutableSchema} = require('@graphql-tools/schema');
const typeDefs = mergeTypeDefs(loadFilesSync(path.join(__dirname, "./graphql/typeDefs")));
const resolvers = mergeResolvers(loadFilesSync(path.join(__dirname, "./graphql/resolvers")));
const schema = makeExecutableSchema({ typeDefs, resolvers});
async function startApolloServer(typeDefs, resolvers) {
const app = express();
const httpServer = http.createServer(app);
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await server.start();
server.applyMiddleware({ app });
await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve));
console.log(` Server ready at http://localhost:4000${server.graphqlPath}`);
}
startApolloServer(schema);
I've tried:
- reading thoroughly through DOCS and SPECS everything from Apollo to Graphql-tools (which is where I started).
- without the 'const' declaration, directly exported in module.exports (assuming it might not be parsed properly as a variable)
- adding a root query as mentioned in answers to other similar questions
- many other things...
I'm sure this is an easy fix. The problems with ambiguous error messages seem to always be like that.
Otherwise, ask for what you need. I will answer.