Skip to main content

Running the example

The project codebase comes with an example database, you can use to run the example project.

In this article we will follow the instructions on how to setup the example project, and we can perform some useful queries.

Requirements

  1. rustup: https://rustup.rs/
  2. mysql / postgres database (optional)

Install from Cargo

cargo install seaography-cli

Install from source

  1. Clone project

    git clone git@github.com:SeaQL/seaography.git
  2. Build project

    cd ./seaography/cli
    cargo install --path .

MySQL

  1. Locate project

    cd ./examples/mysql
  2. Create database

    mysql -uroot -p -h 127.0.0.1 mysql -e 'CREATE DATABASE `sakila`'
  3. Import schema

    mysql -uroot -p -h 127.0.0.1 sakila < sakila-schema.sql
  4. Import data

    mysql -uroot -p -h 127.0.0.1 sakila < sakila-data.sql

Postgres

  1. Locate project

    cd ./examples/postgres
  2. Create database

    psql -q postgres://postgres:postgres@localhost/postgres -c 'CREATE DATABASE "sakila"'
  3. Import schema

    psql -q postgres://postgres:postgres@localhost/sakila < sakila-schema.sql
  4. Import data

    psql -q postgres://postgres:postgres@localhost/sakila < sakila-data.sql

SQLite

  1. Locate project
    cd ./examples/sqlite

Extending and configuring code

You are free to modify the code and change things to fit your needs. For more info How to extend generated code.

Running server

cargo run

http://127.0.0.1:8000/

Regenerating code

Here are the instructions on how we can generate the examples from scratch.

  1. Set working directory to Seaography folder

    $ cd ./seaography
  2. Build project

    cargo build
  3. Clean generated code

    rm -rf ./examples/{mysql|sqlite|postgres}/src
  4. Generate code

    cd ./examples/{mysql|sqlite|postgres}
    seaography-cli <database_url> seaography-example .

    * Example database urls "sqlite://sakila.db", "mysql://username:password@127.0.0.1/sakila"

Query examples

Fetch films and their actors

{
film(pagination: { limit: 10, page: 0 }, orderBy: { title: ASC }) {
data {
title
description
releaseYear
filmActor {
actor {
firstName
lastName
}
}
}
}
}

Response

{
"data": {
"film": {
"data": [
...
{
"title": "ACE GOLDFINGER",
"description": "A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China",
"releaseYear": 2006,
"filmActor": [
{
"actor": {
"firstName": "BOB",
"lastName": "FAWCETT"
}
},
{
"actor": {
"firstName": "MINNIE",
"lastName": "ZELLWEGER"
}
},
{
"actor": {
"firstName": "SEAN",
"lastName": "GUINESS"
}
},
{
"actor": {
"firstName": "CHRIS",
"lastName": "DEPP"
}
}
]
},
...
]
}
}
}

Fetch store and its employee

{
store(filters: { storeId: { eq: 1 } }) {
data {
storeId
address {
address
address2
}
staff {
firstName
lastName
}
}
}
}

Response

{
"data": {
"store": {
"data": [
{
"storeId": 1,
"address": {
"address": "47 MySakila Drive",
"address2": null
},
"staff": {
"firstName": "Mike",
"lastName": "Hillyer"
}
}
]
}
}
}

Fetch inactive customers with pagination

{
customer(filters: { active: { eq: 0 } }, pagination: { page: 2, limit: 3 }) {
data {
customerId
lastName
email
}
pages
current
}
}

Response

{
"data": {
"customer": {
"data": [
{
"customerId": 315,
"lastName": "GOODEN",
"email": "KENNETH.GOODEN@sakilacustomer.org"
},
{
"customerId": 368,
"lastName": "ARCE",
"email": "HARRY.ARCE@sakilacustomer.org"
},
{
"customerId": 406,
"lastName": "RUNYON",
"email": "NATHAN.RUNYON@sakilacustomer.org"
}
],
"pages": 5,
"current": 2
}
}
}

Fetch expensive orders and customer data

{
payment(filters: { amount: { gt: "11.1" } }) {
data {
paymentId
amount
customer {
lastName
email
}
}
pages
current
}
}

Response

{
"data": {
"payment": {
"data": [
{
"paymentId": 342,
"amount": "11.99",
"customer": {
"lastName": "JACKSON",
"email": "KAREN.JACKSON@sakilacustomer.org"
}
},
{
"paymentId": 3146,
"amount": "11.99",
"customer": {
"lastName": "GIBSON",
"email": "VICTORIA.GIBSON@sakilacustomer.org"
}
},
{
"paymentId": 5280,
"amount": "11.99",
"customer": {
"lastName": "SIMS",
"email": "VANESSA.SIMS@sakilacustomer.org"
}
},
...
],
"pages": 1,
"current": 1
}
}
}