# @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
- Aggregate
-
combined
mode- Expose the
dal
CRUDgraphql
interface - Custom
graphql
interface (usingprisma
instance) - Expose the
dal
CRUDopenapi
interface - Custom
openapi
interface (usingprisma
instance)
- Expose the
# 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
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
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
2
3
4
5
# combined
mode
- Reference api-combine project
- Note:
Graphql-mesh
andopenapi proxy
capabilities are not provided - Carry tenant information through
headers['mrapi-pmt']
, and change the key value throughtenantIdentity
inmrapi.config.js
- Select the schema
graphql
interface and use/graphql/:schemaName
to determine which schema corresponds to theprismaClient
, and the/graphql
prefix can be modified throughapi.graphql.path
- Select the schema
openapi
interface and useheaders['mrapi-schema']
to determine which schema corresponds to theprismaClient
. You can change the key value throughapi.schemaIdentity
inmrapi.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
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
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
2
3
4
5
# Tips
- For custom extensions of
Fastify Server
instances, you can add them by callingapi.server.xxx
after the@mrapi/api
instance is created and beforeapi.start()
@mrapi/api
exposes thelog
instance, which can be used in the service (refer topino
). 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/openap
i directory, please refer to thefastify route
documentation - Customize the graphql interface in the
${root}/src/graphql
directory, please refer tographql nexus
related documents - In
standalone
mode,execute
method is injected into the context of customgraphql/openapi handler
, which can be used to call aggregategraphql
service