GraphQL 插件
¥GraphQL plugin
默认情况下,Strapi 为你的每种内容类型创建 REST 端点。GraphQL 插件添加了一个 GraphQL 端点来获取和修改你的内容。安装 GraphQL 插件后,你可以使用基于 Apollo Server 的 GraphQL Sandbox 以交互方式构建查询和修改,并阅读针对你的内容类型量身定制的文档。
¥By default Strapi create REST endpoints for each of your content-types. The GraphQL plugin adds a GraphQL endpoint to fetch and mutate your content. With the GraphQL plugin installed, you can use the Apollo Server-based GraphQL Sandbox to interactively build your queries and mutations and read documentation tailored to your content types.
Configured through both admin panel and server code, with different sets of options.
@strapi/plugin-graphql


安装
¥Installation
要安装 GraphQL 插件,请在终端中运行以下命令:
¥To install the GraphQL plugin, run the following command in your terminal:
- Yarn
- NPM
yarn add @strapi/plugin-graphql
npm install @strapi/plugin-graphql
安装后,GraphQL 沙箱可通过 /graphql
URL 访问,并可用于以交互方式构建查询和修改并阅读针对你的内容类型量身定制的文档。
¥Once installed, the GraphQL sandbox is accessible at the /graphql
URL and can be used to interactively build your queries and mutations and read documentation tailored to your content-types.
安装插件后,当你的 Strapi 应用服务器运行时,可以通过 /graphql
路由(例如 localhost:1337/graphql)访问 GraphQL Sandbox。
¥Once the plugin is installed, the GraphQL Sandbox is accessible at the /graphql
route (e.g., localhost:1337/graphql) when your Strapi application server is running.
配置
¥Configuration
文档插件的大多数配置选项都通过你的 Strapi 项目的代码处理,尽管 GraphQL 在线运行也提供了一些非特定的 Strapi 设置。
¥Most configuration options for the Documentation plugin are handled via your Strapi project's code, though the GraphQL playground also offers some non-specific Strapi settings.
管理面板设置
¥Admin panel settings
Strapi 管理面板不提供 GraphQL 插件的 Strapi 特定设置。但是,在 /graphql
路由上可访问的 GraphQL Playground 是一个嵌入式 Apollo Server 在线运行,因此它包括此类实例可用的所有配置和设置。有关详细信息,请参阅官方 GraphQL 在线运行文档。
¥The Strapi admin panel does not provide Strapi-specific settings for the GraphQL plugin. However, the GraphQL Playground accessible at the /graphql
route is an embedded Apollo Server playground, so it includes all configuration and settings available with such an instance. Please refer to the official GraphQL playground documentation for details.
基于代码的配置
¥Code-based configuration
插件配置在 config/plugins.js
文件 中定义。此配置文件可以包含一个 graphql.config
对象来定义 GraphQL 插件的特定配置。
¥Plugins configuration are defined in the config/plugins.js
file. This configuration file can include a graphql.config
object to define specific configurations for the GraphQL plugin.
可用选项
¥Available options
Apollo 服务器 选项可以通过 graphql.config.apolloServer
配置对象直接传递给 Apollo。例如,可以使用 Apollo Server 选项来启用 追踪功能,它由 GraphQL Sandbox 支持,用于跟踪查询每个部分的响应时间。Apollo Server
默认缓存选项是 cache: 'bounded'
。你可以在 apolloServer
配置中更改它。有关更多信息,请访问Apollo 服务器文档。
¥Apollo Server options can be passed directly to Apollo with the graphql.config.apolloServer
configuration object. Apollo Server options can be used for instance to enable the tracing feature, which is supported by the GraphQL Sandbox to track the response time of each part of your query. The Apollo Server
default cache option is cache: 'bounded'
. You can change it in the apolloServer
configuration. For more information visit Apollo Server Docs.
GraphQL 插件具有以下特定配置选项,应在 config/plugins
文件中的 graphql.config
对象中声明。所有参数都是可选的:
¥The GraphQL plugin has the following specific configuration options that should be declared in a graphql.config
object within the config/plugins
file. All parameters are optional:
选项 | 类型 | 描述 | 默认值 | 注意 |
---|---|---|---|---|
endpoint | 字符串 | 设置 GraphQL 端点路径。 | '/graphql' | 示例:/custom-graphql |
shadowCRUD | 布尔值 | 启用或禁用内容类型的自动架构生成。 | true | |
depthLimit | 数字 | 限制 GraphQL 查询的深度以防止过度嵌套。 | 10 | 使用它来缓解潜在的 DoS 攻击。 |
amountLimit | 数字 | 限制单个响应中返回的最大项目数。 | 100 | 请谨慎使用以避免性能问题。 |
playgroundAlways | 布尔值 | [已弃用] 在所有环境中启用 GraphQL Playground(已弃用)。 | false | 最好使用 landingPage 。 |
landingPage | 布尔值 | 函数 | 启用或禁用 GraphQL 的登录页面。接受布尔值或返回布尔值的函数或实现 renderLandingPage 的 ApolloServerPlugin。 | 生产环境中为 false ,其他环 境中为 true | |
apolloServer | 目的 | 将配置选项直接传递给 Apollo Server。 | {} | 示例:{ tracing: true } |
默认情况下,响应返回的最大项目数限制为 100。可以使用 amountLimit
配置选项更改此值,但只有在仔细考虑后才应更改:大型查询可能会导致 DDoS(分布式拒绝服务),并可能导致 Strapi 服务器以及数据库服务器负载异常。
¥The maximum number of items returned by the response is limited to 100 by default. This value can be changed using the amountLimit
configuration option, but should only be changed after careful consideration: a large query can cause a DDoS (Distributed Denial of Service) and may cause abnormal load on your Strapi server, as well as your database server.
GraphQL Sandbox 在除生产环境以外的所有环境中默认启用。将 landingPage
配置选项设置为 true
,以便在生产环境中启用 GraphQL 沙盒。
¥The GraphQL Sandbox is enabled by default in all environments except production. Set the landingPage
configuration option to true
to also enable the GraphQL Sandbox in production environments.
以下是自定义配置示例:
¥The following is an example custom configuration:
- JavaScript
- TypeScript
module.exports = {
graphql: {
config: {
endpoint: '/graphql',
shadowCRUD: true,
landingPage: false, // disable Sandbox everywhere
depthLimit: 7,
amountLimit: 100,
apolloServer: {
tracing: false,
},
},
},
};
export default () => ({
graphql: {
config: {
endpoint: '/graphql',
shadowCRUD: true,
landingPage: false, // disable Sandbox everywhere
depthLimit: 7,
amountLimit: 100,
apolloServer: {
tracing: false,
},
},
},
})
动态启用 Apollo Sandbox
¥Dynamically enable Apollo Sandbox