/typegraphql-relay

Relay-compliant GraphQL server using TypeGraphQL and TypeORM boilerplate.

Primary LanguageTypeScriptMIT LicenseMIT

TypeGraphQL Relay

Relay-compliant GraphQL server using TypeGraphQL and TypeORM boilerplate.


Introduction

This documentation will be completely rewritten explaining better how to get started. This means that this repository is constantly changing and that is why we don't recommend using it in production until all features have been completed.

This is a GraphQL server boilerplate that follows the Global Object Identification, GraphQL Server Specification and GraphQL Cursor Connections Specification specifications.

Running

Environment variables

You need to configure the environment variables. Just create a .env based on .env.defaults.

Database

This boilerplate requires that you have a PostgreSQL instance running, you can either install on your operating system or use Docker instead.

In order to change from PostgreSQL to another TypeORM supported database (MySQL, MariaDB, SQL Server, etc) you just need to change the type on the database loader and remove the pg package.

Using Docker

If you have Docker and Docker Compose installed you just need to run docker-compose up -d on your terminal.

The postgres service will create automatically the dabatase specified on the DATABASE_NAME environment variable.

Server

Install all the dependencies

yarn

Then you can finally

yarn start:dev

If you see this message on your terminal:

✔ The server is running at http://127.0.0.1:8080

Congratulations, the server is running. 🚀

The GraphQL endpoint is based on the GRAPHQL_PATH environment variable.

Improvements

The most important parts are implemented and working properly but we have room for improvements.

  • Tests
  • Ordering
  • Filtering
  • Error handling
  • DataLoader
  • Production ready

Feel free to send suggestions and pull requests.

Examples

Mutation

mutation {
  addProduct(
    input: {
      name: "iPhone 12 Pro"
      description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sed dui scelerisque, lacinia ipsum vitae, placerat felis."
    }
  ) {
    product {
      id
      name
      description
      createdAt
      updatedAt
    }
  }
}

Global Object Identification

{
  node(id: "UHJvZHVjdDoy") {
    ... on Product {
      id
      name
      description
      createdAt
      updatedAt
    }
  }
  nodes(ids: ["UHJvZHVjdDoy"]) {
    ... on Product {
      id
      name
      description
      createdAt
      updatedAt
    }
  }
}

Cursor Connections (Pagination)

Forward

{
  products(first: 2) {
    edges {
      node {
        id
        name
        description
        createdAt
        updatedAt
      }
      cursor
    }
    pageInfo {
      startCursor
      endCursor
      hasPreviousPage
      hasNextPage
    }
  }
}

Forward with cursor

{
  products(first: 2, after: "YXJyYXljb25uZWN0aW9uOjE=") {
    edges {
      node {
        id
        name
        description
        createdAt
        updatedAt
      }
      cursor
    }
    pageInfo {
      startCursor
      endCursor
      hasPreviousPage
      hasNextPage
    }
  }
}

Backward

{
  products(last: 2) {
    edges {
      node {
        id
        name
        description
        createdAt
        updatedAt
      }
      cursor
    }
    pageInfo {
      startCursor
      endCursor
      hasPreviousPage
      hasNextPage
    }
  }
}

Backward with cursor

{
  products(last: 2, before: "YXJyYXljb25uZWN0aW9uOjg=") {
    edges {
      node {
        id
        name
        description
        createdAt
        updatedAt
      }
      cursor
    }
    pageInfo {
      startCursor
      endCursor
      hasPreviousPage
      hasNextPage
    }
  }
}