shorten the graphql field value - graphql

I am trying to fetch image path in an alias field using graphql, I am able to get output like this:
Output:
{
"data": {
"leaders": [
{
"partyImg": {
"image": {
"url": "/uploads/17a5f020cc974679ac52e56a22b74dd6.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/70bd673d41654058847e39c14cda5fef.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/c54a0ace0bb34da3985c67945b1d0bf0.png"
}
}
}
]
}
}
When I used the following graphql code:
Input:
query Leaders{
leaders{
partyImg: party{image: Image{ url }},
}
}
The output I am trying to get is:
Expected output:
{
"data": {
"leaders": [
{
"partyImg": "/uploads/17a5f020cc974679ac52e56a22b74dd6.png"
},
{
"partyImg": "/uploads/70bd673d41654058847e39c14cda5fef.png"
},
{
"partyImg": "/uploads/c54a0ace0bb34da3985c67945b1d0bf0.png"
}
]
}
}
Please help me to prepare the graphql input which could generate the expected output.

It appears the graphql schema prevents you from getting the data in the format that you want. Based on your input query, I expect the schema looks something like this:
query {
leaders: [Leader]
}
type Leader {
party: Party
}
type Party {
Image: Image
}
type Image {
url: String
}
To get the data in the format that you want, you would need a schema that looks more like:
query {
leaders: [Leader]
}
type Leader {
party: Party,
imageUrl: String
}
Then you could do:
query Leaders {
leaders {
partyImg: imageUrl
}
}
I assume you don't control the schema, so you would have to do post processing. If you are using javascript, the following could work for the above output as a simple mapping exercise.
(function() {
var output = {
"data": {
"leaders": [{
"partyImg": {
"image": {
"url": "/uploads/17a5f020cc974679ac52e56a22b74dd6.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/70bd673d41654058847e39c14cda5fef.png"
}
}
},
{
"partyImg": {
"image": {
"url": "/uploads/c54a0ace0bb34da3985c67945b1d0bf0.png"
}
}
}
]
}
};
var transformed = {
data: {
leaders: output.data.leaders.map(function flattenUrl(item) {
return {
partyImg: item.partyImg.image.url
};
})
}
}
document.getElementById('transformedOutput').innerHTML = JSON.stringify(transformed);
}());
<div id="transformedOutput"></div>
If you are the author of this graphql schema, you can structure it in whatever way makes the most sense to your applications and/or consumers.

Related

Why is my graphql query return all product?

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" ]}) {
...
}
}
}

How to filter GraphQL query?

I'm new to GraphQL and got stuck in a query. Well, this is sample GQL that returns every record in database:
{
viewer {
savingsAccount {
id
feed {
id
__typename
title
detail
postDate
... on TransferInEvent {
amount
originAccount {
name
}
}
... on TransferOutEvent {
amount
destinationAccount {
name
}
}
... on TransferOutReversalEvent {
amount
}
... on BillPaymentEvent {
amount
}
... on DebitPurchaseEvent {
amount
}
... on BarcodePaymentEvent {
amount
}
... on DebitWithdrawalFeeEvent {
amount
}
... on DebitWithdrawalEvent {
amount
}
}
}
}
}
Here is sample return:
{
"data": {
"viewer": {
"savingsAccount": {
"id": "5c8fe258-34fd-4bdc-bfb6-761bc78435df",
"feed": [{
"id": "5fcd6040-cf19-4dbf-be25-47d2f1a7a29b",
"__typename": "DebitPurchaseEvent",
"title": "Test",
"detail": "Test $$",
"postDate": "2020-12-06",
"amount": 59.7
}, {
"id": "5fcd5bbd-fc70-4d87-944d-d0a186559289",
"__typename": "DebitPurchaseEvent",
"title": "Test",
"detail": "Test $$",
"postDate": "2020-12-06",
"amount": 30.0
}
}
}
}
}
How do I apply filter to return some specific postDate? I mean, after, before, etc. This attribute is stored as string but I believe it is possible to do some convertion.
Thanks in advance!

How to get only the filtered data from Prisma / GraphQL query

I have 2 sets of data (users and events) in many-to-many relation.
I am using the following query to retrieve and filter the data.
{
events(
where: {
AND: [
{ location: { name: "Test" } }
{
time: {
startDate_lt: "2018-12-03T13:46:13.021Z"
endDate_gt: "2018-12-03T13:46:13.021Z"
}
}
{
participantList_some: {
participant: { firstName: "Lorem", lastName: "Ipsum" }
}
}
]
}
) {
participantList {
participant {
firstName
lastName
}
}
location {
name
}
}
}
So far so good, I'm getting the following outcome:
{
"data": {
"events": [
{
"participantList": [
{
"participant": {
"firstName": "Chuck",
"lastName": "Norris"
}
},
{
"participant": {
"firstName": "Lorem",
"lastName": "Ipsum"
}
}
],
"location": {
"name": "Test"
}
}
]
}
}
What I'd like to have would be to get only the participant that I've filtered, ie. "Lorem Ipsum". This way I'm getting all the (2) participants from that event.
So my desired outcome would be:
{
"data": {
"events": [
{
"participantList": [
{
"participant": {
"firstName": "Lorem",
"lastName": "Ipsum"
}
}
],
"location": {
"name": "Test"
}
}
]
}
}
At the moment I'm filtering out the unwanted data from code. I've searched how or if I can do this with a query or additional parameters but didn't find something useful. Any help or guide is appreciated.
You can add a filter to any field of the request. Which means you can filter the participants in your found events:
{
events(
where: {
AND: [
{ location: { name: "Test" } }
{
time: {
startDate_lt: "2018-12-03T13:46:13.021Z"
endDate_gt: "2018-12-03T13:46:13.021Z"
}
}
{
participantList_some: {
participant: { firstName: "Lorem", lastName: "Ipsum" }
}
}
]
}
) {
participantList (where: { participant: { firstName: "Lorem", lastName: "Ipsum" } }) {
participant {
firstName
lastName
}
}
location {
name
}
}
}

prismic graphql querying single user

I'm trying to figure out how to query a single user from graphql schema by id. I'm using the graphiql tool and I'm able to get all Users.
{
allPrismicUsers {
edges {
node {
id
data {
name
}
}
}
}
}
Outputs :
{
"data": {
"allPrismicUsers”: {
"edges": [
{
"node": {
"id": "Prismic__User__WzKFwywAABmiZk_4",
"data": {
"name": “John Doe”
}
}
},
{
"node": {
"id": "Prismic__User__WzKDZywAABmiZkYp",
"data": {
"name": “Jane Doe“
}
}
},
{
"node": {
"id": "Prismic__User__WzKGDiwAAJSiZlFL",
"data": {
"name": “Cat Doe”
}
}
}
]
}
}
}
I also have prismicUser() on the schema
query {
prismicUser {
id
data {
name
}
}
}
Output:
{
"data": {
"prismicUser": {
"id": "Prismic__User__WzKGDiwAAJSiZlFL",
"data": {
"name": "Cat Doe"
}
}
}
}
I'm trying to query a user based on a specific id but not sure if I'm querying the wrong way.
I tried this.
query {
prismicLocation(id: "Prismic__User__WzKDZywAABmiZkYp") {
data {
name
}
}
}
I get an error
{ "errors": [
{
"message": "Argument \"id\" has invalid value \"Prismic__User__WzKDZywAABmiZkYp\".\nExpected
\"prismicUserIdQueryString_2\", found not an object.",
"users": [
{
"line": 25,
"column": 23
}
]
} ] }
How can I call a specific user based on their id ?
According to the Gatsby GraphQL reference, Gatsby allows you to filter results by any field in GraphQL (using operators such as eq, ne, etc.)
The gatsby-source-prismic plugin provides a field called prismicId (i.e. W1syKSIAAAzdN1Jg).
Here is an example query:
{
prismicUser(prismicId:{eq:"WzKDZywAABmiZkYp"}) {
data {
name
}
}
}
But you can also query by id:
prismicUser(id:{eq:"Prismic__User__WzKDZywAABmiZkYp"})

RestHeart passing multiple parametrs in aggregation

Basically I am trying to use REST hreat to create two variables issuerId and sectionName. I am trying to get data from the URI mentioned below
http://ftc-wbpyrdb201:8080/statdata/InsStatData/_aggrs/getDataByIssuerAndSectionName?avars={"issuerId":19038},{"sectionName":"ASSETS"}
My aggregation definition is a below
{"$push" : {
"aggrs":
{
"type": "pipeline",
"uri": "getDataByIssuerAndSectionName",
"stages": [
{
"_$match": {
"_$and": [
{"issuerId": {"_$var": "issuerId" }},
{
"sectionName": {"_$var": "sectionName"}
}
]
}
},
{
"_$unwind": "$sections"
},
{
"_$unwind": "$sections.data"
},
{
"_$unwind": "$sections.data.values"
},
{
"_$match": {
"_$and": [
{"issuerId": {"_$var": "issuerId" }},
{
"sectionName": {"_$var": "sectionName"}
}
]
}
}
]
}
}
}
I am sure I am doing something wring in either defining the aggregation or while requesting the URI. There is not enough in the documentation. Please help me with this request.

Resources