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
Become a member
to unlock all features

Level Up!

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


    Write a GraphQL Schema in JavaScript

    0.7 - 14

    Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but as our application grows, or when we start using more complex types like interfaces or unions, we find that we can’t use a GraphQL Language file in the same way as before. In this video, we’ll learn how to translate a GraphQL Schema written in GraphQL into a GraphQL Schema written in JavaScript.



    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




    We look inside of our index.js file and look at how we're defining our schema. What we're doing is using the build schema function, passing in a template literal to define our schema as a string.

    However, what we can do is build out this entire schema definition using JavaScript. Let's try that.

    The first thing that we're going to need to do is change this require statement to grab some of the functions that we need.

    The first one's going to be GraphQL schema so we can actually write out our schema. The second one's going to be GraphQL object type for our higher order types. Then finally we can grab GraphQLString, GraphQLInt and GraphQLBoolean as some primitive types in GraphQL.

    Now let's take all of these types and start rewriting our schema. The first thing we can do is say con schema equals new GraphQL schema. What we do is we pass in this configuration object that takes in a couple of keys.

    In this video, what we're going to be focusing on is the query type, and we'll actually go and make a variable in an second. But we can also pass in mutation as a key, as well as subscription to represent other kind of capabilities of our GraphQL server.

    Now let's actually go into find our query type. This query type is going to be a new GraphQL object type. I'll take in that same kind of configuration object that the schema did.

    The first field that we'll define on it is name, which is query type. We can also add in optional description, and in this case, we'll just add a description of the route query type. We can also add a key called fields, which will define what the things are that we can query for in our GraphQL schema.

    Let's go ahead and add a video field. Inside of this field, we need to specify the type, which will be video type, and we'll define that in a second. Then we'll also add in a resolve field, which will just be a function that will return a promise. This promise is just going to resolve with a simple object.

    We're going to use this object to just represent a video, so we'll add in some of the fields that we need, such as ID, title. We can also add in duration, and finally, watched.

    The idea behind this structure is that we're going to have all of these fields and each field has a type and a resolve statement. What the job of the resolve statement is is to provide a function that can return something asynchronous like a promise, and it will actually resolve with the information that we need to resolve this field.

    Now that we know how those statements work, let's actually go and write out our video type.

    Our video type follows a very similar structure to our query type, where we're just building it off of a GraphQL object type. We also give it a name, in this case it'll be video. We'll also give it a description, which will be A Video on It also has fields that we can query for on this type.

    The first field that we're going to add is ID, and the type of ID is going to be GraphQL ID. Let's actually make sure that we're grabbing that from the GraphQL package at the top of our file.

    Next up, we can add a description, and in this case, it's going to be the ID of the video.

    Next up, let's add the title, which would be very similar, except now the type is a GraphQLString and the description part is going to be the title of the video.

    We can also add the duration, which will be a GraphQLInt. The description's going to be the duration of the video in seconds.

    The last field that we're going to add is watched, which is just a GraphQLBoolean. We'll grab that and then then description is going to be whether or not the viewer has watched the video.

    Now that we have our video type defined and we have all of these fields defined on it, including ID, and title, duration and watched, we have our query type that's telling us what kind of fields are available for us to query on. We actually have a almost complete representation of our original schema that we defined using build schema and the resolvers.

    Let's actually try and get rid of this schema that we have as well as the resolve statements that we created before, and use this new schema that we created with the GraphQL schema constructor inside of our server.

    First, we can remove the root value and then we can switch into our terminal and run node index.js to start our server. Then we can switch out of full screen here, bring in our window, and run Enter and actually visit our graphical editor.

    Inside of here, we can query for our video type, we can get the ID, title, duration and whether or not it was watched. We'll execute the query and we get a response.