How to get the same object properties from different items in Graphql - 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
}
}
}

Related

Using slug as a filter with Graphql and Strapi V4

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

WPGraphQL/GraphQL custom post type filter

I have created custom post type with 2 taxonomies, which I want to filter first by country taxonomy and then by practice area taxonomy. The problem is that I'm not allowed (can't find how to do it in docs) to do filtering in one step. The goal is to achieve all post types "experience" from "experience_countries" - "estonia" taxonomy, and that have "practice_area_category" - "all" taxonomy.
So graphQL schema allows me to do like this:
What this query returns:
Instead of my goal, I'm getting post types for all countries (estonia and latvia, instead of only estonia) that match "practice_area_category" - "all" taxonomy.
I would greatly appreciate, any advice!
From what I understand you want to filter posts by experienceCountries then further filter with experiencePracticeAreas.
You don't need two separate custom taxonomies, the first custom taxonomy is enough "experienceCountries".
In WordPress do the following:
In experienceCountries taxonomy/category you have "Lativa", "Estonia" ...etc
In your custom post type go to the experienceCountries category and add a new name for example "all" or "town name" whatever and from the dropdown option set parent category "estonia"
So that you have "estonia" as a parent and "all" as a child
Your GraphQL Schema:
experienceCountries(where: {slug: "estonia"}) {
nodes {
children(where: {slug: "all"}) {
nodes {
experiences {
nodes {
title
}
}
}
}
}
}
I have a custom post type call books and taxonomy call author. In order to filter books by author I use this plugin:
https://github.com/wp-graphql/wp-graphql-tax-query
And my query is like this one.
{
books(
where: {
taxQuery: {
taxArray: {
taxonomy: AUTHORTAG,
operator: AND,
terms: "john-doe",
field: SLUG
}
}
}
) {
nodes {
databaseId
title
authorTags {
nodes {
databaseId
slug
}
}
}
}
}
I think it could work with your custom post type and both taxonomies. You need to filter in the taxQuery with multiple taxonomy values using AND operator

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.

GraphQL: Filter data in an array

I'm sure it's a simple thing to do, but I couldn't find anything in either GraphQL's doc or Graphcool's.
Say I have an entity with this schema (new GraphQL user, sorry if I make mistake in the schema representation):
Book {
name: String!
author: String!
categories: [String!]
}
How would I do a query for all books that are part of the "mystery" category? I know I can filter with allBooks(filter: {}), but categories_in: ["mystery"] and categories_contains: "mystery" didn't do the trick.
Category model
Thinking a bit more about this situation, creating a Category model is definitely the way to go.
For example, imagine you want to allow readers to subscribe to their favorite categories later. Or, what if you want a list of all existing categories? Using string lists, you would need to query all books and somehow postprocess all obtained categories. Handling this on a model level rather than using string lists feels much more natural.
Instead, you can create a new Category model and add a many-to-many relation between Category and Book. In situations like this, I like to add a unique enum field tag and a string field text. (A unique string field tag alone would also be suitable, probably a matter of taste.)
With this setup, you can easily fulfill data requirements like
Which books are assigned to a given category?
query {
# query books by unique category tag
Category(tag: MYSTERY) {
books {
id
}
}
# query books by specific category text
Category(filter: {
text: "mystery"
}) {
books {
id
}
}
}
Which books are assigned to at least one category of a given list?
query {
allCategories(filter: {
OR: [{
tag: MYSTERY
}, {
tag: MAGIC
}]
}) {
books {
id
}
}
}
Which books are assigned to all categories of a given list?
query {
allCategories(filter: {
AND: [{
tag: MYSTERY
}, {
tag: MAGIC
}]
}) {
books {
id
}
}
}
Related filters
Even though the above queries fulfill the specified data requirements, books are grouped by Category in the response, meaning that we would have to flatten the groups on the client.
With so called related filters, we can turn that around to only obtain books based on conditions defined its related categories.
For example, to query books assigned to at least one category of a given list:
query {
allBooks(filter: {
OR: [{
categories_some: {
tag: MYSTERY
},
categories_some: {
tag: MAGIC
}
}]
}) {
id
}
}
If you are interested in using a hosted GraphQL service, scaphold.io has had this feature for a while now. All connection fields in your API come with a WhereArgs argument that exposes filters that let you really dig into your data. When you have a list of scalars like this, the WhereArgs include a contains & notContains field that allow you to filter results based off the values in your list. This allows you to make a query like this.
query MysteriousBooks($where:BookWhereArgs) {
viewer {
allBooks(where:$where) {
edges { node { title, ... } }
}
}
}
# Variables
{
"where": {
"categories": {
"contains": "mystery"
}
}
}
Just to be complete, you could also do a slight schema readjustment to make this work without having to filter on a scalar list. For example, you could make Category a node implementing type and then create a connection between Category and Book. Although a Book will likely not have many categories, this would allow you to issue a query like this:
query MysteriousBooks($where: CategoryWhereArgs) {
viewer {
allCategories(where: $where) {
books {
edges { node { title, ... } }
}
}
}
}
# Variables
{
"where": {
"name": {
"eq": "mystery"
}
}
}
If you structure your schema this way then you would also be able to do more filtering on the books in the category without having to loop through every book in your archive. E.G. you could efficiently ask for "all the mystery books written in the last year."
Full disclosure: I work at Scaphold and although I'd love you to try it out no hard feelings if you don't switch over. I'm excited to see people trying and loving GraphQL. If you're curious about how to implement this type of behavior on your own server let me know and I'd be happy to help there as well!
I hope this helps!

Resources