Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create a GraphQL API with Apollo Server 2.0

    Eve PorcelloEve Porcello
    apolloApollo
    graphqlGraphQL

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 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.

    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.

    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.

    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.

    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.

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