GraphQL - how to filter a hierarchy? ("customers who ordered gizmos last month") - graphql

Let's assume a type hierarchy of Customer -(hasMany)-> Orders -(hasMany)-> OrderLines
Something like this:
Customer {
Name
Orders [
{
OrderId
Date
OrderLines [
{
ItemCount
ItemName
}
]
}
]
}
I want to query for this whole tree, and filter on properties at any level in the tree.
For instance: Get all customers who ordered 'gizmos'.
This is what I tried: at each level of the hierarchy, I specify optional arguments that would filter based on the properties available at that level:
Customer (Name) {
Name
Orders (OrderId, Date) [
{
OrderId
Date
OrderLines (ItemCount, ItemName) [
{
ItemCount
ItemName
}
]
}
]
}
GraphQL needs me to define how to resolve each type in the hierarchy, so when resolving, I filter based on the arguments in the query.
But what if I only specify a filter at a deep level? e.g. ItemName : 'gizmo'
Assuming there's only one order line in the system containing a gizmo, I would expect to get a response like this:
[{
Name: "cust12",
Orders [{
OrderId: "ade32f",
OrderLines: [{
ItemCount: 50000, //customer really likes gizmos
ItemName: "gizmo"
}]
}]
}]
But what I actually get is all customers (no filter there), all their orders (no filter there) and all order items, mostly empty (the items inside are filtered).
[{
Name: "cust12",
Orders [
{
OrderId: "aaaaaa",
OrderLines: [ ]
},
{
OrderId: "ade32f",
OrderLines: [{
ItemCount: 50000,
ItemName: "gizmo"
}]
},
{
OrderId: "bbbbbb",
OrderLines: [ ]
},
{
OrderId: "cccccc",
OrderLines: [ ]
}
]
},
{
Name: "cust345",
Orders [
{
OrderId: "eeeeee",
OrderLines: [ ]
},
{
OrderId: "ffffff",
OrderLines: [ ]
}
]
}]
GraphQL calls the resolvers top-down:
- get all (filtered) clients
- for each of these get all (filtered) orders
- for each of those get all (filtered) order lines
Because of the top-down nature of calling the resolvers, I get a lot more data than I bargained for.
How should I approach this?

Relation filters
This is actually a more complex topic than it first seems. The problem is that your current filter condition expresses
get all customers, but only include items named 'gizmo'
but what you really want is
get all customers that are related to at least one item named 'gizmo'
get all customers that are related to at least one item named 'gizmo'
An elegant solution for this problem is the addition of relation filters to the schema. In your case, it could look like this:
query {
Customer(filter: {
orders_some: {
orderLines_some: {
item: {
itemName: "gizmo"
}
}
}
}) {
Name
Orders {
OrderId
Date
OrderLines {
ItemCount
ItemName
}
}
}
}
Using
orders_some: {
orderLines_some: {
item: {
itemName: "gizmo"
}
}
}
we only fetch customers that are indirectly related to an item named 'gizmo', exactly what we wanted.
Two more examples:
get all customers that are not related to any item named 'gizmo'
query {
Customer(filter: {
orders_none: {
orderLines_some: {
item: {
itemName: "gizmo"
}
}
}
}) {
Name
Orders {
OrderId
Date
OrderLines {
ItemCount
ItemName
}
}
}
}
get all customers where all their orders contain some order line with an item named 'gizmo'
query {
Customer(filter: {
orders_every: {
orderLines_some: {
item: {
itemName: "gizmo"
}
}
}
}) {
Name
Orders {
OrderId
Date
OrderLines {
ItemCount
ItemName
}
}
}
}
The every, some and none relation filters are an essential part of the Graphcool APIs - you can read more here.

Related

Contentful graphql one too many relationship

I am trying to achieve this: getArticleBySlugWithFilteredTags('tag1', 'tag2', 'tag3') using 1 query ( 1 request ) and avoid clientside filtering ( grab many and filter out with javascript ).
I have content type Article that has an entry type as list: Tag ( another custom content type ).
So there is a one too many relationship: an Article can have multiple Tags.
Now getting back to this: getArticleBySlugWithFilteredTags('tag1', 'tag2', 'tag3').
Attempt using custom content type: Tag
Query:
data: articleCollection(limit: 1, where: {
slug: "article-unique-1",
}) {
items {
title
tagsCollection(limit: 5) { // here it would be nice if I can use "where": {name: "tag1"}
items {
name
value
linkedFrom {
relatedArticles: articleCollection(limit: 7) { // other related articles that has the same tag as parent Article
items {
slug
title
category
}
}
}
}
}
}
}
}
The only thing that is missing here is the that I need to filter out the tagsCollection ( based on some property: name or value ).
I see that I am limited to use "where" on tagsCollection.
Attempt using contentfulMetadata tags
Query
{
data: articleCollection(where:
{
slug: "article-unique-1",
contentfulMetadata: {
tags_exists: true,
tags: {
id_contains_some: ["tag1", "tag2"]
}
}
}) {
items {
contentfulMetadata {
tags {
id
name
linkedFrom { // I can't use this here
relatedArticles: articleCollection(limit: 7) {
items {
slug
title
category
}
}
}
}
}
slug
title
publicationDate
}
}
}
With this approach I am not able to use the linkedFrom in order to get also other related articles that have the same contentfulMetadata tags. What should I do in other to achieve this making 1 query and no clientside filtering with javascript ?

Shopify Admin API, graphQL, create draft order - add products

This is my fist time creating an order. From what I understand you need to create a draft order - add products, price, email, notes etc. I am just creating a test query now to see how it works and it tells me "Add at least 1 product". I am trying to add a product, but I dont know how. I have been messing around and reading and cant figure it out.
This is my query:
mutation draftOrderCreate {
draftOrderCreate(input: {email: "123abc#hotmail.com"}) {
draftOrder {
id
order {
id
}
status
}
userErrors {
field
message
}
}
}
If anyone can give me an example on how to add products to this would be great. Thanks.
You can create an draft order like so:
mutation draftOrderCreate($items: DraftOrderInput!) {
draftOrderCreate(input: $items) {
draftOrder {
id
order {
id
}
status
}
userErrors {
field
message
}
}
}
query variables:
{
"items": {
"email": "123abc#hotmail.com",
"lineItems": [
{
"variantId": "gid://shopify/ProductVariant/32231002996788",
"quantity": 1
}
]
}
}
Or if you don't want to use query variables you can pass the whole object as the input:
mutation draftOrderCreate {
draftOrderCreate(input: {email: "123abc#hotmail.com", lineItems: [{variantId: "gid://shopify/ProductVariant/32231002996788", quantity: 1}]}) {
draftOrder {
id
order {
id
}
status
}
userErrors {
field
message
}
}
}

Dynamically create pages with Gatsby based on many Contentful references

I am currently using Gatsby's collection routes API to create pages for a simple blog with data coming from Contentful.
For example, creating a page for each blogpost category :
-- src/pages/categories/{contentfulBlogPost.category}.js
export const query = graphql`
query categoriesQuery($category: String = "") {
allContentfulBlogPost(filter: { category: { eq: $category } }) {
edges {
node {
title
category
description {
description
}
...
}
}
}
}
...
[React component mapping all blogposts from each category in a list]
...
This is working fine.
But now I would like to have multiple categories per blogpost, so I switched to Contentful's references, many content-type, which allows to have multiple entries for a field :
Now the result of my graphQL query on field category2 is an array of different categories for each blogpost :
Query :
query categoriesQuery {
allContentfulBlogPost {
edges {
node {
category2 {
id
name
slug
}
}
}
}
}
Output :
{
"data": {
"allContentfulBlogPost": {
"edges": [
{
"node": {
"category2": [
{
"id": "75b89e48-a8c9-54fd-9742-cdf70c416b0e",
"name": "Test",
"slug": "test"
},
{
"id": "568r9e48-t1i8-sx4t8-9742-cdf70c4ed789vtu",
"name": "Test2",
"slug": "test-2"
}
]
}
},
{
"node": {
"category2": [
{
"id": "75b89e48-a8c9-54fd-9742-cdf70c416b0e",
"name": "Test",
"slug": "test"
}
]
}
},
...
Now that categories are inside an array, I don't know how to :
write a query variable to filter categories names ;
use the slug field as a route to dynamically create the page.
For blogposts authors I was doing :
query authorsQuery($author__slug: String = "") {
allContentfulBlogPost(filter: { author: { slug: { eq: $author__slug } } }) {
edges {
node {
id
author {
slug
name
}
...
}
...
}
And creating pages with src/pages/authors/{contentfulBlogPost.author__slug}.js
I guess I'll have to use the createPages API instead.
You can achieve the result using the Filesystem API, something like this may work:
src/pages/category/{contentfulBlogPost.category2__name}.js
In this case, it seems that this approach may lead to some caveats, since you may potentially create duplicated pages with the same URL (slug) because the posts can contain multiple and repeated categories.
However, I think it's more succinct to use the createPages API as you said, keeping in mind that you will need to treat the categories to avoid duplicities because they are in a one-to-many relationship.
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
query {
allContentfulBlogPost {
edges {
node {
category2 {
id
name
slug
}
}
}
}
}
`)
let categories= { slugs: [], names: [] };
result.data.allContentfulBlogPost.edges.map(({node}))=> {
let { name, slug } = node.category2;
// make some checks if needed here
categories.slugs.push(slug);
categories.names.push(name);
return new Set(categories.slugs) && new Set(categories.names);
});
categories.slugs.forEach((category, index) => {
let name = categories.names[index];
createPage({
path: `category/${category}`,
component: path.resolve(`./src/templates/your-category-template.js`),
context: {
name
}
});
});
}
The code's quite self-explanatory. Basically you are defining an empty object (categories) that contains two arrays, slugs and names:
let categories= { slugs: [], names: [] };
After that, you only need to loop through the result of the query (result) and push the field values (name, slug, and others if needed) to the previous array, making the needed checks if you want (to avoid pushing empty values, or that matches some regular expression, etc) and return a new Set to remove the duplicates.
Then, you only need to loop through the slugs to create pages using createPage API and pass the needed data via context:
context: {
name
}
Because of redundancy, this is the same than doing:
context: {
name: name
}
So, in your template, you will get the name in pageContext props. Replace it with the slug if needed, depending on your situation and your use case, the approach is exactly the same.

How does GraphQL support AND-OR query conditions?

I'm new to GraphQL. There is a requirement that query all the qualified data from CUSTOMERS table, which meets condition: GENDER == 'MALE' or 'AGE' >= 20. What GQL should looks like for it?
Someone proposed a similar thing before but it is rejected. That means GraphQL does not support it natively and you have to roll it out by yourself.
Several ways to do it based on what I see :
(1) Define your own query language such as what stackoverflow or Shopify does :
type Query{
customers (query:String) : [Customer]
}
The query becomes :
{
customers (query : "GENDER == 'MALE' or 'AGE' >= 20"){
id
name
}
}
(2) Define your own input object models that can cover all the required searching requirement.Prisma try to define one in OpenCRUD specification .You may take a look on it for the idea . For example , you can define an input model like :
input CustomerFilter {
AND : [CustomerFilter]
OR : [CustomerFilter]
# Define the fields that you support to search
gender : String
gender_not : String
....
....
...
...
age : Int
age_gte : Int
}
type Query{
customers (filter:CustomerFilter) : [Customer]
}
And the query becomes :
{
customers (filter : {
OR: [
{ gender : 'MALE' } ,
{ age_gte: 20 }
]
}){
id
name
}
}
This is another filter model for reference. The idea is to tailor-made it such that it is just enough to handle all your application requirements without introducing any unnecessary filtering complexity.
Also , you most probably need to consider something like pagination if it potentially will return many data. It means you have to add an offset and limit to the input arguments for each query to somehow limit the number of record returned if you are doing offset-based pagination or take a look on Relay Specification if you want to do it in the cursor-based pagination style.
You need to define query, schema and resolver. Your Query will be like :
type Query {
nameItAsPerUsecase: Customer
}
Schema will be like these :
type Customer{
name : String
age: Int
.. add more fields as per as your need
}
Your resolver will be like this: -
#Component
public class CustomerQuery implements GraphQLQueryResolver {
#Autowired
private CustomerRepository customerRepository ;
public Customer getNameItAsPerUsecase() {
return customerRepository.findByGenderAndAgeGreaterThanEqual(String gender, int age);
}
}
Nested logic with the same and/or conjunction can be simplified into a single list.
For example, the following complex query:
or: [
{ or: [ { foo: { eq: "A" } }, { bar: { eq: "B" } } ] },
{ or: [ { baz: { eq: "C" } }, { quz: { eq: "D" } } ] }
]
} ) { ... }
Moreover, can be simplified into the following simplified query syntax:
queryPost(filter: {
or: [
{ foo: { eq: "A" } },
{ bar: { eq: "B" } },
{ baz: { eq: "C" } },
{ quz: { eq: "D" } }
]
} ) { ... }

Add or delete from connection depending on response

Essentially, I have users who can have payments, and theses payments can be filtered with an arg.
Here is my schema, simplified :
type User {
payments($filter: PaymentsFilter): PaymentsConnection,
}
enum PaymentsFilter {
MissingDetails,
}
type PaymentsConnection {
edges { ... }
pageInfo { ... }
}
type Payment {
id
description
}
The MissingDetails filter returns only the Payment who are missing a description.
For example, if I have 2 Payment :
[
{ id: 1, description: null },
{ id: 2, description: 'A great payment' },
]
A query as such :
query {
loggedUser {
payments(filter: MissingDetails) {
...
}
}
}
Would return only the first Payment, with id: 1.
I want to achieve an UpdatePaymentMutation, that would update the payment and depending on if the description is set or not in the response, I would RANGE_ADD it to the connection with the filter MissingDetails, or RANGE_DELETE it.
How can I achieve that ?

Resources