如何在 API 中包含一个字段而不在数据库中添加 key/val?
How to include a field in API without adding the key/val in database?
我正在寻找 Strapi API returns 一对 key/value 计算并添加到服务器响应但未从数据库中获取的方法:
我用 ApolloServer
和 Prisma
做过类似的事情。如果你想知道我是怎么做到的?然后这是我的设置:
Note: I am not looking for how slug
feature in Strapi but in general I like to know how dynamic/calculate fields can be added to Strapi API Server response.
文件:./src/resolvers/Team.js
const Team = {
slug(parent) {
return parent.title.replace(/\s+/g, '-').toLowerCase();
},
};
export { Team as default };
文件:./src/resolvers/index.js
import { extractFragmentReplacements } from 'prisma-binding';
import Query from './Query';
import Mutation from './Mutation';
import Team from './Team';
const resolvers = {
Query,
Mutation,
// static
Team,
};
const fragmentReplacements = extractFragmentReplacements(resolvers);
export { resolvers, fragmentReplacements };
文件:./src/prisma.js
import { Prisma } from 'prisma-binding';
import { fragmentReplacements } from './resolvers/index';
require('dotenv').config({ path: './.env' });
const prisma = new Prisma({
typeDefs: 'src/generated/prisma.graphql',
endpoint: process.env.API_URL,
secret: process.env.PRISMA_MANAGEMENT_API_SECRET,
fragmentReplacements,
});
export { prisma as default };
文件:./src/schema.graphql
type Team {
id: ID!
title: String!
color: String!
slug: String! // this is not in Db but taken care by `./src/resolvers/StaticTeam.js`
}
正如您在上面看到的,我是如何获得 slug
的,尽管它不在数据库中。我只是想计算 key:val
添加到我的 API.
I like to know how dynamic/calculate fields can be added to Strapi API Server response
基本上您正在寻找 Custom data response,很可能与自定义端点结合使用(因为您不想覆盖现有端点)。
您可以通过扩展现有的 API 来做到这一点,让我们分解它:
- 添加自定义 API 端点
一种。添加路由
b.添加处理程序
C。 Add permissions
- 运行 一些逻辑
- Return 自定义回复
(1) 要将自定义 API 端点添加到用户定义的内容类型,您需要 (a) 在以下目录中添加路由:
./api/{content-type}/config/routes.json
像这样(在路由数组中):
{
"method": "GET",
"path": "/teams/customData",
"handler": "team.findCustomHandler",
"config": {
"policies": []
}
}
(b)在以下目录添加方法:
./api/{content-type}/controllers/{Content-Type}.js
像这样:
'use strict';
module.exports = {
async findCustomHandler(ctx) {
//your logic here
}
};
可以用原来的find method to start off and add your values using your logic (this is a great example):
async find(ctx) {
let entities;
if (ctx.query._q) {
entities = await strapi.services.team.search(ctx.query);
} else {
entities = await strapi.services.team.find(ctx.query);
}
// TODO: add your extra calculated value
return entities.map(entity => {
// You can do this here
sanitizeEntity(entity, { model: strapi.models.restaurant }));
}
}
您可以查看的文档:
Extending a Model Controller
欢迎提问
我正在寻找 Strapi API returns 一对 key/value 计算并添加到服务器响应但未从数据库中获取的方法:
我用 ApolloServer
和 Prisma
做过类似的事情。如果你想知道我是怎么做到的?然后这是我的设置:
Note: I am not looking for how
slug
feature in Strapi but in general I like to know how dynamic/calculate fields can be added to Strapi API Server response.
文件:./src/resolvers/Team.js
const Team = {
slug(parent) {
return parent.title.replace(/\s+/g, '-').toLowerCase();
},
};
export { Team as default };
文件:./src/resolvers/index.js
import { extractFragmentReplacements } from 'prisma-binding';
import Query from './Query';
import Mutation from './Mutation';
import Team from './Team';
const resolvers = {
Query,
Mutation,
// static
Team,
};
const fragmentReplacements = extractFragmentReplacements(resolvers);
export { resolvers, fragmentReplacements };
文件:./src/prisma.js
import { Prisma } from 'prisma-binding';
import { fragmentReplacements } from './resolvers/index';
require('dotenv').config({ path: './.env' });
const prisma = new Prisma({
typeDefs: 'src/generated/prisma.graphql',
endpoint: process.env.API_URL,
secret: process.env.PRISMA_MANAGEMENT_API_SECRET,
fragmentReplacements,
});
export { prisma as default };
文件:./src/schema.graphql
type Team {
id: ID!
title: String!
color: String!
slug: String! // this is not in Db but taken care by `./src/resolvers/StaticTeam.js`
}
正如您在上面看到的,我是如何获得 slug
的,尽管它不在数据库中。我只是想计算 key:val
添加到我的 API.
I like to know how dynamic/calculate fields can be added to Strapi API Server response
基本上您正在寻找 Custom data response,很可能与自定义端点结合使用(因为您不想覆盖现有端点)。
您可以通过扩展现有的 API 来做到这一点,让我们分解它:
- 添加自定义 API 端点
一种。添加路由
b.添加处理程序
C。 Add permissions - 运行 一些逻辑
- Return 自定义回复
(1) 要将自定义 API 端点添加到用户定义的内容类型,您需要 (a) 在以下目录中添加路由:
./api/{content-type}/config/routes.json
像这样(在路由数组中):
{
"method": "GET",
"path": "/teams/customData",
"handler": "team.findCustomHandler",
"config": {
"policies": []
}
}
(b)在以下目录添加方法:
./api/{content-type}/controllers/{Content-Type}.js
像这样:
'use strict';
module.exports = {
async findCustomHandler(ctx) {
//your logic here
}
};
可以用原来的find method to start off and add your values using your logic (this is a great example):
async find(ctx) {
let entities;
if (ctx.query._q) {
entities = await strapi.services.team.search(ctx.query);
} else {
entities = await strapi.services.team.find(ctx.query);
}
// TODO: add your extra calculated value
return entities.map(entity => {
// You can do this here
sanitizeEntity(entity, { model: strapi.models.restaurant }));
}
}
您可以查看的文档:
Extending a Model Controller
欢迎提问