Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
I am running into the follow error: Error: Query.totalPosts defined in resolvers, but not in schema

I am running into the error: Error: Query.totalPosts defined in resolvers, but not in schema. I have been looking for a solution but am unable to find a work around or a solution.

my server.js:

    const express = require('express')
    const {ApolloServer} = require('apollo-server-express');
    const http = require('http');
    const path = require('path');
const {fileLoader, mergeTypes} = require('merge-graphql-schemas');
require('dotenv').config();

// //resolvers`enter code here
const resolvers = {
    Query: {
        totalPosts: () => 42,
        me: () => 'Gaia'
    }
};

const typeDefs = mergeTypes(fileLoader(path.join(__dirname, './typeDefs')));

async function startApolloServer(typeDefs, resolvers){
    const apolloServer = new ApolloServer({typeDefs, resolvers});
    const app = express();

    // typeDefs
    // const typeDefs = mergeTypes(fileLoader(path.join(__dirname, './typeDefs')));

    await apolloServer.start();
    
    //this method connects Apollo server to a specific HTTP framework ie: express
    apolloServer.applyMiddleware({app, path: '/graphql'});
    apolloServer.applyMiddleware({ app });


const httpserver = http.createServer(app);


    // rest endpoint
app.get('/rest', function(req, res) {
    res.json({
        data: 'hit rest endpoint'
    });
});

    app.listen(process.env.PORT, function() {
        console.log(`Server running at http://localhost:${process.env.PORT}`);
        console.log(`Graphql server running at http://localhost:${process.env.PORT}${apolloServer.graphqlPath}`);
    });
};

startApolloServer(typeDefs, resolvers);
about 4 years ago ยท Santiago Gelvez
1 answers
Answer question

0

Your field totalPosts exist in typeDef ? As documentation said in step 3, https://www.apollographql.com/docs/apollo-server/getting-started/ , you have to define your graphql schema for use it.

Every GraphQL server (including Apollo Server) uses a schema to define the structure of data that clients can query. In this example, we'll create a server for querying a collection of books by title and author.

You should type something like this :

type Query {
    totalPost: Int
    me: String
}

And, when i build a graphql api on nest.js, schema file automaticly build. I guess, it's same way with express. Did you try to setup your api following this doc : https://www.apollographql.com/docs/apollo-server/v2/integrations/middleware

typeDef look like automaticly resolve and push to a schema file. You may re-load your serve when it's should be update.

const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { typeDefs, resolvers } = require('./schema');

async function startApolloServer() {
  const app = express();
  const server = new ApolloServer({
    typeDefs,
    resolvers,
  });
  await server.start();

  server.applyMiddleware({ app });

  app.use((req, res) => {
    res.status(200);
    res.send('Hello!');
    res.end();
  });

  await new Promise(resolve => app.listen({ port: 4000 }, resolve));
  console.log(`๐Ÿš€ Server ready at http://localhost:4000${server.graphqlPath}`);
  return { server, app };
}
about 4 years ago ยท Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!