$ npm install --save graphql-to-restapi
Chỉ cần sử dụng graphql-to-restapi
như một route handle
import GraphqlRest from 'graphql-to-restapi';
import express from 'express';
const app = express();
app.use('/api', GraphqlRest({
schema: GraphQLSchema,
routeConfigs: routeConfigs
}));
app.listen(4000);
graphql-to-restapi
chấp nhận các options dưới đây:
schema(*)
: LàGraphQLSchema
instance từ [GraphQL.js
][].routeConfigs(*)
: Là config rest api và graphql resolve tương ứng
let routeConfigs = [
// Viewer
{
method: 'post',
path: '/login',
handle: {
type: 'mutation',
name: 'login'
},
tags: ['Viewer']
},{
method: 'get',
path: '/profile',
handle: {
type: 'query',
name: 'profile'
},
parameters: [
{
in: 'header',
name: 'x-token-key',
type: 'string',
required: true
}
],
tags: ['Viewer']
}
/*************************************/
// ACCOUNT
{
method: 'post',
path: '/account',
handle: {
type: 'mutation',
name: 'createAccount'
},
tags: ['Account']
},{
method: 'get',
path: '/account',
handle: {
type: 'query',
name: 'accounts'
},
tags: ['Account']
}
];
swagger
: Nếu có sẽ gen ra link doc swagger.
swagger = {
basePath: basePath //base path của api
}
graphql
: Nếu có giá trịtrue
thì sẽ gen ra link doc graphql như graphqli củaexpress-graphql
.formatContext
: Là function để formatcontext
chographql()
. NếuformatContext
không có thìrequest
sẽ được gán vào context.