在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):acro5piano/graphql-rest-proxy开源软件地址(OpenSource Url):https://github.com/acro5piano/graphql-rest-proxy开源编程语言(OpenSource Language):TypeScript 95.7%开源软件介绍(OpenSource Introduction):graphql-rest-proxyConvert your REST server to GraphQL server. Installnpm -g install graphql-rest-proxy Or if you use Yarn: yarn global add graphql-rest-proxy WhyWe 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.
Getting StartedSTEP 1. Define your schema.
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
}
}
} ExamplesBasic 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 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 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 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")
} ConfigurationCLI options are:
You can also set a config file.
module.exports = {
baseUrl: 'https://myapi.com',
port: 3000,
} And run with the configuration: graphql-rest-proxy --config proxy.config.js schema.graphql NotesRequest as less as possible
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, Development StatusStill in Beta. If you have any suggestions or feature requests, feel free to open new issues or Pull Requests! TODO:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论