Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda