如何使用Strapi在GraphQL中添加自定义查询?

问题描述 投票:1回答:1

我使用Strapi作为我的CMS,在React中使用GraphQL来查询MongoDB数据库。我正在使用Apollo来处理GraphQL查询。我可以通过传递一个ID参数来获取对象,但我希望能够传递不同的参数,比如名字。

这可以工作。

{
  course(id: "5eb4821d20c80654609a2e0c") {            
    name
    description
    modules {
       title
    } 
  }
}

这行不通,会出现 "Unknown argument \"name\"on field \"course\"type \"Query\"的错误。

{
  course(name: "course1") {            
    name
    description
    modules {
       title
    } 
  }
}

根据我所读到的内容,我需要定义一个自定义查询,但我不知道如何做到这一点。

Course的模型目前是这样的。

  "kind": "collectionType",
  "collectionName": "courses",
  "info": {
    "name": "Course"
  },
  "options": {
    "increments": true,
    "timestamps": true
  },
  "attributes": {
    "name": {
      "type": "string",
      "unique": true
    },
    "description": {
      "type": "richtext"
    },
    "banner": {
      "collection": "file",
      "via": "related",
      "allowedTypes": [
        "images",
        "files",
        "videos"
      ],
      "plugin": "upload",
      "required": false
    },
    "published": {
      "type": "date"
    },
    "modules": {
      "collection": "module"
    },
    "title": {
      "type": "string"
    }
  }
}

和任何帮助将是感激的。

mongodb graphql strapi
1个回答
0
投票

引用Strapi GraphQL 查询API

您可以使用 where 与查询 courses 来过滤您的字段。您将得到一个课程列表,而不是一个课程。

这应该可以。

{
  courses(where: { name: "course1" }) {
    name
    description
    modules {
       title
    } 
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.