Using slug as a filter with Graphql and Strapi V4 - graphql

Trying to use slug as an uuid to fetch single pages from a strapi backend v4.
my query to get pages works well. It's just I can't filter using slug:
my pages query:
query {
pages {
data {
attributes {
Name
slug
}
}
}
}
how to get single page using slug uuid within this schema ? every trails to implement ($slug: String!) fails as where to locate slug.

You can use filters. Just pass slug as argument and use the 'eq' keyword for comparison which stands for equals.
query pages($slug:String) {
pages(filters:{slug:{eq:$slug}}) {
data {
attributes {
Name
slug
}
}
}
}

Try this
query pages($id: ID) {
page(id: $id) {
data {
attributes {
name
slug
}
}
}
}

base on this blog, from strapi, a resolver must be coded
I Found a Blog for strapi v4

Related

How to get the same object properties from different items in Graphql

I'm using Contentful and it's Graphql. I have a collection, that has reference to different content types, like: page, landing, format, static page etc. The content types are different, but has some overlap. I want to get name and slug from each entry, but I want to know if I can do this without
...on Page {
name
slug
}
...on Landing {
name
slug
}
...on Static_Contante {
name
slug
}
etc.
is there something like:
...on GenericCommonFields {
name
slug
}
I read about fragments, but I don't see if and how I should use them here. Should I just repeat the code, or is there better way?
You can solve it with fragments. First is to create fragment with all common item's parts like that:
fragment commonCollectionItems on SectionBlocksItem {
...on Page {
name
slug
}
...on Landing {
name
slug
}
...on Static_Contante {
name
slug
}
}
And use it in your collection like:
someCollection {
items {
...commonCollectionItems
... on Page { #example
seoUrl
seoTitle
}
}
}

How can I query my data by published date (newest first) using Apollo and Graphql?

I have React page that is fetching data via Graphql.
I'm using Strapi as backend and queries are already generated.
I´m querying my data in the frontend like this
query GetData{
datas(limit:3){
id
published_at
}
}
In the documentation I found this example about how to sort my queries by some especific order
GET /users?_sort=email:ASC,dateField:DESC
but is not really clear how to use it with the query structure.
I tried something like this and other variations
query GetPodcasts{
podcasts?_sort=published_at:DESC(limit:3){
id
published_at
}
}
but it didn't work.
I may need some help understanding this.
In a forum some nice people also gave me an answer.
When using graphql in frontend and want to sort or filter the data, just have to use "" to specify the sort or filter.
In my case it just had to be:
query GetData{
datas(limit:3, sort:"published_at:desc"){
id
published_at
}
}
Graphql is like an Rest Api but with one end point to the server ,
this query with name GetData , point to datas query but you should define this query in the backend.
please watch some tutorials that will guide you step by step.
you can learn more about graphql here
https://graphql.org/learn/
try this:
query GetData{
datas(input:{
sort:{id:"asc"},
data:{limit:3}
}){
id
published_at
}
}
for more info
https://strapi.io/documentation/developer-docs/latest/development/plugins/graphql.html#query-api
This seem to work for me in Strapi v4:
sort: "publishedAt:DESC"
...
query GetVideos($page: Int!, $pageSize: Int!) {
videos(
pagination: { page: $page, pageSize: $pageSize }
sort: "publishedAt:DESC"
) {
__typename
data {
__typename
id
attributes {
__typename
createdAt
publishedAt
}
}
}
}
}
`

GraphQL: Use returned value from field as parameter of sub-query [duplicate]

Imagine the following query:
query {
user {
id
}
SomeOtherStuff(id: <--- I want to pass the id obtained from user) {
id
}
}
How do you pass a parameter obtained from one query to another ?
In GraphQL, fields at each "level" of the request are executed and resolved in parallel. In your example, user and SomeOtherStuff are both fields of the same type (the root Query type) -- so they will be resolved at the same time. That means each query essentially is not aware of the other or what the other resolved to.
You would have to handle this kind of scenario client side. In other words, request the user first, parse the response for the id and then make the second request.
Edit: In Apollo, you would utilize compose for this purpose:
const userQuery = gql`query User { user { id } }`;
const stuffQuery = gql`query SomeOtherStuff($id: ID) { someOtherStuff(id: $id){ stuff } }`;
export default compose(
graphql(userQuery, { name: 'userData' })
graphql(stuffQuery, { name: 'stuffData', options: ({userData:{id}={}}) => ({variables: {id}}) }),
)(YourComponent)
I agree with #DanielRearden. You should make type-resolvers so you can go infinitely deep into the graph. I made a simple server example here that shows deep relationships. Because all the noun-fields are references, it goes infinitely deep for any query.
With that server, you can run a query like this, for example:
{
hero {
name
friends {
name
friends {
name
friends {
name
friends: {
name
}
}
}
}
}
}
So, in your example, structure it like this:
query {
user {
id
otherStuff {
id
}
}
}
I was looking for same scenario and landed on this question. You can get it work other way around. It all depends how you have written your graphql resolver and you need to make sure that your database relations are intact. I have got it working like this.

Wordpress Gatsby - How do i access ACF relationship content in graphql, or otherwise

Using gatsby-source-wordpress I have a content type called 'student' which has a ACF relationship field that links to content type 'posts'. I want to show the related posts (title and other fields) on the student page.
On a student post i can get various fields of the related content, wordpress_id seems most useful (below is an example of the ACF relationship field on a 'student' set to show 'wordpress_id's:
{
"node": {
"acf": {
"related_projects": [
64,
88,
1
]
}
}
}
What i'd like to do is construct a graphQL query on the student page the selects all posts that match enter code herea set of wordpress_ids. I can see how to match a single id, but not multiple. Or can a single graphQL query be looped over on a page?
If you're using the ACF to REST API plugin on your WordPress site, then you can query related posts added via an ACF Relationships field.
Here is a GraphQL query example using a Flexible Content layout field called Related Content and a ACF relationship field called Related Posts:
{
allWordpressPost {
edges {
node {
title
acf {
content_post {
__typename
... on WordPressAcf_related_content {
related_posts {
post_title
post_content
}
}
}
}
}
}
}
}

Gatsby.js: Filter GraphQL query by nested object property

I'm working on a news site which will have articles, each with one or multiple respective authors. If you click on an author's name, it will take you to a page displaying their information and a list of articles they have contributed to.
So each article has an authors property, which in turn is an array of author objects with properties: full name, slug (lowercase version of full name with spaces replaced by dashes), etc.
Is it possible to filter the articles by a particular author's slug when defining the query?
query authorQuery($slug: String!) {
allContentfulArticle(filter: { //not sure what to do here }) {
edges {
node {
title
slug
authors {
name
slug
}
}
}
}
}
My other option would be to load all of the articles, then setup a filter in the component like so:
const articles = data.allContentfulArticle.edges.filter(({ node }) => {
return node.authors.some((author) => author.slug === data.contentfulAuthor.slug);
});
This wouldn't be the end of the world, however it goes against the GraphQL principle of only loading the data you need.
What you want to achieve here if I understood correctly, you want to group articles by author.
You can achieve that if you query and apply filter to allContentfulAuthor
and request the article field, like so:
{
allContentfulAuthor(filter: {slug: {eq: "myslug"}}) {
edges {
node {
name
article {
title
slug
}
}
}
}
}
Note that the article name is your contentTypeId for your articles.

Resources