Skip to content

caiyongmin/graphql-basedon-restful

Repository files navigation

GraphQL-Basedon-RESTful

Showcase about GraphQL server based on RESTful API server with focus on easy setup, performance and great developer experience. see this article(基于 RESTful API 的 GraphQL 服务构建) for more information.

Run

# install dependencies
yarn

# run koa server
npm run examples:koa

# run mock restful server
npm run start:json

*Tips: project does not have hot module reload, if you have modify the code, you need to rebuild it & db.json will be reset.

Examples

Altair GraphQL Client is recommended for debugging.

Create user

↥ back to examples

mutation createUser($body: UserInput!) {
  createUser(body: $body) {
    name
  }
}

variables:

{
  "body": {
    "name": "gogogo111",
    "orderId": 1,
    "productId": 1
  }
}

create user

Query user

↥ back to examples

query {
  user(userId: 1) {
    name
  }
}

query user

Query user graph

↥ back to examples

query {
  user(userId: 1) {
    name
    orders {
      price
      user {
        name
        orders {
          price
        }
      }
    }
    collections {
      title
    }
  }
}

query-user-graph

N+1 Problem

↥ back to examples

query {
  products {
    id
    title
    users(productId: 1) {
      name
    }
  }
}
# product users will only request once, not three

GET /products 200 3.528 ms - 437
GET /products/1/users 200 4.274 ms - 84

n+1-problem

Refs

Thank you here!

Welcome to commit issue and pull request!