Graphql Yoga and mongodb build to Heroku

The project stack is made up of nodejs, graphql yoga and mongodb and I am trying to build it to heroku. The build works perfectly locally but the issue though is that on heroku the build is successful but it immediately crashes when I open it up.

Here is the index.js file:

const {execute, ExecutionArgs, subscribe} = require('graphql');
const {makeBehavior} = require('graphql-ws/lib/use/uWebSockets');
const {createSchema, createYoga, Repeater, createPubSub} = require('graphql-yoga');
const {App, HttpRequest, HttpResponse} = require('uWebSockets.js');

const typeDefs = require('./src/schema');
const resolvers = require('./src/resolvers/index');
const models = require('./src/models/index');
const {decode} = require('next-auth/jwt');
const {connect} = require('./src/db');
const {useResponseCache} = require("@graphql-yoga/plugin-response-cache");
const {useDeferStream} = require("@graphql-yoga/plugin-defer-stream");
// Connect to MongoDB
connect(process.env.DB_HOST).then(() => {
    console.log("Connected to MongoDB!");
    console.log(process.env.PORT, process.env.NODE_ENV)
}).catch(e => {
    console.error(e);
    console.log('MongoDB connection error. Please make sure MongoDB is running.');
    process.exit();
});

const pubSub = createPubSub()

const yoga = createYoga({
    cors: {
        origin: '*',
        methods: ['GET', 'POST'],
        allowedHeaders: ['Content-Type', 'Authorization'],
    },
    schema: createSchema({
        typeDefs: typeDefs,
        resolvers: resolvers,
    }),
    plugins: [
        useDeferStream(),

    ],
    context: async ({ request: req}) => {
        let token = req?.headers?.get('authorization')?.split(' ')[1] || (req?.headers?.get("authorization")) 
        // let token = req?.headers?.get('authorization')?.split(' ')[1];
        //check if introspection query
        if (req?.body?.query?.includes("IntrospectionQuery")) {
            return {
                models,
                user: null,
            };
        }
        const start = Date.now();
        try {
            const jwtPayload = await decode({
                token: token,
                secret: process.env.NEXTAUTH_SECRET,
            });
            // console.log("jwtPayload", jwtPayload)
            const mid = new Date().getTime();
            const user = await models.User.findOne({email: jwtPayload?.email}, {
                _id: 1,
                email: 1,
                preferences: 1,
                accountType: 1,

            });
            const end = new Date().getTime();
            const duration = end - start;
            // console.log(`Request took: ${duration}ms`, end - mid);
            return {
                pubSub,
                user,
                models,
            };
        } catch (e) {
            console.log(e);
            return {
                models,
                user: null,
            };
        }
    },
    graphiql: {
        subscriptionsProtocol: 'WS', // use WebSockets instead of SSE
    },
});

// yoga's envelop may augment the `execute` and `subscribe` operations
// so we need to make sure we always use the freshest instance


const wsHandler = makeBehavior({
    execute: args => (args).rootValue.execute(args),
    subscribe: args => (args).rootValue.subscribe(args),
    onSubscribe: async (ctx, msg) => {
        const {schema, execute, subscribe, contextFactory, parse, validate} = yoga.getEnveloped(ctx);

        const args = {
            schema,
            operationName: msg.payload.operationName,
            document: parse(msg.payload.query),
            variableValues: msg.payload.variables,
            contextValue: await contextFactory(),
            rootValue: {
                execute,
                subscribe,
            },
        };

        const errors = validate(args.schema, args.document);
        if (errors.length) return errors;
        return args;
    },
});

const port = process.env.NODE_ENV === 'production' ? process.env.PORT : 8080;

console.log('PORT:', process.env.PORT);

App()
    .any('/*', yoga)
    .ws(yoga.graphqlEndpoint, wsHandler)
    .listen(port, (token) => {
        if (token) {
            console.log(`Server is running on port ${port}`);
        } else {
            console.error('Failed to start server');
        }
    });

// Handle graceful shutdown
process.on('SIGTERM', () => {
    console.log('SIGTERM signal received. Shutting down gracefully.');
    // Perform any necessary cleanup here
    process.exit(0);
});

// export const app = App().any('/*', yoga).ws(yoga.graphqlEndpoint, wsHandler);

I believe there is some issue with the App() function at the bottom and the port which in the env is set to development locally and production in heroku.

New contributor

Austin Edelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật