# @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
-
standalonemode- Aggregate
graphqlinterface - Custom
graphqlinterface - Proxy
openapiyinterface - Custom
openapiinterface
- Aggregate
-
combinedmode- Expose the
dalCRUDgraphqlinterface - Custom
graphqlinterface (usingprismainstance) - Expose the
dalCRUDopenapiinterface - Custom
openapiinterface (usingprismainstance)
- 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-meshandopenapi proxycapabilities are not provided - Carry tenant information through
headers['mrapi-pmt'], and change the key value throughtenantIdentityinmrapi.config.js - Select the schema
graphqlinterface and use/graphql/:schemaNameto determine which schema corresponds to theprismaClient, and the/graphqlprefix can be modified throughapi.graphql.path - Select the schema
openapiinterface and useheaders['mrapi-schema']to determine which schema corresponds to theprismaClient. You can change the key value throughapi.schemaIdentityinmrapi.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 Serverinstances, you can add them by callingapi.server.xxxafter the@mrapi/apiinstance is created and beforeapi.start() @mrapi/apiexposes theloginstance, 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/openapi directory, please refer to thefastify routedocumentation - Customize the graphql interface in the
${root}/src/graphqldirectory, please refer tographql nexusrelated documents - In
standalonemode,executemethod is injected into the context of customgraphql/openapi handler, which can be used to call aggregategraphqlservice