IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Strapi创建Content Api自定义GraphQL接口

    Lynan发表于 2023-12-11 06:26:08
    love 0

    上一篇文章介绍了使用 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 接口。



沪ICP备19023445号-2号
友情链接