I am using drupal 8 to build a headless page with gatsby and graphql.
How to query a translation of a page?
query MyQuery {
allNodePage {
nodes {
body {
value
}
}
}
}
I want to get this allNodePage.nodes.body.value in a specific language.
Edit:
Here is the the graphiql allNodePage entity.
Edit_1:
According to this issue you can only query translations by modifying the api host-route (/fr/jsonapi/node/page) in drupal 8.
But in gatsby-source-drupal-plugin the api host route is not changeable in runtime.
Edit_2:
I try to translate a page node. Default language is english. I want to request the german translation which exists in drupal.
After some experimentation I see it's not working yet with Drupal JSON API, but I got it working with the Drupal GraphQL module. This can be used with Gatsby and gatsby-source-graphql instead of gatsby-source-drupal. The query would look something like this:
{
nodeQuery(
filter: {
conditions:[
{operator: IN, field: "type", value: ["my_node_type"]},
{operator: EQUAL, field: "langcode", value: "de"},
]
}
) {
entities {
...on NodeMyNodeType {
entityTranslation(language: DE) {
entityLabel
}
}
}
}
}
If you want to filter allNodePage by langcode you can do it like this:
query MyQuery {
allNodePage(filter: {langcode: {eq: "fr_FR"}}) {
nodes {
body {
value
}
}
}
}
Of course, you can use a variable in place of "fr_FR" instead of hardcoding it
query MyQuery($lang: String!) {
allNodePage(filter: {link: {eq: $lang:}}) {
nodes {
body {
value
}
}
}
}
Related
I want to add links to some related entities and collection, without all property data in one response, only the Link.
For unterstanding I structure the Question in some parts
Simple example of data Model
Order
Order->AddressFrom (Entity)
Order->PackageItems (Collection)
Which is the wright HAETOAS way to generate links or how to name entities in response.
First question: How to link address, double in entity name and links part and how to nam?
{
"_embedded":{
"orders":[
{
"id":"id",
"addressLinkFrom":{
"href":"link"
},
"_links":{
"self":{
"href":"link"
},
"addressFrom":{
"href":"d"
}
}
}
]
}
}
or
"addressFrom":{
"href":"link"
},
"_links":{
...
}
or
or
"addressFrom":{
"self":{
"href":"link"
}
},
"_links":{
...
}
Second: How to link collection to specific and not all entities
{
"_embedded":{
"orders":[
{
"id":"id",
"packageItemIds":[
{
"href":"link"
},
{
"href":"link"
}
]
}
]
}
}
or
"packageItemIds":[
{
"self:"{
"href":"link"
}
}]
Third: How is the right format for (update, post), which I have to parse, like the address, where I'm sending the ID or like the packageItems, where I sending the link.
{
"id":"id",
"addressFrom":{
"id":"12345"
}
"packageItems":[
{
"href":"link"
},
{
"href":"link"
}
]
}
For your information, as frontend Client I'm using lagoshny /
ngx-hateoas-client,
when someone has their extra information beside the HAETOAS standard, how is the best way, please tell me.
Thanks for your help.I want to add links to some related entities and collection, without all data in one response.
I have a reusable block and I am trying to return it's dynamicContent using the following query. I am using the blocks id
{
reusableBlock(id: "10061756", idType: DATABASE_ID) {
blocks {
dynamicContent
saveContent
}
}
}
I am just getting null back no matter what I try
{
"data": {
"reusableBlock": null
},
"extensions": {
"debug": []
}
}
If I query a page where the block is, it works fine.
Is my query wrong or what am I missing?
I'm trying to change the result that apollo returns with the data on the client side. I've read in the docs of version 3 that maybe this is possible. I currently have a graphql query that returns data within an EDGES array, and then each item is in a "node" before you get to the actual data.
The query looks like:
export const QUERY_ALL_POSTS = gql`
query AllPosts {
posts(first: 20) {
__typename
edges {
__typename
node {
__typename
author {
node {
avatar {
height
url
width
}
id
name
slug
}
}
id
categories {
edges {
node {
databaseId
id
name
slug
}
}
}
content
date
excerpt
featuredImage {
node {
altText
caption
sourceUrl
srcSet
sizes
id
}
}
modified
databaseId
title
slug
isSticky
}
}
}
}
`;
And the response looks like:
I know that I can write the typePolicy this way to change the date string for example:
Post:{
fields:{
date(){
return 'date edit'
}
}
},
So I wanted to see if I could change the response of the entire array that gets returned to something like this because I'll be needing to check for edges and nodes in every component which is kinda of annoying:
posts:[
{...item 1},
{...item 2},
{...item 3},
...etc
]
I've tried to write a typePolicy like this, and it returns the correct data, but when I query it on the frontend, nothing has changed. The data is still contained in an Edges object and each item is still in a NODE from the wpGraphql backend.
typePolicies: {
Post:{
fields:{
date(){
return 'date edit test' // returns a modified date object
}
}
},
Query: {
fields: {
// posts: flatten(),
posts:{
read(posts, { readField }) {
if(posts){
const newItem = posts.edges.map(post => {
return post.node
})
console.log('newItem', newItem)
return newItem
}
return posts // does not return a modified posts object
},
}
},
},
},
Can I modify the response like this or do I just have to write a custom helper function to modify the data every time I query it before using the data inside a component?
I can't access a primaryTag variable in my GraphQL page-query.
What I want to achieve is on a blog Post page:
display the post content
display the related posts (based on the first tag)
In my gridsome.server.js
api.createPages(async ({ graphql, createPage }) => {
// Use the Pages API here: https://gridsome.org/docs/pages-api
const { data } = await graphql(`{
allPost {
edges {
node {
id
path
tags {
id
}
}
}
}
}`)
data.allPost.edges.forEach(({ node }) => {
createPage({
path: `${node.path}`,
component: './src/templates/Post.vue',
context: {
id: node.id,
path: node.path,
primaryTag: (node.tags[0] && node.tags[0].id) || '',
}
})
})
})
then in my Post.vue
<page-query>
query Post ($path: String!, $primaryTag: String!) {
post: post (path: $path) {
title
path
content
}
related: allPost(
filter: { tags: { contains: [$primaryTag] }, path: { ne: $path } }
) {
edges {
node {
id
title
path
}
}
}
}
</page-query>
Unfortunately I get the following error: `Variable "$primaryTag" of non-null type "String!" must not be null.
Also, as a side note (and that might be the bug issue) I'm using #gridsome/source-filesystem and #gridsome/transformer-remark to create my Post collection.
If you know how to solve this or have a better approach for getting the related posts, comment below.
Libs:
- gridsome version: 0.6.3
- #gridsome/cli version: 0.1.1`
In firebase, my app will be creating nested data all the time. My question is do I first have to check if a key exists before i can reference it, or can i just...
const ref = firebase.database().ref("dataTree/" + childVar1 + "/" + childVar2);
ref.push({data: "this is data"});
and it will create the nested structure for me? i.e. :
{
"dataTree": {
"notChildVar1": {
"someChildName": {
"data": "test"
}
}
"alsoNotChildVar1": {
"someChildName": {
"data": "test"
}
}
}
}
Should Become...
{
"dataTree": {
"notChildVar1": {
"someChildName": {
"data": "test"
}
}
"alsoNotChildVar1": {
"someChildName": {
"data": "test"
}
}
"childVar1": {
"childVar2": {
"data": "test"
}
}
}
}
And if the above code will return an error, what can i do to test if a key exists, add it if it does not exist, and then push something in that?
Maybe you can try something like create the full content of childVar1 and insert directly in your database including the childVar2: {data: test} (I supposed that you're using RealtimeDatabase).
const ref = firebase.database().ref("dataTree/" + childVar1);
ref.push(
"childVar2": {
"data": "this is data"
}
);
You can write to any arbitrary location in the database, and any required nodes that do not exist will be created for you automatically. You don't have to create each child node individually.
Similarly, if all children of a node are deleted, the node itself will be deleted automatically (there is no such thing as an "empty" child node).