Skip to content

Instantly share code, notes, and snippets.

@ivanstnsk
Created January 10, 2022 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanstnsk/a6c91c43e70c1d171c0d0500ef6a9493 to your computer and use it in GitHub Desktop.
Save ivanstnsk/a6c91c43e70c1d171c0d0500ef6a9493 to your computer and use it in GitHub Desktop.
Apollo server setup valid on 10 Jan 2022 (for the course https://www.linkedin.com/learning/learning-graphql-11292553)
const { createServer } = require('http');
const { execute, subscribe } = require('graphql');
const { SubscriptionServer } = require('subscriptions-transport-ws');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { ApolloServer, gql } = require('apollo-server-express');
const { MockList } = require('@graphql-tools/mock');
const { ApolloServerPluginLandingPageGraphQLPlayground } = require('apollo-server-core');
const express = require('express');
(async function () {
const typeDefs = gql`
scalar Date
type SkiDay {
id: ID!
date: Date!
mountain: String!
condition: Conditions
}
type Query {
totalDays: Int!
allDays: [SkiDay!]!
}
enum Conditions {
POWDER
HEAVY
ICE
THIN
}
input AddDayInput {
date: Date!
mountain: String!
condition: Conditions
}
type RemoveDayPayload {
day: SkiDay!
removed: Boolean
totalBefore: Int
totalAfter: Int
}
type Mutation {
addDay(input: AddDayInput!): SkiDay!
removeDay(id: ID!): RemoveDayPayload!
}
type Subscription {
newDay: SkiDay!
}
`;
// const resolvers = {
// };
const mocks = {
Date: () => "12.12.1993",
Query: () => ({
allDays: () => new MockList(5),
}),
}
const app = express();
const httpServer = createServer(app);
const schema = makeExecutableSchema({
typeDefs,
mocks,
// resolvers,
});
const subscriptionServer = SubscriptionServer.create(
{ schema, execute, subscribe },
{ server: httpServer, path: '/graphql' }
);
const server = new ApolloServer({
schema,
plugins: [
{
async serverWillStart() {
return {
async drainServer() {
subscriptionServer.close();
}
};
}
},
ApolloServerPluginLandingPageGraphQLPlayground(),
],
});
await server.start();
server.applyMiddleware({ app });
const PORT = 4000;
httpServer.listen(PORT, () =>
console.log(`Server is now running on http://localhost:${PORT}/graphql`)
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment