Contentful GraphQL 端点:如何检索内容类型的所有条目

Contentful GraphQL endpoint: how to retrieve all entries of a content type

{
  Post {
      name
  }
}

在尝试检索内容类型的所有条目时,它只给出错误:

"Argument \"id\" of required type \"String!\" was not provided."

因为 id 字段是必需的。那么我如何获得一个内容类型的所有条目?

参考:https://www.contentful.com/developers/docs/references/graphql/

来自文档 here:

The produced Query object exposes two fields that you can use to query content of that type: one to fetch individual content documents (friendlyUser in the example) and another to do queries over all the content of the type (friendlyUserCollection).

因此,对于要检索其所有条目的任何资源,您需要在其 id 末尾附加 Collection,然后使用 items 字段检索所有条目。如:

{
  PostCollection {
      items {
          name
      }
  }
}

除了文档,您还可以在此处查看相应 GraphiQL 实例的所有可用资源,这可能非常有用:

https://graphql.contentful.com/content/v1/spaces/{SPACE_ID}/explore?access_token={ACCESS_TOKEN}

搜索或 select Query 查看所有模式:

查询单个id

我想你可以在 GraphQL playgound 上试试这个 http://localhost:8000/___graphql

query PostById($id: String!) {
  contentfulPost(id: { eq: $id }) {
    name
  }
}

并添加一个查询变量

{
  "id": "my-awesome-id"
}

查询全部Posts

How do I get all entries of a content type then?

在 GraphQL playground 上,你应该可以做这样的事情

{
  allContentfulPost {
    nodes {
      name
    }
  }
}