我们如何使用Strapi CMS访问动态添加的组件字段(即JSON字段)?

huangapple go评论66阅读模式
英文:

How can we access the dynamically added component fields (i.e., JSON fields) using Strapi CMS?

问题

上下文:

我有一个自定义组件(自动完成带有来自外部 API 的选项列表),允许我根据 API 请求选择选项。在选择之后,选项的主体部分变成了结构的一部分,看起来像这样(其中 'type' 是自定义组件的字段):

Grapql 响应:

{
  "data": {
    "newsCollection": {
      "data": [
        {
          "id": "23",
          "attributes": {
            "type": [
              {
                "id": "DSDSD",
                "name": "DSADASDASD",
                "name2": "DSADASDASD"
              },
              {
                "id": "DSDSD3",
                "name": "DSADASDASD",
                "name2": "DSADASDASD"
              }
            ]
          }
        }
      ]
    }
  }
}

当我尝试通过 GraphQL 请求有选择地获取此组件的字段,或者按嵌套字段(name,name2,id)进行过滤时,我遇到了一个问题,即 GraphQL 不知道这些字段存在,因为它将该组件识别为 JSON。

示例:

query Test(
    $filters: NewsFiltersInput
    $pagination: PaginationArg = {}
    $sort: [String] = []
    $publicationState: PublicationState = LIVE
){
 newsCollection(
      filters: $filters
      pagination: $pagination
      sort: $sort
      publicationState: $publicationState
    ) {
  data {
    id
    attributes {
     type {
       ..... I can't identify fields here 😞
     }
    }
  }
}
}

在 Strapi 中有解决这个问题的方法吗?🙏

我尝试在 ./src/index.js 文件中以这种方式覆盖模式:

module.exports = {
  /**
   * 在初始化应用程序之前运行的异步注册函数。
   *
   * 这为您提供了扩展代码的机会。
   */
  register({ strapi }) {
    const extensionService = strapi.plugin("graphql").service("extension");

    const extension = () => ({
      typeDefs: `
        type Type {
           name: String!
         }

          type ObetaTeaser {
            type: [Type]!
          }
      `,
    });
    extensionService.use(extension);
  },

  bootstrap(/*{ strapi }*/) {},
};

但我们不能覆盖,只能扩展现有类型以添加新字段😞

如果 Strapi 能够定义自定义字段的自定义类型,而不仅仅是 JSON 或字符串,那将是很棒的。

英文:

Context:

I have a custom component ( autocomplete with list of options from external api ) that allows me to select an option based on an API request. After the selection, the body of the option becomes part of the structure and looks like this (where ‘type’ is a field of the custom component):

Grapql response:

{
  "data": {
    "newsCollection": {
      "data": [
        {
          "id": "23",
          "attributes": {
            "type": [
              {
                "id": "DSDSD",
                "name": "DSADASDASD",
                "name2": "DSADASDASD",
              },
              {
                "id": "DSDSD3",
                "name": "DSADASDASD",
                "name2": "DSADASDASD",
              }
            ]
          }
        }
      ]
    }
  }
}

When I try to make a GraphQL request to selectively get the fields of this component, or to filter by nested fields ( name, name2, id ), I run into a problem that GraphQL doesn’t know these fields exist because it identifies the component as JSON.

Example:

query Test(
    $filters: NewsFiltersInput
    $pagination: PaginationArg = {}
    $sort: [String] = []
    $publicationState: PublicationState = LIVE
){
 newsCollection(
      filters: $filters
      pagination: $pagination
      sort: $sort
      publicationState: $publicationState
    ) {
  data {
    id
    attributes {
     type {
       ..... I can't identify fields here 😞
     }
    }
  }
}
}

Is there a way to solve this problem in Strapi? :pray:

I tried to override schema such way in ./src/index.js file

module.exports = {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register({ strapi }) {
    const extensionService = strapi.plugin("graphql").service("extension");

    const extension = () => ({
      typeDefs: `
        type Type {
           name: String!
         }

          type ObetaTeaser {
            type: [Type]!
          }
      `,
    });
    extensionService.use(extension);
  },

  bootstrap(/*{ strapi }*/) {},
};

but we cann't override, but only extend existing type with new fields(

It would be great if strapi has ability to define custom type for custom fields but not JSON or string only

答案1

得分: 0

问题是通过使用自定义解析器解决的,该解析器创建了字段的副本并描述了其类型。

英文:

The problem is solved using a custom resolver that creates a duplicate of the field and describes its type.

huangapple
  • 本文由 发表于 2023年8月4日 01:38:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定