(`typeDefs 必须只包含字符串,...得到 ${typeof typeSource}`);
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除 module.exports 内的大括号解决了我的问题
Removing the braces inside module.exports fixed the issue for me