How to deploy Next.js with GraphQL backend on Zeit Now?

牧云@^-^@ 提交于 2019-12-11 11:47:03

问题


I have an Next.js/Express/Apollo GraphQL app running fine on localhost.

I try to deploy it on Zeit Now, and the Next.js part works fine, but the GraphQL backend fails because /graphql route returns:

502: An error occurred with your deployment
Code: NO_STATUS_CODE_FROM_LAMBDA

My now.json looks like:

{
  "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" }
  ]
}

Suggestions?


回答1:


I was getting that error until I found on a solution on the Wes Bos slack channel.

The following worked for me, but it's possible you could be getting that error for a different reason.

I'm not sure why it works.

You can see it working here

  1. cd backend
  2. Run npm install graphql-import
  3. Update scripts in package.json:
"deploy": "prisma deploy --env-file variables.env&& npm run writeSchema",
"writeSchema": "node src/writeSchema.js"

Note: For non windows users make sure to place space before &&

  1. Create 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)
  1. Update src/db.js:
const db = new Prisma({
typeDefs: __dirname + "/schema_prep.graphql",
...
});
  1. Update src/createServer.js:
return new GraphQLServer({
typeDefs: __dirname + '/schema.graphql',
...
});
  1. Update src/schema.graphql:
# import * from './schema_prep.graphql'
  1. Create now.json
{
    "version": 2,
    "name": "Project Name",
    "builds": [
        { "src": "src/index.js", "use": "@now/node-server" }
    ],
    "routes": [
        { "src": "/.*", "dest": "src/index.js" }
    ],
    "env": {
        "SOME_VARIABLE": "xxx",
        ...
    }
}
  1. Run npm run deploy to initially create schema_prep.graphql.
  2. Run now

Another reply said this:

You should not mix graphql imports and js/ts imports. The syntax on the graphql file will be interpreted by graphql-import and will be ignored by ncc (the compiler which reads the __dirname stuff and move the file to the correct directory etc) In my example 'schema_prep.graphql' is already preprocessed with the imports from the generated graphql file.

Hopefully this helps.



来源:https://stackoverflow.com/questions/56305509/how-to-deploy-next-js-with-graphql-backend-on-zeit-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!