Create a GraphQL API with Apollo Server 2.0

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

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.

egghead
egghead
~ 36 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today