How do you perform operations on variables in GraphQL? - graphql

I am a traditional programmer new to GraphQL and I can't seem to find documentation on what I consider the basics, aka manipulating variables. Note: I am using GraphQL with Shopify(Admin API), through an app GraphiQL, so that my effect syntax and capabilities.
Here is a piece of hypo (& broken) code where I have two iterations of the same code block
<>that tries to add two variables
<>on that sum items in a list. The specific code in this lines are guesswork..
If anyone has suggestions on sources for example code or API docs beyond GraphQL site, I have been searching and nothing I have found addresses this type of functionality.
query fiveorTenOrders($n: Int = 5,$m: list =[5,5], $boo: Boolean = true) {
FiveOrds: orders(first: $n) {
edges #include(if: $boo) {
node {
...ordrecs
}
}
}
#<<<HERE and as basic arithmetic>>>
TenOrds: orders(first: ($n+$n) {
edges #skip(if: $boo) {
node {
...ordrecs
#<<<OR HERE ...as a sum of list>>>
TenOrds: orders(first: $m:SUM {
edges #skip(if: $boo) {
node {
...ordrecs
}
}
}
}
fragment ordrecs on Order {
id
name
createdAt
shippingAddress {
id
city
provinceCode
zip
}
}

GraphQL does not currently support this sort of functionality (and may never). Variables are used as-is with no way to apply arbitrary transformations to them. In your example, you would need to sum the values yourself on the client and then inject them into the query as a separate variable.

Related

Filter nested array in graphQL

I am trying to filter an array of quotes based on what work or event the quote is referencing.
I only want the quotes referencing the current work the user is visiting. I've tried filtering, but I can't get it to work.
query MyQuery {
allSanityQuote {
edges {
node {
work {
... on SanityEvent {
_id
}
}
}
}
}
}
I want the _id in SanityEvent to match wphb0cG6N3lm4RQzm1xf51
Is SanityEvent a so called union? I've tried to read the graphQL documentation, but can't seem to find any example of this.

Github Graphql Filter issues which has multiple labels

This query returns those issues which has either label1 or label2. Basically, It returns all the issues of label1 and label2.
{
repository(owner: "another-user", name: "another-repo") {
issues(first: 100, filterBy: { labels: ["label1", "label2"}"] }) {
nodes {
title
body
bodyHTML
bodyText
number
labels(first: 100) {
nodes {
color
name
id
}
}
author {
url
avatarUrl
login
}
}
}
}
}
I was trying to find a specific issue which have both label1 and label2 labels. How can I execute AND operation?
I often see areas where "filtering" an endpoint falls short and the answer is always to use the search node. You can use the search API, but if you really like GraphQL there's a search endpoint there, too.
https://docs.github.com/en/graphql/reference/queries#search
query {
search(query="repo:another-user/another-repo+label:label1+label:label2) {
nodes {
//work with nodes as usual
__typename
... on Issue {
author {
}
createdAt
}
}
}
}
Search Query Syntax
I find the documentation a little lacking here because they do not explain the + syntax used in #rbennett485's query, but you can infer that it is the opposite of the - (exclusion) to mean it must include the search criteria (label/repo/etc).
Understanding the search syntax
GraphQL Inline Fragments
Because the search node returns a "Union" (SearchItemResultItem) type we need to be able to switch on the particular object returned.
This is where GraphQL Inline Fragments come into play. In my example I "switch" based on the __typename of the returned node. __typename is known as a "Meta Field" and will be available in any spec compliant GraphQL server (GraphQL: October 2021 Edition:4.4 Type Name Introspection). For more see Introspection.
It's not possible to do with the graphql API other than by returning all the issues with those labels then filtering client side (docs https://docs.github.com/en/graphql/reference/input-objects#issuefilters).
You can do it with the search API instead though https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests.
Haven't tested but I think something like this should do the job
q=label:label1+label:label2+repo:another-user/another-repo

How to perform a nested query in graphql using gatsby-source-prismic

I'm just getting started with gatsby and graphql and I've started using prismic as a cms. I cannot figure out how to perform nested queries, avoiding overfetching. I don't even know if it would be possible or if it's more just me thinking of the problem in SQL terms.
I have two custom types on prismic which are related using a content relationship. These are Productions which have many People through a repeatable group of fields. The result I want is to have a page (the home) which shows the latest production with the list of people who starred in it, and another page with each person from people and all their roles in every production.
I managed to do this by fetching all the people in the home page and the required production and filtering the returned data in the frontend via javascript. However, I really feel that this way is not ideal since it requires to fetch all the people and not only the ones required for my page.
allPrismicProduction(
limit: 1
sort: { fields: [last_publication_date], order: DESC }
) {
edges {
node {
data {
casting {
...castingData
}
}
}
}
}
allPrismicPerson {
edges {
node {
id
data {
name {
text
}
photo {
url
}
}
}
}
}
}
const productions = data.allPrismicProduction.edges
const people = data.allPrismicPerson.edges
const sortedprods = []
productions.forEach(el => {
let castings = el.node.data.casting.map(cast => cast.person.uid)
castings.forEach(casting =>{
people.filter(person => {
if(castings.indexOf(person.node.uid) > -1){
return person
}
sortedprods.push({
production: el.node,
people: relpeople,
})
})
})
So what I do is I fetch all people and then filter them according to the uids found in the productions returned by the query.
I would like to know if it is possible, or otherwise what would be a better way to achieve this, how to limit overfetching by making it possible to only fetch the people who's uid is present in the productions given by the first part of the query.
Is this way of thinking compatible with graphql?
I managed to solve this by looking around a bit more in the other issues of gatsby-source-prismic on github.
The related-content node can be queried using the following structure:
{
allPrismicMyContentType {
edges {
node {
data {
my_relations {
relation {
document {
data {
where in data you can access all the properties of the type needed.
In this way one can do a single query to get all related content on prismic

Filter empty nodes on GitHub GraphQL API

I'm trying to use the V4 API of GITHUB to get a list of my assigned issues along with its labels and references.
After some time I got the query you can see below, which works exactly how I want.
There is however, a problem: it includes a lot of empty nodes I am not interested at. For example, if I want to get all the CrossReferencedEvent that are issues I will get a lot of empty nodes on the timeline edges array because the other events: LabeledEvent, ReferencedEvent, AssignedEvent etc.
How can I filter out those so I only get the events I am interested at?
Is this a limitation of graphql? Am I forced to remove the useless nodes locally?
This is the query that I have currently
{
search(query: "assignee:danielo515", type: ISSUE, last: 100) {
edges {
node {
... on Issue {
number
title
state
timeline(first: 10) {
edges {
node {
... on CrossReferencedEvent {
source{
... on Issue {
title
number
}
}
}
}
}
}
labels(last: 10) {
nodes {
name
color
}
}
repository {
name
}
}
}
}
}
}
One improvement I can make is, on the query part add is:issue. This will . save me all the empty nodes at the root edges array, but I don't see how to do the same for the nested timeline.
Thanks in advance

How can I do a WpGraphQL query with a where clause?

This works fine
query QryTopics {
topics {
nodes {
name
topicId
count
}
}
}
But I want a filtered result. I'm new to graphql but I see a param on this collection called 'where', after 'first', 'last', 'after' etc... How can I use that? Its type is 'RootTopicsTermArgs' which is likely something autogenerated from my schema. It has fields, one of which is 'childless' of Boolean. What I'm trying to do, is return only topics (a custom taxonomy in Wordpress) which have posts tagged with them. Basically it prevents me from doing this on the client.
data.data.topics.nodes.filter(n => n.count !== null)
Can anyone direct me to a good example of using where args with a collection? I have tried every permutation of syntax I could think of. Inlcuding
topics(where:childless:true)
topics(where: childless: 'true')
topics(where: new RootTopicsTermArgs())
etc...
Obviously those are all wrong.
If a custom taxonomy, such as Topics, is registered to "show_in_graphql" and is part of your Schema you can query using arguments like so:
query Topics {
topics(where: {childless: true}) {
edges {
node {
id
name
}
}
}
}
Additionally, you could use a static query combined with variables, like so:
query Topics($where:RootTopicsTermArgs!) {
topics(where:$where) {
edges {
node {
id
name
}
}
}
}
$variables = {
"where": {
"childless": true
}
};
One thing I would recommend is using a GraphiQL IDE, such as https://github.com/skevy/graphiql-app, which will help with validating your queries by providing hints as you type, and visual indicators of invalid queries.
You can see an example of using arguments to query terms here: https://playground.wpgraphql.com/#/connections-and-arguments

Resources