4. GraphQL

In order to set up GraphQL access to the database we need to update the configuration:

tutorial> 
......... 
......... 
......... 
......... 
......... 
......... 
......... 
CONFIGURE SYSTEM INSERT Port {
    protocol := "graphql+http",
    database := "tutorial",
    address := "127.0.0.1",
    port := 8888,
    user := "http",
    concurrency := 4,
};
CONFIGURE SYSTEM

If you are using Docker to run the EdgeDB server, replace the “address” value above with “0.0.0.0” to make sure the GraphQL port is proxied to the host.

This will expose GraphQL API on port 8888 (you can also specify any other port that you want). Pointing your browser to http://127.0.0.1:8888/explore will bring up a GraphiQL interface to EdgeDB. This interface can be used to try out queries and explore the GraphQL capabilities.

Let’s look at a basic Movie query:

{
    Movie {
        title
        year
    }
}

Which results in:

{
  "data": {
    "Movie": [
      {
        "title": "Blade Runner 2049",
        "year": 2017
      },
      {
        "title": "Dune",
        "year": null
      }
    ]
  }
}

It’s possible to apply a filter to get a specific Movie:

{
    Movie(filter: {title: {eq: "Dune"}}) {
        title
        year
        director { name }
        cast { name }
    }
}

Which results in:

{
  "data": {
    "Movie": [
      {
        "title": "Dune",
        "year": null,
        "director": {
          "name": "Denis Villeneuve"
        },
        "cast": [
          {
            "name": "Jason Momoa"
          },
          {
            "name": "Zendaya"
          },
          {
            "name": "Oscar Isaac"
          }
        ]
      }
    ]
  }
}

If we wanted to provide some customized information, like which Movie a Person acted in without altering the existing types, we could do that by creating a view instead. Let’s add that view to the schema via EdgeDB DDL:

tutorial> 
......... 
......... 
CREATE VIEW PersonView := Person {
    acted_in := Person.<cast[IS Movie]
};
CREATE VIEW

Now, after reloading the GraphiQL page, we will be able to access the PersonView:

{
    PersonView(order: {last_name: {dir: ASC}}) {
        name
        acted_in { title }
    }
}

Which results in:

{
  "data": {
    "PersonView": [
      {
        "name": "Harrison Ford",
        "acted_in": [
          {
            "title": "Blade Runner 2049"
          }
        ]
      },
      {
        "name": "Ryan Gosling",
        "acted_in": [
          {
            "title": "Blade Runner 2049"
          }
        ]
      },
      {
        "name": "Oscar Isaac",
        "acted_in": [
          {
            "title": "Dune"
          }
        ]
      },
      {
        "name": "Jason Momoa",
        "acted_in": [
          {
            "title": "Dune"
          }
        ]
      },
      {
        "name": "Denis Villeneuve",
        "acted_in": []
      },
      {
        "name": "Zendaya",
        "acted_in": [
          {
            "title": "Dune"
          }
        ]
      },
      {
        "name": "Ana de Armas",
        "acted_in": [
          {
            "title": "Blade Runner 2049"
          }
        ]
      }
    ]
  }
}