(`typeDefs 必须只包含字符串,...得到 ${typeof typeSource}`);

发布于 2025-01-09 19:37:33 字数 1334 浏览 1 评论 0原文

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(`
              

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-16 19:37:33

删除 module.exports 内的大括号解决了我的问题

module.exports = Posts

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

module.exports = Posts
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文