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

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


3

{
  Post {
      name
  }
}

While trying to retrieve all the entries on a content type, it only gives error of:

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

Since id field is required. How do I get all entries of a content type then?

Ref: https://www.contentful.com/developers/docs/references/graphql/

3 Answers
3


5

From docs 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).

So for any resource that you want to retrieve all entries of, you need to append Collection at the end of its id, then use items field to retrieve all entries. As in:

{
  PostCollection {
      items {
          name
      }
  }
}

Apart from docs, you can also view all available resources at corresponding GraphiQL instance here, which could be pretty useful:

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

Search or select Query to see all schemas:

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

0


0

Having agreed that the right way to filter Post’s is to query for a PostCollection, there are a few parameters that may come handy:

{
  PostCollection(limit: 30) {
      items {
          name
          text
      }
  }
}

Having an idea of the size of the collection, this one limits the back-end resources that the query will allocate.
By default the size is 100 and if the collection contains only a few elements, not specifying the limit is a waste of resources.

{
  PostCollection(where: { topic: "politics" }) {
      items {
          name
          text
      }
  }
}

This one will retrieve only the posts with a certain topic field (obviously, the field must be present in the content type and be filled in the contents).

There are a lot of other possibilities.


-1

Query a single id

I think you can try this in the GraphQL playgound
https://localhost:8000/___graphql

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

and add a QUERY VARIABLE

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

Query all the Posts

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

As already mentioned in the accepter answer, in the GraphQL playground app, you shall be able to do the following:

{
  PostCollection {
    items {
      name
    }
  }
}

1

  • Your answer regarding the query variable is off-topic, because the OP is not interested in runtime filtering. The answer about retrieving all the posts is completely wrong. Correcting it along the lines of the accepted answer.

    – Marco Faustinelli

    1 hour ago




Leave a Reply

Your email address will not be published. Required fields are marked *