Gatsby: combining two graphql sources on a page (.json and .jpg sources) - json

context
I am turning an old php ecommerce site into a static site built with gatsby.js.
I put all my product metadata into separate .json files (one per product) and I am able to load them with json and file transformer plugins. They are in /items. However each item is related to a set of standard images... eg item-01-main.jpg, item-01-placement.jpg, etc... I put all the images together in /itemphotosand followed the instructions here to load them into graphql: https://www.gatsbyjs.org/packages/gatsby-image/
question
On the page that loads all the products, i have no idea how to incorporate each one's item-xx-main.jpg image: I don't know what graphql query would fetch BOTH sets of data and match/merge them.:
/items
item-01.json
item-02.json
/itemphotos
item-01-main.jpg
item-01-placement.jpg
item-02-main.jpg
item-02-placement.jpg
I have a feeling my directory structure was the wrong approach and maybe I should store all the related images with my product json together in a folder:
/items
item-01/
item-01.json
item-01-main.jpg
item-01-placement.jpg
item-02/
item-02.json
item-02-main.jpg
item-02-placement.jpg
But then how do I source an items/ directory made up of item-xx/ subfolders holding both images and json, as representing a single entity, in graphql?
I did not go with markdown files because I wanted max UI flexibility.

Structure 2
Can you reference your images inside the .json? If so, the 2nd structure could be the winner, since you won't have to do much extra work:
// item-01.json
{
"meta": "...",
"main": "./item-01-main.jpg",
"placement": "./item-01-placement.jpg"
}
Query:
{
json {
id
main {
childImageSharp {
fixed {
src
}
}
}
}
}
Structure 1
If that's not possible or if you'd like to keep the 1st structure, you can try this query:
{
item: allFile(filter: { relativePath: { regex: "/item-01/" } }) {
nodes {
name
extension
children {
... on ImageSharp {
fixed {
src
}
}
... on Item01Json {
id
}
}
}
}
}
This will yield an array containing all files that share the same item id, regardless where they are stored. You can then use extension field to find the json & jpg nodes. It's not pretty, but it also doesn't require that much additional work.
None of the above
If none of that works for you, you could explore adding a image field to the json's graphql schema with createTypes and createResolvers. Add a type definition for the json via createTypes, then use createResolvers to locate the imageSharp node & resolve it.

A more clear cut example can be found from their code repo this page. This uses the same method as #Derek discussed. But if you want an example to understand more.

Related

How to set response code while using json views with Grails 3

I am now in process of switching to use json view in one of my apps built with Grails 3.3
It all looks pretty simple and here is one of my controllers:
def create(ProjectCommand command) {
if (command.validate()) {
// do something with user
Project project = projectService.create(command, springSecurityService.principal.id as Long)
if (project) {
[status: HttpStatus.CREATED, project: project]
} else {
badRequest("failed to create the project")
}
}
else {
badRequest(command.errors)
}
}
Here, I assumed that the status will be used as a response status code, but it does not.
Is there an easy way to set status code of the response without explicitly going through render?
Hmmm... that was easy.
Apparently, inside the view file itself, there is a way to almost anything.
For this particular case, it is enough to do:
response.status HttpStatus.CREATED
I hope it will be useful to someone

JSON Deserialization on Talend

Trying to figuring out how to deserialize this kind of json in talend components :
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
"ryan.buckley#toofr.com": {
"confidence":18,"email":"ryan.buckley#toofr.com","default":16
},
"ryanbuckley#toofr.com": {
"confidence":17,"email":"ryanbuckley#toofr.com","default":17
},
"ryan_buckley#toofr.com": {
"confidence":16,"email":"ryan_buckley#toofr.com","default":18
},
"ryan-buckley#toofr.com": {
"confidence":15,"email":"ryan-buckley#toofr.com","default":19
},
"ryanb#toofr.com": {
"confidence":14,"email":"ryanb#toofr.com","default":14
},
"buckley#toofr.com": {
"confidence":13,"email":"buckley#toofr.com","default":13
}
}
This JSON comes from the Toofr API where documentation can be found here .
Here the actual sitation :
For each line retreived in the database, I call the API and I got this (the first name, the last name and the company change everytime.
Does anyone know how to modify the tExtractJSONField (or use smthing else) to show the results in tLogRow (for each line in the database) ?
Thank you in advance !
EDIT 1:
Here's my tExtractJSONfields :
When using tExtractJSONFields with XPath, you need
1) a valid XPath loop point
2) valid XPath mapping to your structure relative to the loop path
Also, when using XPath with Talend, every value needs a key. The key cannot change if you want to loop over it. Meaning this is invalid:
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
but this structure would be valid:
{
"contact": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"contact": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
So with the correct data the loop point might be /contact.
Then the mapping for Confidence would be confidence (the name from the JSON), the mapping for Email would be email and vice versa for default.
EDIT
JSONPath has a few disadvantages, one of them being you cannot go higher up in the hierarchy. You can try finding out the correct query with jsonpath.com
The loop expression could be $.*. I am not sure if that will satisfy your need, though - it has been a while since I've been using JSONPath in Talend because of the downsides.
I have been ingesting some complex json structures and did this via minimal json libraries, and tjava components within talend.

typesafe config: define global configurations to be used in "sub-configs "

I have a large config fileand I would like to avoid writing duplicates.
example:
shared_part: {
}
cof1 {
. . .
subconf {
shared: shared_part
}
}
cof2 {
. . .
subconf {
shared: shared_part
}
}
I tried to import the shared_parby simply writing it as it is and by ${shared_part}but none worked.
how would be the correct way to do it?
Firstly, I would recommend against big config files if the config blocks are meant to be used in different contexts, as I suppose your conf1 and conf2 do.
Define all your common properties in a sharedPart.conf (the naming is for the sake of your example), and the others in conf1.conf and conf2.conf.
Afterwards, in order to use the common properties within the last two files, add the following to the head of the files:
include "sharedPart.conf"

Loop through JSON to put result in HTML

I'm just starting with JSON and JavaScript and I am having some difficulties parsing JSON result. This is because there is a variable array with within episodes array the JSON that I want to have. So this is an array in an array if I'm right.
Example code:
{
"description":"This is a description",
"banner":"This is a banner",
"episodes":{
"15":[
{
"id":"28685",
"active":1,
"lang":"en",
"link":"http:\/\/link.com\video.php?hd=1"
}
],
"14":[
{
"id":"28577",
"active":1,
"lang":"ru",
"link":"http:\/\/link.com\video.php?hd=1"
}
]};
The "15 and 14" are episode numbers, and id, active, lang, and links are properties from that episode.
So in HTML I want to display that as a group together. Can anyone help me out on this, because I can't find any results on the JQuery page how to get those variable episode number array name.
Maybe to make it a bit more clear. This is my JSON source, and i want to create a video page from it. Where it lists all these episodes with the properties. I'm planning on putting this together with the twitter bootstrap library. The description is the "description from the tv serie" banner is for the banner image, and the list above here are episode 15 and 14. I want to be able to click on such an episodelink. The only think i have difficulties with getting it all apart in different objects: Full json: http://pastie.org/6635498
And it needs to be dynamic this way if the Json updates it also updates the html, that way i can't make static references to "15"
Some quick html i've made to be the target idea: http://pastie.org/6635571
using jquery you can convert your json string (assuming it's a valid json string) to a javascript object like this:
var jsonObj = $.parseJSON('{
"description":"This is a description",
"banner":"This is a banner",
"episodes":{
"15":[
{
"id":"28685",
"active":1,
"lang":"en",
"link":"http:\/\/link.com\video.php?hd=1"
}
],
"14":[
{
"id":"28577",
"active":1,
"lang":"ru",
"link":"http:\/\/link.com\video.php?hd=1"
}
]}');
from here you can access the json data in javascript like this (e.g. for episode 15's id):
var episode15id = jsonObj['episodes']['15']['id'];
then thru javascript magic you can put that into your html:
html:
<div id="jsondatahere" ></div>
js:
$("#jsondatahere").html(episode15id);

JSON structuring a groupchat

I'm going to implement a groupchat into my app by using Firebase. I were thinking of 2 different structures of saving data in JSON.
First structure:
Second structure:
I would like to achieve a fast query and i would like it to parse a small amount of data each time. What structure should i go for and are there maybe a better alternative then these 2?
The first solution is clearly not viable, as you would have a hard time finding all messages belonging to a given group.
The second solution is ok if every time you will query a given group's node, you are going to need also all of its messages, which is probably not what you want.
It is hard, of course, to advise on data structure without more info on your use-case, the queries you are going to make, etc., but a rather standard approach would be:
{
"users": {
"$userId"": {
// user data
}
},
"groups": {
"$groupId": {
// group data
}
},
"group_users": {
"$groupId": {
"$userId": true
// separation of list of users from the group is useful
// if you are going to query the group node not needing its full list of users
}
},
"group_messages": {
"$groupId": {
"$messageId": {
// message data
}
}
}
}