1

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

File Structure Tree

I've tried:

  1. reading thoroughly through DOCS and SPECS everything from Apollo to Graphql-tools (which is where I started).
  2. without the 'const' declaration, directly exported in module.exports (assuming it might not be parsed properly as a variable)
  3. adding a root query as mentioned in answers to other similar questions
  4. 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.

1 Answers1

0

Removing the braces inside module.exports fixed the issue for me

module.exports = Posts