This is GraphQL query from the gatsby project:
const Data = useStaticQuery(graphql`
query {
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 5
) {
edges {
node {
excerpt(pruneLength: 300)
fields {
slug
}
frontmatter {
date(formatString: "DD [<span>] MMM [</span>]")
title
description
tags
}
}
}
group(field: frontmatter___tags) {
totalCount
fieldValue
}
}
}
`);
const Posts = Data.allMarkdownRemark.edges;
const Tags = Data.allMarkdownRemark.group;
The above code is used to show the latest 5 posts and all the tags (including count of each tag). The problem is after showing 5 latest posts it only shows tag count of 5.
If I apply limit of 2000 to the group section as shown below, I get error while doing gatsby develop.
group(field: frontmatter___tags, limit: 2000)
Here is my GraphQL IDE output for the query:
{
allMarkdownRemark(sort: {fields: id}) {
group(field: frontmatter___tags) {
totalCount
fieldValue
}
}
}
output:
{
"data": {
"allMarkdownRemark": {
"group": [
{
"totalCount": 27,
"fieldValue": "android"
},
{
"totalCount": 24,
"fieldValue": "c"
},
{
"totalCount": 42,
"fieldValue": "chash"
},
{
"totalCount": 31,
"fieldValue": "cplus"
},
{
"totalCount": 36,
"fieldValue": "css"
},
{
"totalCount": 22,
"fieldValue": "html"
},
{
"totalCount": 24,
"fieldValue": "html5"
},
{
"totalCount": 30,
"fieldValue": "java"
},
{
"totalCount": 11,
"fieldValue": "jsp"
},
{
"totalCount": 18,
"fieldValue": "mysql"
},
{
"totalCount": 37,
"fieldValue": "networking"
},
{
"totalCount": 33,
"fieldValue": "php"
},
{
"totalCount": 8,
"fieldValue": "xml"
}
]
}
},
"extensions": {}
}
Somehow the limit of 5 for latest posts is getting applied to the count of tags to be displayed.
In that case, run multiple queries by giving each one an alias.
Here is an example:
const Data = useStaticQuery(graphql`
query {
posts: allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 5
) {
edges {
node {
excerpt(pruneLength: 300)
fields {
slug
}
frontmatter {
date(formatString: "DD [<span>] MMM [</span>]")
title
description
tags
}
}
}
}
tags: allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
group(field: frontmatter___tags) {
totalCount
fieldValue
}
}
}
`);
const Posts = Data.posts.edges;
const Tags = Data.tags.group;
Related
Running the following query, I get two results:
query x($keyword: String = "Pulp", $limit: Int, $nexttoken: String) {
listProducts(filter: {or: [{name: {contains: $keyword}}, {description: {contains: $keyword}}]}, limit: $limit, nextToken: $nexttoken) {
items {
id
}
nextToken
}
}
Response:
{
"data": {
"listProducts": {
"items": [
{
"id": "ba8048f3-1099-4184-8e78-f0e57b521465"
},
{
"id": "335770ad-f566-4385-a861-7d0dab6c9dd8"
}
],
"nextToken": null
}
}
}
By setting $limit: Int=1, I get an empty array of items:
{
"data": {
"listProducts": {
"items": [],
"nextToken": "xxxxxx"
}
}
}
By setting $limit: Int=2, I get one item:
{
"data": {
"listProducts": {
"items": [
{
"id": "ba8048f3-1099-4184-8e78-f0e57b521465"
}
],
"nextToken": "xxxxxx"
}
}
}
In order to get all 2 two items, I need to set $limit=4. Why?
I followed the Gatsby tutorial and ended up with a GraphQL like:
query {
allMdx(sort: {fields: frontmatter___date, order: DESC}) {
nodes {
frontmatter {
date(formatString: "MMMM D, YYYY")
title
}
id
slug
}
}
}
This gives me the expected result of:
{
"data": {
"allMdx": {
"nodes": [
{
"frontmatter": {
"date": "January 25, 2022",
"title": "Image Blog"
},
"id": "cc11f4c4-43a5-573a-9ea6-3806f7e2fc64",
"slug": "image-blog/"
},
{
"frontmatter": {
"date": "July 24, 2021",
"title": "Page B"
},
"id": "1fb36e34-d1f4-5ac2-8c18-1030733cfce1",
"slug": "b"
},
{
"frontmatter": {
"date": "July 24, 2021",
"title": "Page C"
},
"id": "2f0ed709-c7ab-5a3f-9846-1340699b4bd1",
"slug": "c"
},
{
"frontmatter": {
"date": "July 24, 2021",
"title": "Sub Page D"
},
"id": "d9cc842d-dc45-508a-8d5d-8dc1f2b0b232",
"slug": "sub/d"
},
{
"frontmatter": {
"date": "July 24, 2021",
"title": "Sub Sub Page E"
},
"id": "2005bd62-37a7-580b-9950-e1b7c3688c0b",
"slug": "sub/subsub/e"
},
{
"frontmatter": {
"date": "July 23, 2021",
"title": "Page A"
},
"id": "58dd9cf4-8e20-5602-87ef-5eb18cd74636",
"slug": "a"
}
]
}
},
"extensions": {}
}
As you can see the result contains all pages.
What I would like to achieve is to have an "index" page that only show links to the pages within the same directory of the "index" page.
How can I modify the query to return only the current directory's pages?
EDIT:
I made some progress by changing the query to:
query{
allMdx(
sort: {fields: frontmatter___date, order: DESC}
filter: {slug: {regex: "/^sub/[^/]+$/"}}
) {
nodes {
frontmatter {
date(formatString: "D MMMM YYYY")
title
}
id
slug
}
}
}
which gives the result of:
{
"data": {
"allMdx": {
"nodes": [
{
"frontmatter": {
"date": "24 July 2021",
"title": "Sub Page D"
},
"id": "4de57b7f-5e87-520f-ab06-940fb6f91340",
"slug": "sub/d"
}
]
}
},
"extensions": {}
}
But my graphQL is hard-coded. I still do not understand how to pass in a variable dynamically so that it would give different results depending on which slug/directory the page was loaded, for example to have something like:
filter: {slug: {regex: $expression}}
This is the full page code I have so far:
import * as React from 'react'
import { Link, graphql } from 'gatsby'
import Layout from '../../components/layout'
const BlogPage = ({ data, location }) => {
//const slug = location.pathname;
return (
<Layout pageTitle="My Blog Posts">
{
data.allMdx.nodes.map(node => (
<article key={node.id}>
<h2>
<Link to={`/blog/${node.slug}`}>
{node.frontmatter.title}
</Link>
</h2>
<p>Posted: {node.frontmatter.date}</p>
</article>
))
}
</Layout>
)
}
export const query = graphql`
query{
allMdx(
sort: {fields: frontmatter___date, order: DESC}
filter: {slug: {regex: "/^sub/[^/]+$/"}}
) {
nodes {
frontmatter {
date(formatString: "D MMMM YYYY")
title
}
id
slug
}
}
}
`
export default BlogPage
Your query is fetching all data from the MDX (hence the allMdx node), set in the gatsby-source-filesystem so it's getting all title and date from frontmatter, without filtering.
One easy thing that is the straightforward workaround in this scenarios of filtering desired pages is adding a key field in your MDX files so you can filter your query using that field:
query {
allMdx(
filter: {frontmatter: { key: { eq: "index-page"}}}
sort: {fields: frontmatter___date, order: DESC}) {
nodes {
frontmatter {
date(formatString: "MMMM D, YYYY")
title
}
id
slug
}
}
}
So you will be only fetching one-page data.
I have the following query that can be run against the github graphql API
query userRepositories($cursor: String, $q: String!, $githubId: String!) {
search(query: $q, type: REPOSITORY, first: 100, after: $cursor) {
repositoryCount
pageInfo {
endCursor
startCursor
}
nodes {
... on Repository {
id
name
description
isArchived
isPrivate
nameWithOwner
url
defaultBranchRef {
target {
... on Commit {
history(first: 10, author: {id: $githubId}) {
totalCount
}
}
}
}
}
}
}
}
It returns results like this:
{
"data": {
"search": {
"repositoryCount": 103,
"pageInfo": {
"endCursor": "Y3Vyc29yOjEwMA==",
"startCursor": "Y3Vyc29yOjE="
},
"nodes": [
{
"id": "MDEwOlJlcG9zaXRvcnk2MTg1OTczOA==",
"name": "microstates",
"nameWithOwner": "thefrontside/microstates",
"url": "https://github.com/thefrontside/microstates",
"defaultBranchRef": {
"target": {
"history": {
"totalCount": 0
}
}
},
{
"id": "MDEwOlJlcG9zaXRvcnkxNTU5MTUyODc=",
"name": "effection",
"nameWithOwner": "thefrontside/effection",
"url": "https://github.com/thefrontside/effection",
"defaultBranchRef": {
"target": {
"history": {
"totalCount": 16
}
}
}
},
I am only interested in the nodes array that has a defaultBranchRef.target.history.totalCount that is greater than 0.
So I am not interested in element 0 of the nodes array but I am of element 1.
Can I filter this in graphql or do I have to do that in code outside of the query?
GraphQL can't filter an array so if the API support filter base on totalCount you can pass this filter otherwise you have to filter in your code. In JS it's very easy:
const filtered = {
...data,
search: {
...data.search,
nodes: data.search.nodes.filter(node => node.defaultBranchRef.target.history.totalCount > 0),
}
};
I'm testing graphql query on Shopify GraphiQL Explorer for filtering products on Storefront API.
My query is like this:
query ProductType {
collectionByHandle(handle: "pants") {
handle
products(first: 10, filters: {
productType: "pants"
}) {
edges {
node {
handle
productType
}
}
}
}
}
And got the result like this:
{
"data": {
"collectionByHandle": {
"handle": "pants",
"products": {
"edges": [
{
"node": {
"handle": "my-test-product-pants",
"productType": "pants"
}
},
{
"node": {
"handle": "pleated-straight-pants-mens-fashion-elastic-waist-casual-pants-men-streetwear-loose-ice-silk-trousers-mens-wide-leg-pants-s-2xl",
"productType": ""
}
},
...
Basically, the result has all the products that I have for that collection. Can anybody help me with this? This is literally the code I got from shopify website
As we can see in the tutorial filters is an array
{
"product_filters": [
{
"productType": "shoes"
},
{
"productVendor": "bestshop"
},
{
"variantOption": {
"name": "color",
"value": "blue"
}
}
]
}
So, try this instead
query ProductType {
collectionByHandle(handle: "pants") {
handle
products(first:10, filters: [{ productType: "pants" ]}) {
...
}
}
}
Now I am testing the Shopify graphql for get customer by email.
But Shopify Customer query does not work correctly. query is following.
{
customers(first: 10, query:"email:'test#gmail.com'") {
edges {
node {
id
lastName
verifiedEmail
firstName
lastName
email
phone
tags
}
}
}
}
but the result is following.
{
"data": {
"customers": {
"edges": []
}
},
"extensions": {
"cost": {
"requestedQueryCost": 252,
"actualQueryCost": 2,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 998,
"restoreRate": 50
}
}
}
}
Shopify Store have that customer. But the query didn't find customer.
I am sure we can solve this problem.
If you have experience one, please help me.
Thanks.
Not sure if you still need an answer. The string after query: needs to be just the email. see here:
query GetThat {
customers(first: 10, query: "mikeyboy#aol.com"){
edges{
node {
firstName
lastName
defaultAddress {
id
address1
}
}
}
}
}
Results:
{
"data": {
"customers": {
"edges": [
{
"node": {
"firstName": "MikeyBoy",
"lastName": "ohohoh",
"defaultAddress": {
"id": "gid://shopify/MailingAddress/7969224163540?model_name=CustomerAddress",
"address1": "123 sesame st"
}
}
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 22,
"actualQueryCost": 4,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 996,
"restoreRate": 50
}
}
}
}