WPGraphQL with WP GraphQL Gutenberg - How to get - wordpress

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?

Related

Nested pages in Hygraph (GraphCMS) with Nextjs dynamic routing

I am trying to create nested pages using Hygraph (GraphCMS) and Nextjs. I thought about using [..slug].js
But the problem I have is that writing my slug-like this "page/subpage" the GraphCMS returns the following structure which is not an array or nested
{
"data": {
"pages": [
{
"slug": "page/subpage"
}
]
}
}
And if I try to get static paths Nextjs simply will find nothing at page/subpage, it will always be 400 not found because it is a string and only will work as http://localhost:3000/page%2Fsubpage
Does anyone know how to create subpages in GraphCMS (Hygraph)? Really need your help.
Right now I have the following code to get static paths
export async function getStaticPaths() {
const client = graphcmsClient()
const { pages } = await client.request(`
query {
pages(where: {slug_not_in: ["home"]}) {
slug
}
}
`)
return {
paths: pages.map(({ slug }) => ({
params: { slug },
})),
fallback: false
}
}

Using Gatsby `createResolvers` to set default image if GraphQL returns null?

I'm working on a gatsby site using gatsby-source-wordpress to source posts for the blog. However, if any of the WordPress posts do not include a featured image this causes the build to fail. I understand that this is expected behavior.
Here is the build error I am seeing:
29 | {posts.map(({ node: post }, index) => (
30 | <li key={post.id} {...post}>
> 31 | <Img fixed={post.featured_media.localFile.childImageSharp.fixed} />
| ^
32 | <p>
33 | <Link to={`/insights/${post.slug}`}>
34 | {post.title}
WebpackError: TypeError: Cannot read property 'localFile' of null
This is caused by the resulting query, which is returning a null result in the second node because there is no featured image on the post:
{
"data": {
"allWordpressPost": {
"edges": [
{
"node": {
"id": "28ec9054-5b05-5f94-adcb-dcbfc14659b1",
"featured_media": {
"id": "f12d613b-e544-560b-a86f-cd0a7f87801e",
"localFile": {
"id": "7fca2893-ff80-5270-9765-d17d3dc21ac2",
"url": "https://www.mycustomdomain.com/wp-content/uploads/2020/01/some-featured-image.jpg"
}
}
}
},
{
"node": {
"id": "91a236ed-39d5-5efc-8bed-290d8344b660",
"featured_media": null
}
}
]
}
}
}
How I would like to fix:
As an ideal solution, I would like to use schema customization to set a default image if there is no featured image in WordPress. But I am at a total loss how to correctly do so. I am working from this documentation to guide me, but I'm just not getting my head wrapped around it properly.
A similar working example:
Tag data is similar to featured images in that the query returns null if the post has no tags. However I am able to set a default undefined tag using createResolvers like so:
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
wordpress__POST: {
tags: {
resolve(source, args, context, info) {
const { tags } = source
if (tags === null || (Array.isArray(tags) && !tags.length)) {
return [
{
id: 'undefined',
name: 'undefined',
slug: 'undefined',
}
]
} else {
return info.originalResolver(source, args, context, info)
}
},
},
},
}
createResolvers(resolvers)
}
And this works as shown in the following query results:
{
"data": {
"allWordpressPost": {
"edges": [
{
"node": {
"id": "28ec9054-5b05-5f94-adcb-dcbfc14659b1",
"tags": [
{
"id": "undefined"
}
]
}
},
{
"node": {
"id": "91a236ed-39d5-5efc-8bed-290d8344b660",
"tags": [
{
"id": "50449e18-bef7-566a-a3eb-9f7990084afb"
},
{
"id": "8635ff58-2997-510a-9eea-fe2b88f30781"
},
{
"id": "97029bee-4dec-5198-95af-8464393f71e3"
}
]
}
}
]
}
}
}
What I tried for images (isn't working...)
When it comes to nested nodes and image files I'm at a total loss. I am heading in the following direction based on this article and this code example, but so far it isn't working:
exports.createResolvers = ({
actions,
cache,
createNodeId,
createResolvers,
store,
reporter,
}) => {
const { createNode } = actions
const resolvers = {
wordpress__POST: {
featured_media: {
type: `File`,
resolve(source, args, context, info) {
return createRemoteFileNode({
url: 'https://www.mycustomdomain.com/wp-content/uploads/2017/05/placeholder.png',
store,
cache,
createNode,
createNodeId,
reporter,
})
},
},
},
}
createResolvers(resolvers)
}
I realize the above code does not have an if else statement, so the expectation is that all featured images would be replaced by the placeholder image. However the resulting GraphQL query is unaffected (as shown at top).
Can anyone point me in the right direction here? I can't seem to wrap my head around what information I can find out there.
WebpackError: TypeError: Cannot read property 'localFile' of null
'localFile' of null means that nulled is a parent of localfile - featured_media ... you can see that in results:
"featured_media": null
... so you're trying to fix localfile while you should work on featured_media level
why?
You can easily render conditionally [in react] what you need (placeholde, component) on nulled nodes ... why at all you're trying to fix graphql response?

How to get a specific translation of drupal node with graphql

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
}
}
}
}

When querying for a single item in graphql, meteor, and apollo i get null values returned

I have probably overlooked something in the docs, but I have seem to run into a problem with being able to get a single object from my graphql queries.
Here is the schema:
type Query {
product(name: String!): Product
}
type Product {
_id: String
name: String
}
Here is the resolver:
Query: {
product (_, args) {
return Products.find({where: args})
},
products () {
return Products.find().fetch()
}
}
Here is the Query:
query {
product(name: "burgers") {
name
}
}
I get a result of this:
{
"data": {
"product": {
"name": null
}
}
}
Am I just forgetting to add something to this, and if so could you point me the right direction.
If Products is a Meteor Collection, then .find returns a cursor, so the right thing to return would be Products.findOne({name: args.name})
http://docs.meteor.com/api/collections.html#Mongo-Collection-findOne

Elasticsearch - Count distinct

I have a basic index with logs
Some logs are visit of user1 to user2
I managed to count the total of visits a user has received, but I don't know how count the total of distinct users a user has received
This is giving me all the logs for a user
{
"post_filter":{
"bool":{
"must":[
{
"term":{
"message":"visit"
}
},
{
"term":{
"ctxt_user2":"733264"
}
}
]
}
},
"query":{
"match_all":{}
}
}
Actually, I'm using FoSElasticaBundle for Symfony2
$filter->addMust((new Term())->setTerm('message', 'visit'));
$filter->addMust((new Term())->setTerm('ctxt_user2', $this->search->getVisit()));
I read some pages in the ES doc with aggregator, but I never managed to get what I want
Convert to SQL, I just need
SELECT COUNT(DISCTING ctxt_user1)
FROM logs
WHERE ctxt_user2 = 733264
EDIT:
Cardinality seams to be what I need.
Now just need to find how use it with FosElasticaBundle
"aggs": {
"yourdistinctcount": {
"cardinality": {
"field": "ctxt_user1"
}
}
}
Try this query ( not tested...):
{
"query" : {
"bool":{
"must":[
{
"term":{
"message":"visit"
}
},
{
"term":{
"ctxt_user2":"733264"
}
}
]
}
},
"aggs": {
"yourdistinctcount": {
"terms": {
"field": "ctxt_user1"
}
}
}
}
The post_filter query cannot be used in your case. As it write on Elastic.co website: The post_filter is applied to the search hits at the very end of a search request, after aggregations have already been calculated.`
HtH,

Resources