• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

acro5piano/graphql-rest-proxy: Turn your REST API into GraphQL - A Proxy Server ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

acro5piano/graphql-rest-proxy

开源软件地址(OpenSource Url):

https://github.com/acro5piano/graphql-rest-proxy

开源编程语言(OpenSource Language):

TypeScript 95.7%

开源软件介绍(OpenSource Introduction):

test release npm version codecov

image

graphql-rest-proxy

Convert your REST server to GraphQL server.

Install

npm -g install graphql-rest-proxy

Or if you use Yarn:

yarn global add graphql-rest-proxy

Why

We all know GraphQL is great, so you want to move from REST API to GraphQL.

However, it requires a lot of effort to replace your current REST API with a brand new GraphQL server.

graphql-rest-proxy comes in to address this issue. It proxies GraphQL to REST API according to the defined schema.

image

Getting Started

STEP 1. Define your schema.

schema.graphql

type User {
  id: Int
  name: String
  isActive: Boolean
}

type Query {
  getUser: User @proxy(get: "https://my-rest-api.com/user")
}

STEP 2. Run your proxy server.

graphql-rest-proxy schema.graphql

# => graphql-rest-proxy is running on http://localhost:5252

STEP 3. Request!

curl -XPOST -H 'Content-Type: application/json' \
    -d '{ "query": "{ getUser { id name isActive } }" }' \
    http://localhost:5252/graphql

It will return like this:

{
  "data": {
    "getUser": {
      "id": 1,
      "name": "Tom",
      "isActive": false
    }
  }
}

Examples

Basic Query Proxy

type User {
  id: Int
  name: String
}

type Query {
  getUser: User @proxy(get: "https://my-rest-api.com/user")
  getUsers: [User] @proxy(get: "https://my-rest-api.com/users")
}

Query with Parameters

You can refer the id of query args by $id.

type User {
  id: Int
  name: String
}

type Query {
  getUserById(id: Int!): User @proxy(get: "https://my-rest-api.com/users/$id")
}

Mutation with Input Parameters

Mutation forward variables to the REST API.

input UserInput {
  name: String!
}

type User {
  id: Int
  name: String
}

type Mutation {
  createUser(user: UserInput!): User @proxy(post: "https://my-rest-api.com/users")
  updateUser(id: Int!, user: UserInput!): User @proxy(patch: "https://my-rest-api.com/users/$id")
}

Request example:

fetch('http://localhost:5252/graphql', {
  method: 'patch',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    query: gql`
      mutation UpdateUser($id: Int!, $user: UserInput!) {
        updateUser(id: $id, user: $user) {
          id
          name
        }
      }
    `,
    variables: {
      id: 1,
      user: {
        name: 'acro5piano',
      },
    },
  }),
})

Nested Objects

You can refer the id of parent object by $id.

type Post {
  id: Int
  title: String
}

type User {
  id: Int
  name: String
  posts: [Post] @proxy(get: "https://my-rest-api.com/users/$id/posts")
}

type Query {
  getUser: User @proxy(get: "https://my-rest-api.com/user")
}

Specify base url

You can set base url to reduce verbosity:

graphql-rest-proxy --baseUrl https://my-rest-api.com schema.graphql
type Query {
  getUser: User @proxy(get: "/user")
}

Configuration

CLI options are:

Usage: graphql-rest-proxy <command> [options]

Commands:
  graphql-rest-proxy <file>        Start graphql-rest-proxy server.          [default]
  graphql-rest-proxy print <file>  Print GraphQL schema

Options:
  --version      Show version number                                   [boolean]
  -c, --config   Specify config file
  -p, --port     Specify port
  -b, --baseUrl  Specify proxy base url
  -h, --help     Show help                                             [boolean]

You can also set a config file.

proxy.config.js

module.exports = {
  baseUrl: 'https://myapi.com',
  port: 3000,
}

And run with the configuration:

graphql-rest-proxy --config proxy.config.js schema.graphql

Notes

Request as less as possible

graphql-rest-proxy does not request if proxy response includes child object. This means you can reduce API call if you includes child object.

For example, if the schema is like this:

type Post {
  id: Int
  title: String
}

type User {
  id: Int
  name: String
  posts: [Post] @proxy(get: "https://my-rest-api.com/users/$id/posts")
}

type Query {
  getUser: User @proxy(get: "https://my-rest-api.com/user")
}

And REST API returns like this:

curl https://my-rest-api.com/user
{
  "id": 1,
  "name": "acro5piano",
  "posts": {
    "id": 1,
    "title": "graphql-rest-proxy"
  }
}

In this case, posts is embbed in response, so graphql-rest-proxy doesn't request to https://my-rest-api.com/users/1/posts.

Development Status

Still in Beta. If you have any suggestions or feature requests, feel free to open new issues or Pull Requests!

TODO:

  • More type support
    • Fragment
    • Scalar
  • Refactoring
  • Logging



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
amanjeetsingh150/GraphQL-Android: A project for showing use of apollo-android fo ...发布时间:2022-06-22
下一篇:
machadop1407/graphql-full-course发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap