Include mediaItems in GraphQL Wordpress query search - reactjs

I have a headless WordPress installation, and using a React frontend to query posts with a search query of:
posts(where: {search: $searchStr}) {
nodes {
title
content
link
}
}
but would like to include media files in the search results as well. I'm able to query and log out the mediaItems from the following additional query:
mediaItems {
nodes {
title
sourceUrl
}
}
but unable to figure out how to include these in the search results. How do I combine the posts and mediaItems in the same search query and return all results based on the search string?
Updated with full query
The full query I'm using, passes the search query into the posts GraphQL query. Since mediaItems and posts are both top level in WordPress, how can I combine the two queries so that all posts and all media items are returned based on the search parameter?
query appQuery($searchStr: String) {
posts(where: {search: $searchStr}) {
nodes {
title
content
link
tags{
nodes {
name
}
}
}
}
mediaItems {
nodes {
title
sourceUrl
}
}
}

You can create another GraphQL type for that & nest it in your main type so that you can also query your data. Also add resolver for that data to be retrieved.
For example: let's say you have a blogging site & you have some posts. For that
type Post {
_id: String
title: String
credit: PostCredit # Name of nested type
}
type PostCredit {
authorId: String # You can also perform further nesting
publicationId: String
}

Related

How to add extra field to Algolia

I have a already made project having connection between Algolia + Gatsby + Contentful.
In which the algolia queries have
allContentfulProduct {
nodes {
objectID: id
productTitle: title
slug
seoTitle
node_locale
}
}
}
const queries = [
{
indexName: process.env.ALGOLIA_SEARCH_INDEX_NAME_PRODUCTS,
query,
transformer: ({ data }) => data.allContentfulProduct.nodes,
}
]
I need to add one extra field to algolia to filter through the search.
But when i add isVisibleInternalSearch to the const query the field is only visible when the gatsby build command in ran.
When there is any change in the contenful fields then the site is not able to find the algolia field.
allContentfulProduct {
nodes {
objectID: id
productTitle: title
slug
seoTitle
node_locale
isVisibleInternalSearch
}
}
}
I am having the same problem in the example site I created on netlify. Once the site was working fine but when there was any kind of change in contentful the field from the algolia disappeared.

GraphQL: Writing Frontend Query with Optional Arguments

I am currently trying to write a GraphQL Query called recipe which can take a number of optional arguments based on a graphQL input called RecipeSearchInput, and uses the input to find a specific recipe matching the attributes passed.
I am struggling to write the frontend query to be able to be able to take the arguments as an object.
Here's my graphQl schema for graphql input RecipeSearchInput.
input RecipeSearchInput {
_id: ID
title: String
cookTime: Int
prepTime: Int
tools: [String!]
ingredients: [String!]
steps: [String!]
videoURL: String
tags: [String!]
country: String
}
And here's my query written in the frontend to access the my mongodb server through graphql:
// gql query that requests a recipe
export const findOne = obj => {
let requestBody = {
query: `
query {
recipe(recipeInput: ${obj}) {
_id
title
cookTime
prepTime
tools
ingredients
steps
videoURL
tags
country
}
}
`
};
return fetchEndpoint(requestBody);
};
When I wrote my frontend query with a simple object that I knew existed in my database:
const displayData = async () => {
const recipeData = await api.recipe.findOne({
title: "Greek Chicken Skewers"
});
console.log(recipeData);
};
This gives me the following error:
message: "Expected value of type "RecipeSearchInput", found [object, Object]."
The problem I'm seeing is that obj is not formatted properly (in String form) to be received by Graphql as an input. The Graphql query params look like this:
Here is what the working query looks like
What's the best approach for making a query that takes more than one argument? How should I package up the argument in the frontend to please GraphQL?
Thanks in advance, and please let me know if any of this was unclear!
Shawn

How set limit for nested field in AWS amplify DynamoDB schema?

I have next query
export const listCategorys = `query ListCategorys(
$filter: ModelCategoryFilterInput
$limit: Int
$nextToken: String
) {
listCategorys(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
words {
items {
id
en
ru
statusLearn
}
nextToken
}
}
nextToken
}
}
`;
I want use limit for nested element words and try get result with help next query
const listCats = await API.graphql(graphqlOperation(listCategorys, {limit:10, words:{limit:100}}));
but this query not work. How right build query?
This sounds very similar to Appsync & GraphQL: how to filter a list by nested value, in that the default Amplify codegen only generates top-level filters/limits.
There's a feature request around this here that you may want to upvote & comment on: https://github.com/aws-amplify/amplify-cli/issues/2311
Otherwise, there isn't a way to do this out-of-the-box yet without modifying the VTL templates.

Filtering Queries from Prismic and GatsbyJS

Seeing some strange behavior querying a Prismic data source and filtering by a content relationship in my gatsby site. I"m trying to create a page filtering some products based on the category passed into this page. Reading both the Prismic and Gatsby docs, i should be able to filter the data with the where clause but i get this error when i try to build
error Unknown argument "where" on field "allPrismicModel" of type "Query"
Below is the relevant sections of the query
query getProducts($uid: String) {
allPrismicModel(where: { system_category: $uid }) {
edges {
node {
data {
system_category {
uid
}
...other fields here...
}
}
}
}
}
Anybody ever encountered this or know how to resolve it?
where doesn't exist in Gatsby. I'd highly recommend using GraphiQL (under localhost:8000/___graphql) to see what you can do. There is also this doc showing all possibilities: https://www.gatsbyjs.org/docs/graphql-reference/
It'll probably will be in the end (untested):
filter: { system_category: { eq: $uid } }
allPrismicModel(filter: { system_category : { eq: $uid } }) {
edges {
node {
data {
system_category {
uid
}
...other fields here...
}
}
}
}
}
This is a more common way to run that query

GatsbyJS - How to query longtext Contentful fields in GraphQL

I ran into a problem querying a field of type "long text" from contentful.
I know the Contentful long text field is actually markdown. So I installed the gatsby-transformer-remark plugin, which I presumed I need.
Here's my GraphQL query:
query getStoreById($relativeUrl: String!) {
contentfulStore(relativeUrl: { eq: $relativeUrl }) {
relativeUrl
shopPageTitle
mainPageTextContent {
mainPageTextContent
}
}
}
The console still shows:
Objects are not valid as a React child (found: object with keys {childMarkdownRemark}).
Something to do with that error message.
Thanks!
The query will look something like:
query getStoreById($relativeUrl: String!) {
contentfulStore(relativeUrl: { eq: $relativeUrl }) {
relativeUrl
shopPageTitle
mainPageTextContent {
childMarkdownRemark {
html
}
}
}
}
If you haven't learned how to use GraphiQL yet, try opening it at localhost:8000/___graphql! It's your best friend for learning how to query your Contentful schema.

Resources