首页 > 解决方案 > 在 nextjs 生产中无法访问 Graphql 服务器

问题描述

我在本地对其进行了测试,它可以完美地从 graphql 获取数据。但是,直到生产版本,游乐场显示消息服务器无法访问。我的本地服务器和生产服务器有什么不同?

  1. 我在 digitalOcean 中手动设置服务器。它在 nginx 和 pm2 中启动 web。
  2. 我已经在我的服务器上安装了 mongdb。
  3. 这个项目是从 nextjs 框架创建的

下面是我的 ApolloServer 代码:

    import { ApolloServer } from "apollo-server-micro";
    import { MongoClient } from "mongodb";
    import { schema } from "@/apollo/schema";

    let db;

    const apolloServer = new ApolloServer({
      schema,
      playground: true,
      context: async ({ res, req }) => {
        if (!db) {
          try {
            const dbClient = new MongoClient(process.env.MONGO_URI, {
              useNewUrlParser: true,
              useUnifiedTopology: true,
            });
    
            if (!dbClient.isConnected()) await dbClient.connect();
            db = dbClient.db("databaseName");
          } catch (error) {
            console.log(`error while connecting with graphql context (db):`;
          }
        }
        return { db, res, req };
      },
    });
    
    export const config = {
      api: {
        bodyParser: false,
      },

};

export default apolloServer.createHandler({ path: "/api/graphql" });

更新:已解决 uri 错误吗?那是我的网站网址。

return new ApolloClient({
    ssrMode,
    link: new HttpLink({
      uri: `http://dayfruit.staging.domian.com/api/graphql`,
      credentials: "include",
      fetch,
    }),
    cache,
  });

标签: graphqlnext.jsapollo-server

解决方案


推荐阅读