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

{
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/

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:

Query a single id
I think you can try this in the GraphQL playgound
http://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?
On the GraphQL playground, you should be able to do something like this
{
allContentfulPost {
nodes {
name
}
}
}

Related

How to resolve problem with gatsby-source-graphql

I tried to connect gatsby-source-graphql with my gatsby JS project. But I have error
Cannot query field "alladventures" on type "Query".
This is my gatsby-config code with my API:
https://api.services.activelyme.com/graphql/playground
resolve: `gatsby-source-graphql`,
options: {
// This type will contain remote schema Query type
typeName: `ALLADVENTURES`,
// This is the field under which it's accessible
fieldName: `alladventures`,
// URL to query from
url: `https://api.services.activelyme.com/graphql`,
},
},
This is my component code
graphql`
query {
alladventures {
adventures {
title
}
}
site {
siteMetadata {
title
description
author
norwayItems {
item {
status {
category
date
season
}
title
location
userName
description1
dates
show {
regular_price
promotional_price
}
rating {
stars
review
adventures
}
mainImage
guideImage
link
}
}
}
}
}
`
)
Schema must contain uniquely named types but contains multiple types named "Query".
Error: Schema must contain uniquely named types but contains multiple types named "Query".
I have to show adventures. But I get error each time.
If I use this example with this API everything works very well
https://www.gatsbyjs.org/docs/third-party-graphql/

Nested query in GraphQL

I'm trying to learn GraphQL and I think I may be misunderstanding something. I want to be able to given a brand and a page, get some information back. I also need to be able to query for a brand and get all pages. Any thoughts on what I'm doing wrong?
query {
base(brand: "somebrand") {
brand
pages(route: "/") {
...
}
}
}
So i wrote a schema like
type Page {
name: String
route: String
}
type Base {
brand: String
pages: [Page]
}
type Query {
base(brand: String): Base
pages(route: String): Page
}
I get an error like Unknown argument "route" on field "pages" of type "base"

How can I filter by uid of a linked document / relationship using the prismic graphql api?

I am trying to list a set of articles by their categories, by uid, and I'm assuming that I would have to use the where query, but I'm not able to get that to worked on linked documents.
The issue seems to be that where only accepts a string on a field, but in the case of a linked document you would need to dig down to the uid field.
I'm not sure if I'm using the wrong query, but struggling to find anything in the documentation to help me out.
I tried digging into the category object:
{
allDirectoryServices(
where: { category: { _meta: { uid: "developers" } } }
) {
edges {
node {
name
city
region
country
category {
...on DirectoryTaxonomy {
_meta {
uid
}
name
}
}
}
}
}
}
But that returns an error that it's expecting a string:
"message": "Expected type String, found {_meta: {uid: \"developers\"}}.",
{
allDirectoryServices(
where: { category: "developers"}
) {
edges {
node {
name
city
region
country
category {
...on DirectoryTaxonomy {
_meta {
uid
}
name
}
}
}
}
}
}
This returns no results, obviously.
I asked this question on the Prismic Slack group too, and got the answer from them:
In order to query by a Content Relationship / Link field like this, you need to use the document ID.
where: { category: "WBsLfioAABNUo9Kk" }
Unfortunately it isn’t possible to query by the UID (or any other field).
I imagine they will be updating their documentation soonish, as this isn't covered by it.
Thanks to the Prismic guys!

GraphQL non nested relation

I'm trying to have a representation of nodes on GraphQL more akin to what jsonapi would be like http://jsonapi.org/
What I mean is if we take one of the examples on GraphQL
{
hero {
name
# Queries can have comments!
friends {
name
}
}
}
Have a representation that would be more along these lines
{
hero {
name
# Queries can have comments!
friends {
id
}
},
friends {
id, name
}
}
Is that at all possible in GraphQL
Thanks
It is possible, and there's nothing wrong with having a friends field. In GraphQL terms you can have the following part of the schema:
type User {
id: ID
name: String
firends: [User]
}
type RootQuery {
hero: User
friends(forUserId: ID!): [User]
}
And then you can query this as you like – you can ask for friends separately:
{
friends(forUserId: "12") {
id, name
}
}
But the whole idea of GraphQL is that you don't have to do multiple queries to get the information you need. If you just need a list of users – that's a reasonable query, that most people have (with arguments for pagination and so on). With that said, there's no reason to fetch a list of IDs and to send another fetch query for the data right after that.

GraphQL arguments to connection sub results

I'm need help passing arguments to collections/connections/arrays in GraphQL syntax.
I'm just learning it, playing with the SWAPI at http://graphql.org/swapi-graphql/
I can pass an id argument to a single type, like this:
query getANewHope {
film(id: "ZmlsbXM6MQ==") {
id
title
}
}
But I don't know how to query the results of a collection/connection
query starships {
allStarships(id: "c3RhcnNoaXBzOjI=") { # this doesn't work
edges {
node(id: "c3RhcnNoaXBzOjI=") { # ...nor this.
id
}
}
}
}
I want to query collections because, I'd like to connect the two ideas like "All Starfighter type ships in A New Hope"?
query filmStarships {
film(id: "ZmlsbXM6MQ==") {
title
starshipConnection { #How to limit this? I can't use (starshipClass: "Starfighter") here...
edges {
node { # ...nor here..
starshipClass # ...nor here.
}
}
}
}
}
query starships2 {
starship (id: "c3RhcnNoaXBzOjI=") { # This doesn't work either
id # even without an arugment abovce, it says "Unknown argument \"id\" on field \"node\" of type \"StarshipsEdge\"."
}
}
Arguments like you're asking for have to be implemented in the schema. The SWAPI does does not expose an argument to filter by starshipClass. Click Docs on the top right of the GraphiQL window to explore the provided schema.
If you are implementing your own schema it would be very easy to add a filter on starshipClass in the resolvers.

Resources