Querying multiple data of a array containing same properties via graphql - reactjs

I wish to nested query the array via graphql, by elimination repeated properties. Below is the json file
{
"MAIN_ARRAY": [
{
"One": [
{
"title": "Title",
"description": "Description",
"avatar": "../../assets/image/author-1.jpg"
}
],
"Two": [
{
"title": "Title",
"description": "Description",
"avatar": "../../assets/image/author-1.jpg"
}
]
}
]
}
I dont want to repeat the properties title, description, avatar for One and Two because its the same. Is there any workaround for this to avoid repeating it. Below code didnt work.
query {
fileJson {
MAIN_ARRAY {
One, Two {
title
description
avatar
}
}
}
}

Assuming the underlying types of both One & Two are the same, you can use Fragments
query {
fileJson {
MAIN_ARRAY {
One {
...MyFragment
}
Two {
...MyFragment
}
}
}
}
fragment MyFragment on MyType {
title
description
avatar
}

Related

How to store nested structure in graph database

I am analyzing how to store nested/hierarchical structure in graph database. I want to store like a tree where settings vertex will have two children DigitalInput and Input2 and like in subsequent parameters. Any inputs for which approach I should choose and how?
"properties": {
"A": {
"value": "prop1 new value"
},
"settings": {
"DigitalInput": {
"Input1": {
"nTransIn1": {
"tagName": {
"value": ""
}
}
},
"Input2": {
"nTransIn2": {
"tagName": {
"value": ""
}
}
}
}

How to avoid specifying every leaf attribute in graphql query with gatsby

I uploaded some json to my graphql using gatsby-transformer-json and gatsby-source-filesystem.
I would like to request an object of that data, which contains subfields.
When I try to make this request, I receive the error
Field \"sections\" of type \"DataJsonSections\" must have a selection of subfields. Did you mean \"sections { ... }\"?
Below is my json file. I am trying to request the section called "Sections". I would really like to find a way, so that I don't have to specify spiel and abbr for every section.
My json file
{
"slogan": "some slogan",
"drawIn": "Something to draw people in",
"fullName": "My full title",
"shortName": "My title",
"metadata": {
"desc": "...........",
"title": "............"
},
"sections": {
"about": {
"spiel": "..........."
},
"web-design": {
"abbr": "Websites",
"spiel": ".............."
},
"app-design": {
"abbr": "Apps",
"spiel": "..........."
},
"seo": {
"spiel": ".........."
},
"contact": {
"title": "Contact",
"subtitle": "Contact subtitle",
}
}
}
This is the request I would like to make, but I receive the error above when I do
query MyQuery {
allDataJson {
edges {
node {
sections
}
}
}
}
Extra
Within my gatsby-config.js(This is how I'm using gatsby-transformer-json and gatsby-source-filesystem)
`gatsby-transformer-json`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `data`,
path: `${__dirname}/src/data`
}
},
Field "sections" of type "DataJsonSections" must have a selection of subfields

How to show NULL in graphql result if the field is empty

I'm using Strapi as cms for my frontend page in Gatsby.
In Strapi I have a content-type Pages. The Pages content-type is many to many relations to all other content types.
Thus I have content types as follows:
Pages
Footers
Headers
For query as follows:
query MyQuery {
allStrapiPage {
edges {
node {
footers {
name
}
headers {
name
}
}
}
}
}
I'm getting all results as I should get.
Thus, the result is as follows:
{
"data": {
"allStrapiPage": {
"edges": [
{
"node": {
"footers": [
{
"name": "Footer ONE"
}
]
}
},
{
"node": {
"headers": [
{
"name": "Header one"
}
]
}
},
]
}
}
}
But if I do not have any records in footers or headers content type for the query above I get an error as follows:
"Cannot query field \"footers\" on type \"StrapiPage\".
That is not what I want. How can I get null on the field that not exists? Thus if footers content type is empty, I expect result as follows:
{
"data": {
"allStrapiPage": {
"edges": [
{
"node": {
"footers": null
}
},
{
"node": {
"headers": [
{
"name": "Header one"
}
]
}
},
]
}
}
}
How can I do that?
Thanks in advance.
I just joined and can't comment on posts yet so I'll put this in the answer section. Did you check that in your schema you defined footers and headers as nullable fields? That could be causing your error

Elasticsearch daterange using two date fields

I send a query from the client for docs where the currentDate is between two values attached to all docs. The docs have an expireDate and an availableDate.
How do I formulate this query? As a guess I came up with the following, which doesn't work:
{
"constant_score": {
"filter": {
"range" : {
"expiredate" : {
"lte": "2015-08-15T11:28:45.114-07:00"
},
"availabledate" : {
"gte": "2015-08-11T11:28:45.114-07:00"
}
}
}
}
}
This even looks wrong because afaik the range won't work on a compound object or an array of one-sided ranges. I guess I could add an entire new filter so one for each field. Is there a way to use one date range for two fields?
The correct way of expressing that query is using two range filters (one for each date field) and place them in another bool/must filter.
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"range": {
"expireDate": {
"lte": "2015-08-15T11:28:45.114-07:00"
}
}
},
{
"range": {
"availabledate": {
"gte": "2015-08-11T11:28:45.114-07:00"
}
}
}
]
}
}
}
}
}

Checking if field exists under an elasticsearch nested aggregation

Trying to perform an ES query, I ran into a problem while trying to do a nested filtering of objects in an array. Our structure of data has changed from being:
"_index": "events_2015-07-08",
"_type": "my_type",
"_source":{
...
...
"custom_data":{
"className:"....."
}
}
to:
"_index": "events_2015-07-08",
"_type": "my_type",
"_source":{
...
...
"custom_data":[ //THIS CHANGED FROM AN OBJECT TO AN ARRAY OF OBJECTS
{
"key":".....",
"val":"....."
},
{
"key":".....",
"val":"....."
}
]
}
this nested filter works fine on indices that have the new data structure:
{
"nested": {
"path": "custom_data",
"filter": {
"bool": {
"must": [
{
"term":
{
"custom_data.key": "className"
}
},
{
"term": {
"custom_data.val": "SOME_VALUE"
}
}
]
}
},
"_cache": true
}
}
However, it fails when going over indices that have the older data structure, so that feature cannot be added. Ideally I'd be able to find both data structures but at this point i'd settle for a "graceful failure" i.e. just don't return results where the structure is old.
I have tried adding an "exists" filter on the field "custom_data.key", and an "exists" within "not" on the field "custom_data.className", but I keep getting "SearchParseException[[events_2015-07-01][0]: from[-1],size[-1]: Parse Failure [Failed to parse source"
There is an indices filter (and query) that you can use to perform conditional filters (and queries) based on the index that it is running against.
{
"query" : {
"filtered" : {
"filter" : {
"indices" : {
"indices" : ["old-index-1", "old-index-2"],
"filter" : {
"term" : {
"className" : "SOME_VALUE"
}
},
"no_match_filter" : {
"nested" : { ... }
}
}
}
}
}
}
Using this, you should be able to transition off of the old mapping and onto the new mapping.

Resources