Create a GraphQL API with Apollo Server 2.0

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet

Apollo Server is a GraphQL server that listens for incoming HTTP requests on a single endpoint. In this lesson, we will stand up a simple GraphQL service that handles a query for total days skied during a season.

Instructor: [00:01] We'll start by installing GraphQL in Apollo Server. Once these are installed, we will import Apollo Server from the Apollo Server package, and then we'll create a new instance of Apollo Server.

[00:17] The Apollo Server requires two things -- typeDefs which is the GraphQL schema, and resolvers which are functions that return data for each field. We're going to pass these into the Apollo Server constructor.

[00:30] Our typeDefs are our schema. We'll create a type called Query that has a query for total days that should return an Int. We need to match this in our resolvers with a function called Total Days that Returns an Int, in this case, 100. We also can comment our schema. I can say, Count of Total Days Sche During a Season, and this will appear in the documentation.

[00:55] Now from here, I'll call .listen on the server to run it. Once the server starts, the URL where the server is running will be passed to this function. The default URL is localhost 4000.

[01:09] Then I'm going to log that to the console, and I'll handle any errors with .cacheconsole.error. Now I can run the index.js file with Node., and this should run the server on localhost 4000. It'll open up the playground where I can run a query for total days that returns 100.

[01:30] Our typeDefs string contains the schema. Each of these fields map to a resolver that returns data for that field.