Become a member
to unlock all features

Level Up!

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


    Build a Simple GraphQL Server with GraphQL Yoga


    GraphQL Yoga is an open source server utility tool based on Express that simplifies how developers build GraphQL servers. This lesson shows how quick it is to get started with Yoga by writing simple types and resolvers. It also shows you how to test and inspect a GraphQL server/schema using the GraphQL playground.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Instructor: First thing we need to do is to import GraphQL server from GraphQL Yoga. After importing GraphQL server, I can go ahead and create an instance of this server, which takes a configuration object and receives the type definitions, as well as the resolvers.

    We can then start the server using the start method. It also takes a configuration object. We will inaudible set the port to 4200, as well as a callback function that is called once the server starts running.

    The type definitions on the resolvers are correctly undefined. Let's start creating the type definitions. We need a type, computer, and it has a property module, which is of string type, a property maker, which is also a string, and then the year it was manufactured, which can be an integer.

    We also need the query type. It has just a single property, the return of the computers we have. For now, we can just the leave the resolvers as an empty object. We can start the server and see what the schema looks like.

    Running the server starts the playground. You can open the schemata and see the type definitions we just created. So far, we have the module, the maker, and the year fields in the computer type. You can also see that we have the query type, which returns an array of computers.

    Tell this all happens when we send a computers query. We first create a computers resolver, which is simply a function that returns a value. For now, we just leave the value as an array. When we head back to the playground, we can then ask the server for these computers. As expected, we should get an empty array.

    Rather than returning an empty array, we can return something more meaningful, which could be an array of two computers. The first one is made by Apple, and is the MacBook Pro. It was manufactured in the year 2017.

    The second is made by the same company, but is the MacBook Air, and manufactured 2016. We can then replace the empty array with computer's data. We can save now and query the server, so we can see what we have.

    We can see we have the MacBook Pro and the MacBook Air. We can also expand the query to have the year and the maker. As you can see, now it includes the year, 2017, maker, Apple. Whenever I will send a query to the server, the GraphQL SRI receives this query, validates it against the type definitions, and then uses the resolvers to send the payload back to the client.