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

    Model your API data with Lists and Fields in KeystoneJS

    Simon VrachliotisSimon Vrachliotis
    graphqlGraphQL
    javascriptJavaScript
    nodeNode.js

    In this lesson, we create a couple of KeystoneJS Lists. We will give the lists a name, which Keystone will use to automatically generate GraphQL queries and mutations. We will then query items and create new items from the 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: To create a list in Keystone, I'll start by requiring the text field so we can use it in the list. I will now create a list with Keystone that create list.

    First, we give it a singular name, let's say cat. We'll define what fields we want in there. In the fields object, I'll add a name field which uses the text field type that we required just before. I will now start my dev server with Dev.

    If we go to the admin UI on localhost port 3000, we can see our cats list up there. It currently has no items. Let's create a cat named Pixel and save. Let's create another list now. We'll copy and paste our cat list and make this one a list of dogs. Fields can remain the same. If I restart my server, we now see our two lists.

    Let's also create a dog quickly. We'll call this one Maya. Both our lists now have one entry.

    Let's go to the graph QL playground. In the dogs tab here, we can see that we can, for example, query for all cats or all dogs. Query all cats. I want all cats and for each, I'll ask for the name field. Here's Pixel that we created just before.

    We can also use mutations to create items. I'll write a mutation called create cat. To create a cat, I'll need to pass a data objects to the mutation with a value for the name field. Let's call this one Felix. We'll ask for that name in the query response. Let's run that mutation now. There it is. If rerun the old cats query, we now have two cats.

    Just for fun, let's create a dog as well with the create dog mutation. Same deal. We give it a name value. Let's call it Bingo. Ask for the ID and name as a response. I'll run that mutation. Here's the new dog with an auto generated ID and the desired name.

    If we go back to our admin UI, we can see our two cats as well as our two dogs.