Skip to content

Instantly share code, notes, and snippets.

@thoughtspeed7
Created August 11, 2021 09:57
Show Gist options
  • Select an option

  • Save thoughtspeed7/c869ea01c587e4b3a504feaf38358d0b to your computer and use it in GitHub Desktop.

Select an option

Save thoughtspeed7/c869ea01c587e4b3a504feaf38358d0b to your computer and use it in GitHub Desktop.
Apollo GraphQL Server 3 Using Express On Google Cloud Run
// om namah shivaya
const { ApolloServer, gql } = require('apollo-server-express');
const express = require('express');
// dummy data
const countries = [
{
code: 'IN',
name: 'India',
},
{
code: 'US',
name: 'United States',
},
];
// typeDefs
const typeDefs = gql`
type Country {
code: String!
name: String!
}
type Query {
countries: [Country!]!
}
`;
// resolvers
const resolvers = {
Query: {
countries: () => countries,
},
};
async function startApolloServer() {
const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
const app = express();
server.applyMiddleware({ app });
const port = process.env.PORT || 4000;
await new Promise((resolve) => app.listen({ port }, resolve));
console.log(`Apollo Server 3 listening on port ${port}`);
}
startApolloServer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment