Module 3 Summary
This lesson is a summary of the final state of the work done in Module 3.0.
Module 3 Summary
In Module 3, we've set up a GraphQL API with Apollo Server (in particular with the apollo-express-server package).
src/index.ts
src/index.tsIn the main src/index.ts file, we set up a new Apollo Server instance with the ApolloServer constructor. In the ApolloServer constructor, we pass in a typeDefs string and a resolvers map that we've created in the src/graphql/ folder of our app.
We applied middleware on the Apollo Server instance and passed in the Express app instance as well as specified the path of our API endpoint to be /api.
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { typeDefs, resolvers } from './graphql';
const app = express();
const port = 9000;
const server = new ApolloServer({ typeDefs, resolvers });
server.applyMiddleware({ app, path: '/api' });
app.listen(port);
console.log(`[app] : http://localhost:${port}`);
GraphQL Schema
typeDefs (i.e. type definitions) is a string that represents the GraphQL schema. In the src/graphql/typeDefs.ts file, we use the gql tag that apollo-server-express provides to help parse a template literal into a GraphQL Abstract Tree.
We created a Listing GraphQL object type that represents the shape of a single listing. We also created the root Query and Mutation object types. In the root Query type, we established a listings field where we return a list of listings. The deleteListing field in our Mutation object accepts an id argument and when complete returns the deleted listing.
This lesson preview is part of the The newline Guide to Building Your First GraphQL Server with Node and TypeScript course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Building Your First GraphQL Server with Node and TypeScript, plus 90+ \newline books, guides and courses with the \newline Pro subscription.
