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 server from scratch with Apollo Server 2.0

    Tomasz ŁakomyTomasz Łakomy
    graphqlGraphQL

    In this lesson we are going to learn how to use Apollo Server 2.0 to create a GraphQL server from scratch and how to verify if the server behaves as expected by using GraphQL Playground.

    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: First, install Apollo Server and GraphQL from NPM. Once it's done, create a new JavaScript file. Inside of this file, get Apollo Server and GQL from Apollo Server. Next, I'm going to paste in an array of superhero movies.

    We have an array of two elements. Each one of those has title and the release date. Next, we need to define the shape of our data. To do that, create a typedefs object. It's going to use the GQL which we imported from Apollo Server to define what kind of types we're going to have in our server.

    We're going to have a type which is called movie, and each movie has a title, which is a string, and a release date, which also is a string. As well as we're going to have a type of query, so we would be able to query not books, sorry, movies. It's going to return an array of movies.

    Next we need to create the resolvers object. In the resolvers object, we're going to specify what kind of queries we're going to have. We're going to have a query for movies, and it's going to have a function which is going to return the superhero movies array that we defined over here.

    Next, we're going to create the server. It's going to be a new Apollo Server, and it's going to take as an argument object containing resolvers and the typedefs. Next, launch the server. Do server.listen, and then we're going to pass in a function which is going to console.log that our server is ready.

    Server ready at URL, and we're going to get the URL from here. After we save that, we are ready to go. Start the server by running node index.js. Afterwards, we see that our server is ready at localhost 4000. After we click on this thing, we're going to see GraphQL playground.

    Here, we can see a couple of interesting things. First off, we can click over here to check out our GraphQL schema. Here, we can see that our server's ready to take one query, which is a query for movies.

    It's going to return an array of movies, and a type of movie has a title and release date, exactly what we specified. Now, we can type in a query. We're going to fetch some movies, and I'm going to ask for a title.

    After we execute this query, we're going to see that our server has returned two movies, and we only get the title, because this is exactly what we asked for. If you would like to get the release date, we need to ask for it as well, because GraphQL returns only the data you have asked for and nothing more. Now, we can be absolutely sure that our server works as intended.