graphql-to-restapi

0.2.12 • Public • Published

Cài module:

$ npm install --save graphql-to-restapi

Sử dụng

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);

Options

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'
        },
        // áp dụng security chỉ ở phạm vi api get profile
        security: [{
            BearerAuth: []
        }],
        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. info, servers, components, security theo như config của swagger
swagger = {
   info: {
        title: 'Sample API',
        version: '0.1.9'
    },
    servers: [
        {
            url: '/api'
        }
    ],
    components: {
        securitySchemes: {
            // Định nghĩa các kiểu authen https://swagger.io/docs/specification/authentication/
            BearerAuth: {
                type: 'http',
                scheme: 'bearer'
            }
        }
    },
    //Áp dụng security global cho tất cả api
    security: [
        {
            BearerAuth: []
        }
    ]
}
 
  • graphql: Nếu có giá trị true thì sẽ gen ra link doc graphql như graphqli của express-graphql.
  • formatContext: Là function để format context cho graphql(). Nếu formatContext không có thì request sẽ được gán vào context.

Readme

Keywords

Package Sidebar

Install

npm i graphql-to-restapi

Weekly Downloads

2

Version

0.2.12

License

ISC

Unpacked Size

39.6 kB

Total Files

15

Last publish

Collaborators

  • thanhtuan