Skip to content

Commit

Permalink
Test deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangChanVi committed Nov 21, 2023
1 parent 2eeec00 commit 6a20a46
Showing 1 changed file with 32 additions and 55 deletions.
87 changes: 32 additions & 55 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Apollo
import { ApolloServer } from '@apollo/server';
// import { startStandaloneServer } from '@apollo/server/standalone';
import { startStandaloneServer } from '@apollo/server/standalone';

import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';

Expand All @@ -27,57 +27,47 @@ const pubsub = new PubSub();
export { pubsub };

export async function bootstrap(port) {
// const server = new ApolloServer({
// typeDefs,
// resolvers,
// context: () => {
// return { prisma };
// },
// introspection: true,
// cors: {
// origin: '*', // <- allow request from all domains
// credentials: true, // <- enable CORS response for requests with credentials (cookies, http authentication)
// },
// plugins: [
// ApolloServerPluginLandingPageLocalDefault({ embed: true }),
// ...(parseInt(process.env.IS_LOGGING) ? [loggingPlugin] : []),
// ],
// logger: console,
// // csrfPrevention: true,
// });

// const { url } = await startStandaloneServer(server, {
// listen: { port: 4000 },
// });

// console.log(`Server ready at: ${url}`);

//!!!!!!!!!!!!!!!!!!
const server = new ApolloServer({
typeDefs,
resolvers,
context: () => {
return { prisma };
},
introspection: true,
cors: {
origin: '*', // <- allow request from all domains
credentials: true, // <- enable CORS response for requests with credentials (cookies, http authentication)
},
plugins: [
ApolloServerPluginLandingPageLocalDefault({ embed: true }),
...(parseInt(process.env.IS_LOGGING) ? [loggingPlugin] : []),
],
logger: console,
// csrfPrevention: true,
});

const { url } = await startStandaloneServer(server, {
listen: { port: port },
});

console.log(`Server ready at: ${url}`);
}

export async function bootstrapWithSubscription(port) {
// https://www.apollographql.com/docs/apollo-server/data/subscriptions/

const app = express();
// Our httpServer handles incoming requests to our Express app.
// Below, we tell Apollo Server to "drain" this httpServer,
// enabling our servers to shut down gracefully.
const httpServer = http.createServer(app);

// Creating the WebSocket server
const wsServer = new WebSocketServer({
// This is the `httpServer` we created in a previous step.
server: httpServer,
// Pass a different path here if app.use
// serves expressMiddleware at a different path
// path: '/subscriptions',
path: '/',
});

const schema = makeExecutableSchema({ typeDefs, resolvers });
// Hand in the schema we just created and have the
// WebSocketServer start listening.
const serverCleanup = useServer({ schema }, wsServer);

// Same ApolloServer initialization as before, plus the drain plugin
// for our httpServer.
const server = new ApolloServer({
schema,
context: () => {
Expand All @@ -87,10 +77,7 @@ export async function bootstrap(port) {
plugins: [
ApolloServerPluginLandingPageLocalDefault({ embed: true }),
...(parseInt(process.env.IS_LOGGING) ? [loggingPlugin] : []),
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),

// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
Expand All @@ -102,27 +89,17 @@ export async function bootstrap(port) {
},
],
logger: console,
// csrfPrevention: true,
});

// Ensure we wait for our server to start
await server.start();

// Set up our Express middleware to handle CORS, body parsing,
// and our expressMiddleware function.
app.use(
'/',
cors(),
express.json(),
// expressMiddleware accepts the same arguments:
// an Apollo Server instance and optional configuration options
expressMiddleware(server),
);
app.use('/', cors(), express.json(), expressMiddleware(server));

httpServer.listen(port, () => {
console.log(`Server is now running on http://localhost:${port}`);
});
}

bootstrap(4000);
// bootstrap(4001);
process.env.ALLOW_SUBSCRIPTION
? bootstrapWithSubscription(4000)
: bootstrap(4000);

0 comments on commit 6a20a46

Please sign in to comment.