0.2.12 • Published 3 years ago

graphql-to-restapi v0.2.12

Weekly downloads
8
License
ISC
Repository
github
Last release
3 years ago

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.
0.2.12

3 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago