现在如何在Zeit上部署带有GraphQL后端的Next.js?

现在如何在Zeit上部署带有GraphQL后端的Next.js?,graphql,apollo,react-apollo,next.js,vercel,Graphql,Apollo,React Apollo,Next.js,Vercel,我有一个Next.js/Express/Apollo GraphQL应用程序在本地主机上运行良好 我现在尝试在Zeit上部署它,Next.js部分工作正常,但GraphQL后端失败,因为/GraphQL路由返回: 502: An error occurred with your deployment Code: NO_STATUS_CODE_FROM_LAMBDA 我的now.json看起来像: { "version": 2, "builds": [ { "src": "nex

我有一个Next.js/Express/Apollo GraphQL应用程序在本地主机上运行良好

我现在尝试在Zeit上部署它,Next.js部分工作正常,但GraphQL后端失败,因为
/GraphQL
路由返回:

502: An error occurred with your deployment
Code: NO_STATUS_CODE_FROM_LAMBDA
我的
now.json
看起来像:

{
  "version": 2,
  "builds": [
    { "src": "next.config.js", "use": "@now/next" },
    { "src": "server/server.js", "use": "@now/node" }
  ],
  "routes": [
    { "src": "/api/(.*)", "dest": "server/server.js" },
    { "src": "/graphql", "dest": "server/server.js" }
  ]
}

建议?

在Wes Bos slack通道上找到解决方案之前,我一直在犯这个错误

下面的内容对我很有用,但你可能会因为不同的原因而犯错误

我不知道它为什么有效

你可以看到它在工作

  • cd后端
  • 运行npm安装graphql导入
  • 更新
    package.json中的脚本:
  • 注意:对于非windows用户,请确保在
    &&

  • 创建
    src/writeSchema.js
  • 更新
    src/db.js
  • 更新
    src/createServer.js
  • 更新
    src/schema.graphql
  • 创建
    now.json
  • 运行
    npm Run deploy
    最初创建
    schema\u prep.graphql
  • 立即运行

  • 另一份答复说:

    您不应该混合使用graphql导入和js/ts导入。graphql文件上的语法将由graphql导入进行解释,ncc(读取_dirname内容并将文件移动到正确目录等的编译器)将忽略该语法 在我的示例中,“schema_prep.graphql”已经通过从生成的graphql文件导入进行了预处理


    希望这能有所帮助。

    下面是一个完整的示例,说明了Next.js/Apollo GraphQL在Zeit Now(作为无服务器函数/lambda)和Heroku(使用Express服务器)上运行:

    "deploy": "prisma deploy --env-file variables.env&& npm run writeSchema",
    "writeSchema": "node src/writeSchema.js"
    
    const fs = require('fs');
    const { importSchema } = require('graphql-import');
    const text = importSchema("src/generated/prisma.graphql");
    fs.writeFileSync("src/schema_prep.graphql", text)
    
    const db = new Prisma({
    typeDefs: __dirname + "/schema_prep.graphql",
    ...
    });
    
    return new GraphQLServer({
    typeDefs: __dirname + '/schema.graphql',
    ...
    });
    
    # import * from './schema_prep.graphql'
    
    {
        "version": 2,
        "name": "Project Name",
        "builds": [
            { "src": "src/index.js", "use": "@now/node-server" }
        ],
        "routes": [
            { "src": "/.*", "dest": "src/index.js" }
        ],
        "env": {
            "SOME_VARIABLE": "xxx",
            ...
        }
    }