Strapi创建Content Api自定义GraphQL接口

Strapi创建Content Api自定义GraphQL接口

上一篇文章介绍了使用 Strapi 创建 Content Api 的过程,这里简要写一下生成一个自定义的 GraphQL Content Api。


1.创建 GraphQL Schema

Schema 文件路径/api/movie/config/schema.graphql.js

Schema example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module.exports = {
query: `
moviesCount(where: JSON): Int!
`,
resolver: {
Query: {
moviesCount: {
description: "Return the count of movies",
resolverOf: "application::movie.movie.count",
resolver: async (obj, options, ctx) => {
return await strapi.query("movie").count(options.where || {});
},
},
},
},
};

2.查询

Query moviesCount
1
2
3
{
moviesCount
}

查询结果

注意:如果返回 403Forbidden,需要在设置->角色和权限->Authenticated/Public->APPLICATION->MOVIE->count 选中保存

下一篇文章,写如何创建一个非 Content 相关的 GraphQL 接口。

Strapi创建Content Api自定义GraphQL接口

https://lynan.cn/strapi-cumsom-graphql-content-api/

Author

Lynan

Posted on

2021-02-06

Licensed under