I have two queries, one for fetching automatic discounts and other for fetching code discounts of a shopify store.
So is there a way that I can fetch both the discounts on a single query. Also we are letting users search for their discounts by title, so is there any query that returns data based on discount title because I have seen that in products or collections inside the query we can use:
products(first: 10, query: "title:*searched_text*")
These are my queries:
# for code discounts
query_1 = '''
{
codeDiscountNodes (first:10,query:"status:active"){
edges {
node {
id
codeDiscount {
__typename
... on DiscountCodeBxgy {
status
title
}
... on DiscountCodeBasic {
status
title
}
}
}
}
}
}
'''
# for automatic discounts
query_2 = '''
{
automaticDiscountNodes (first: 10) {
edges {
node {
id
automaticDiscount {
__typename
... on DiscountAutomaticBxgy {
status
title
}
... on DiscountAutomaticBasic {
status
title
}
}
}
}
}
}
'''
Welcome to Stack Overflow!
...is there a way that I can fetch both the discounts on a single query?
Yes, and there are two ways to do this:
First, in GraphQL you can bundle queries together into a single string like so:
{
codeDiscountNodes(first:10) {
edges {
node { id }
}
}
automaticDiscountNodes(first:10) {
edges {
node { id }
}
}
}
Results in:
{
"data": {
"codeDiscountNodes": {
"edges": [
{
"node": { "id": "gid://shopify/DiscountCodeNode/10..." }
}
]
},
"automaticDiscountNodes": {
"edges": [
{
"node": { "id": "gid://shopify/DiscountAutomaticNode/10..." }
}
]
}
}
}
Alternatively, the shopify API provides a general discountNodes endpoint which you can use to request all your discounts together.
{
discountNodes(first:10) {
edges {
node {
id
}
}
}
}
This approach is more efficient than combining the two queries (check out the extensions.cost.actualQueryCost for each of these solutions).
...is there any query that returns data based on discount title?
No, it does not appear like this API allows you to search by title. You can find the list of query filter parameters in the documentation..
Related
For example, through GitHub explorer one can retrieve different types of time line items for a pull request (in this example PULL_REQUEST_COMMIT and PULL_REQUEST_REVIEW):
{
repository(name: "react", owner: "facebook") {
pullRequests(last: 10) {
nodes {
number
timelineItems(last: 10, itemTypes: [PULL_REQUEST_COMMIT, PULL_REQUEST_REVIEW]) {
nodes {
__typename
}
}
}
}
}
}
How can I now access different fields of the types PullRequestEvent or PullRequestReviewEvent? In other words, is there a cast or an if-then-else in GraphQL?
nodes returns an array of PullRequestTimelineItems and a PullRequestTimelineItemsis a GraphQL union type. You can use the ...on notation to query for fields of a specific member in the union type:
{
repository(name: "react", owner: "facebook") {
pullRequests(last: 10) {
nodes {
number
timelineItems(last: 10, itemTypes: [PULL_REQUEST_COMMIT, PULL_REQUEST_REVIEW]) {
nodes {
...on PullRequestReview {
body
}
...on PullRequestCommit {
url
}
}
}
}
}
}
}
I need to get a query using graphql in strapi/gatsby where id is {id}.
According to the documentation found here you query all like so:
{
allStrapiArticle {
edges {
node {
id
title
content
}
}
}
}
This works and I'm able to query however I'd like to get only one Article where id is {id};
I have tried:
{
allStrapiArticle(id: "4") {
edges {
node {
id
title
content
}
}
}
}
And also:
{
allStrapiArticle {
edges {
node(id: "4") {
id
title
content
}
}
}
}
Both of the above give me an error. Any idea how I can achieve this?
Use:
{
allStrapiArticle(filter: {id: {eq: "4" }}) {
edges {
node {
id
title
content
}
}
}
}
elemMatch filter might be useful for your use case as well.
Check the localhost:8000/___graphql playground to test your queries and filters.
More references:
https://www.gatsbyjs.com/docs/query-filters/
https://www.gatsbyjs.com/docs/graphql-reference/
I'm trying to fetch products and their delivery rates from the [deliveryProfiles] object (or shipping profiles). Using the query below, I am able to pull all the products that are associated with a [deliveryProfile] but I can't figure out how to fetch the Delivery Rate for each product.
Our store only uses static shipping rates so I believe I need to use something like the [rateProvider] query below. But I don't know or even how to modified the query to include something like this.
rateProvider {... on DeliveryRateDefinition {id price {amount}
Current query I'm using:
query {
deliveryProfiles (first: 5) {
edges {
node {
profileItems (first: 10) {
edges {
node {
product {
id
handle
}
variants (first: 10) {
edges {
node {
id
title
sku
}
}
}
}
}
}
}
}
}
}
Figured it out. It was simply a matter of understanding nesting. Here is how you fetch both products and delivery rates in Shopify using GraphQL API:
query {
deliveryProfiles (first: 2) {
edges {
node {
profileItems (first: 8) {
edges {
node {
product {
id
handle
}
variants (first: 8) {
edges {
node {
id
title
}
}
}
}
}
}
profileLocationGroups {
locationGroupZones(first: 2) {
edges {
node {
methodDefinitions(first: 2) {
edges {
node {
rateProvider {
... on DeliveryRateDefinition {
id
price {
amount
}
}
... on DeliveryParticipant {
id
fixedFee {
amount
currencyCode
}
percentageOfRateFee
participantServices {
active
name
}
}
}
}
}
}
}
}
}
}
}
}
}
}
I want to display groups of Shopify products based on what collections they're associated with, using gatsby-source-shopify
Filtering to get all products from one collection as easy as running this query:
const { allShopifyCollection } = useStaticQuery(
graphql`
query {
allShopifyCollection(filter: {id: {in: "Shopify__Collection__Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzE3NzAxMjY3MDQ5OA=="}}) {
edges {
node {
products {
title
}
}
}
}
`
)
However it's not possible (to my knowledge) to query multiple times on the same data type in the same component.
What's the preferred way to approach this issue?
Use multiple components that fetches the data for each collection and
pass it to a grid component?
Fetch all collections and filter out each collection?
Another solution?
Can you use query aliases?
const { allShopifyCollection } = useStaticQuery(
graphql`
query {
collection1: allShopifyCollection(filter: {id: {in: "Shopify__Collection__Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzE3NzAxMjY3MDQ5OA=="}}) {
edges {
node {
products {
title
}
}
}
}
collection2: allShopifyCollection(filter: {id: {in: "Shopify__Collection__someOtherCollection"}}) {
edges {
node {
products {
title
}
}
}
}
collection3: allShopifyCollection(filter: {id: {in: "Shopify__Collection__yetAnotherCollection"}}) {
edges {
node {
products {
title
}
}
}
}
`
)
I have the following GraphQL query; "categories" is a one-to-many relationship, each containing a name and a slug. I want to get all Articles with category slug of "derp". Any thoughts?
{
allContentfulArticle(
sort: { order: DESC, fields: [createdAt] }
) {
edges {
node {
title
slug
datePublished
createdAt
categories {
name
slug
}
}
}
}
}
Contentful DevRel here. 👋
I just prototyped your scenario and this query should do it.
query {
categoryCollection(where: { slug_contains: "schnitzel" }) {
items {
linkedFrom {
entryCollection {
items {
...on Article {
title
}
}
}
}
}
}
}
You can find more info in the docs about links to a specific entry.
Hope that helps. :)