# @mrapi/api usage documentation

@mrapi/api serves as the API layer to directly provide services to the client, which can aggregate other openapi/graphql interfaces and operate the database through prisma.

# core

  • Log multi-terminal (file + terminal) output, automatic division
  • standalone mode
    • Aggregate graphql interface
    • Custom graphql interface
    • Proxy openapiy interface
    • Custom openapi interface
  • combined mode
    • Expose the dal CRUD graphql interface
    • Custom graphql interface (using prisma instance)
    • Expose the dal CRUD openapi interface
    • Custom openapi interface (using prisma instance)

# install

npm install @mrapi/api --save
1

# standalone mode

  • Reference api-basic project
  • Note: Does not provide database operation capabilities

# First, configure the basic configuration file

// config/mrapi.config.js
exports.default = {
  api: {
    openapi: {
      dalBaseUrl: 'http://ip OR domains' // openapi代理目的地地址
    },
    graphql: {
      sources: [
        {
          name: 'graphqlSourceName',
          endpoint: 'http://ip OR domains', // 源graphql服务地址
          prefix: 'prefix_', // graphql operationName前缀
          snapshot: false // 请求graphql快照
        }
      ]
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Note: For other configuration items, please refer to API configuration

# Second, start the API service

import Api, { log } from '@mrapi/api'

(async function () {
  const api = new Api()
  await api.start()
})().catch((err) => {
  log.error(err)
})
1
2
3
4
5
6
7
8

# Third, Access service

Server listening at http://127.0.0.1:1358
# access playground http://127.0.0.1:1358/playground
# access graphql http://127.0.0.1:1358/graphql/default
# access custom openapi http://127.0.0.1:1358/api/xx
# For other routes, please check the terminal print Routes Tree
1
2
3
4
5

# combined mode

  • Reference api-combine project
  • Note: Graphql-mesh and openapi proxy capabilities are not provided
  • Carry tenant information through headers['mrapi-pmt'], and change the key value through tenantIdentity in mrapi.config.js
  • Select the schema graphql interface and use /graphql/:schemaName to determine which schema corresponds to the prismaClient, and the /graphql prefix can be modified through api.graphql.path
  • Select the schema openapi interface and use headers['mrapi-schema'] to determine which schema corresponds to the prismaClient. You can change the key value through api.schemaIdentity in mrapi.config.js

# First, Configure the basic configuration file

// config/mrapi.config.js
exports.default = {
  managementUrl: 'mysql://root:123456@127.0.0.1/management',
  api: {
    schemaNames: ['one'], // prisma schema names
    server: {
      type: 'combined',
    },
  }
}
1
2
3
4
5
6
7
8
9
10

Note: For other configuration items, please refer to API configuration

# Second, Configuration before startup

Please refer to the DAL document and configure prisma related dependent files and configuration items, otherwise it may cause startup failure

# Third, Start API service

import Api, { log } from '@mrapi/api'

(async function () {
  const api = new Api()
  await api.start()
})().catch((err) => {
  log.error(err)
})
1
2
3
4
5
6
7
8

# Fourth, Access service

Server listening at http://127.0.0.1:1358
# access playground http://127.0.0.1:1358/playground
# access graphql http://127.0.0.1:1358/graphql/default
# access custom openapi http://127.0.0.1:1358/api/xx
# For other routes, please check the terminal print Routes Tree
1
2
3
4
5

# Tips

  • For custom extensions of Fastify Server instances, you can add them by calling api.server.xxxafter the @mrapi/api instance is created and before api.start()
  • @mrapi/api exposes the log instance, which can be used in the service (refer to pino). The log will be printed on the terminal and recorded on the disk at the same time, and provide the ability to split
  • Customize the openapi interface in the ${root}/src/openapi directory, please refer to the fastify route documentation
  • Customize the graphql interface in the ${root}/src/graphql directory, please refer to graphql nexus related documents
  • In standalone mode, execute method is injected into the context of custom graphql/openapi handler, which can be used to call aggregate graphql service
Last Updated: 9/15/2020, 4:13:04 PM