将 GraphQL 后端部署到 vercel

问题描述 投票:0回答:1

GraphQL Server 在本地工作但在 Vercel 上的生产中失败 我使用 GraphQL 作为后端 API 服务开发了一个 Web 应用程序。该代码在本地开发中完美运行,但当我尝试使其在 Vercel 上投入生产时,我遇到了问题。详情如下:

本地开发代码(server.js)

import { ApolloServer } from "apollo-server";
import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
import typeDefs from './logic/schemaGQL.js';
import mongoose from "mongoose";
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';

dotenv.config();
const MONGO_URI = process.env.DATABASE_URL;
const SECRATE = process.env.SECRATE;

mongoose.connect(MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true
})

mongoose.connection.on("connected", () => {
    console.log("connected to mongodb")
})

mongoose.connection.on("error", (err) => {
    console.log("error connecting", err)
})

import './DB/User.js'
import './DB/SubWaiting.js'
import './DB/Sub.js'
import './DB/Msg.js'
import './DB/SubUncheck.js'
import './DB/Revenue.js'
import './DB/Month.js'
import './DB/PlanSold.js'
import './DB/Waiting.js'
import './DB/Refund.js'
import './DB/Help.js'

import resolvers from './logic/resolver.js'

const context = ({ req }) => {
    const { authorization } = req.headers;
    if (authorization) {
        const { userId } = jwt.verify(authorization, SECRATE)
        return { userId }
    }
}

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context,
    plugins: [
        ApolloServerPluginLandingPageGraphQLPlayground()
    ]
});

server.listen().then(({ url }) => {
    console.log(`🚀  Server ready at ${url}`);
});

Vercel 的生产代码 (server.js)


import { ApolloServer } from 'apollo-server-micro';
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';
import typeDefs from './logic/schemaGQL.js';
import mongoose from "mongoose";
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';

dotenv.config();

const MONGO_URI = process.env.DATABASE_URL;
const SECRATE = process.env.SECRATE;

mongoose.connect(MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

mongoose.connection.on("connected", () => {
    // console.log("Connected to MongoDB");
});

mongoose.connection.on("error", (err) => {
    // console.log("Error connecting to MongoDB", err);
});

import resolvers from './logic/resolver.js';

const context = ({ req }) => {
    const { authorization } = req.headers;
    if (authorization) {
        const { userId } = jwt.verify(authorization, SECRATE);
        return { userId };
    }
};

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context,
    plugins: [
        ApolloServerPluginLandingPageGraphQLPlayground(),
    ],
});


// const startServer = server.start();


export default server.createHandler({
    path: '/api/graphql',
});

export const config = {
    api: {
        bodyParser: false,
    },
};

vercel.json 配置

{
    "version": 2,
    "builds": [{
        "src": "server.js",
        "use": "@vercel/node"
    }],
    "routes": [{
        "src": "/api/graphql",
        "dest": "/api/graphql"
    }]
}

我还尝试将 vercel.json 中的路由更改为:

"routes": [{
    "src": "/(.*)",
    "dest": "/api"
}]

问题 当我尝试访问此 URL:

https://splits-two.vercel.app/api/graphql
时,有时会收到 404 Not Found、500 Internal Server Error 或指示崩溃的无服务器错误。

该应用程序在本地运行得非常好。是什么导致 Vercel 生产中出现这些问题?

graphql vercel serverless apollo-server
1个回答
0
投票

您似乎正在使用

apollo-server
v2 [已达到 EOL(寿命终止)10/2023] 或 v3 [EOL 10/2024]。相反,您应该使用 Apollo v4。
@apollo/server
apollo-server
的替代品,
@as-integrations/next
apollo-server-micro
的替代品。

参见:https://www.npmjs.com/package/@as-integrations/next

import { ApolloServer } from '@apollo/server';
import { startServerAndCreateNextHandler } from '@as-integrations/next';

import resolvers from './logic/resolver.js'
import typeDefs from './logic/schemaGQL.js';

const server = new ApolloServer({
  resolvers,
  typeDefs,
});

export default startServerAndCreateNextHandler(server, {
  context,
});

完成此操作后,您会从服务器收到什么错误?

© www.soinside.com 2019 - 2024. All rights reserved.