Hey i'm using Sanity and have created both a allSanityPost (for my blog posts) and allSanityCases (for all my cases). Do anyone know how to combine these categories into one array and order them by their "published-date"? I want to display them both on one blog page - but not in separated lists.
I'm building the page with Gatsby, so react answers would be preferable :)
Cheers
The current GraphQL endpoint have no way of querying across documents like GROQ does. Therefore, you will have to do this after querying the GraphQL endpoint for data. I suggest querying for both all posts and all cases separately like this:
query {
allSanityPost{
title
},
allSanityCases{
title
}
}
Given that both of these types have some sort of date, you should be able to combine them into one single array, and then do your sorting thereafter.
Related
I am developing a React Firebase web app, and I want to implement a "custom" sort on top of sorting by name and date. How should I go about storing this "sort" information?
Do developers append a "key" property to the components themselves? How will reordering work then (do i shift every component's key values by 1? That seems inefficient). Or should I do a separate file or database entry that just stores the order as an array of component id keys?
Should all of this be resorted on page load or should I reorganize the data in a way where as soon as I request them they're already sorted?
I've tried adding the order key prop to every item but it seems overly complicated for a simple feature. I haven't had much luck searching this online.
Firestore allows you to orderBy and limit queries.
You can also specify a cursor to paginate more data on request.
See This Example in the docs for more info.
I want to build a search page that lets user query data based on certain criteria that they can choose from select and radio checkboxes, on behalf of which a query string is generated that gives the key/value pair for searching, the functionality is somewhat similar to what they have for their staters page.
Please check the link below to get an idea of what I am looking for:
https://www.gatsbyjs.org/starters/?v=2 <---- query string.
These pages need to be shareable so query strings are essential and data fetching need be as quick as it is on their website.
From what I have understood so far, according to documentation:
https://www.gatsbyjs.org/docs/data-fetching
I have two options:
Get data during build time
Get data during run time
For a run time, I think, I would need to use redux-thunk to make network calls to an ExpressJs, MongoDB REST API but my concern is that it can be slow.
For build time, I am sure on how can I source data from Graphql queries, and render them.
Any help on this will be great. Thanks
The Gatsby Starters Library is parsing the URL param in this urlToSearch method in the PluginSearchBar component:
urlToSearch = () => {
if (this.props.location.search) { // get the params from the location prop
const match = /(\?|&)=([^&]+)/.exec(this.props.location.search)
if (match) return decodeURIComponent(match[2])
return ``
}
return ``
}
Mainly, it uses the Gatsby location prop (from #reach/router under the hood) to get the search keyword from the query string.
Search is then implemented client-side using Algolia's react-instantsearch library. You can find the complete implementation in plugin-searchbar-body.js.
There are naturally other ways to implement search. A good place to start for inspiration is Gatsby's Adding Search documentation.
I am trying to perform a query using the in operator where the criteria is based on values in an array. How can I perform the query below to take an array and base its in criteria off a variable array? I am using reactjs + gatsby.
... graphql`
query pageHeader {
.... there is another query in the real code above this line
allContentInSites (filter: {slug: {in: ` + JSON.stringify(searchCriteria.map(item => item.value)) + ` }}) {
edges {
node {
title,
link
}
}
}
}'
I was initially thinking the sample code above would pass a json string object for the in criteria but this seems to break the page.
This won't work in your page queries in Gatsby. The GraphQL query is evaluated and replaced with data statically prior to the code being executed.
You could potentially hook into the Node APIs provided by Gatsby to build this query during boot, though. You may be able to use onPreExtractQueries to create the query, or potentially use the graphql object provided by any of the node APIs in your gatsby-node.js to generate pages.
if you would like to implement dynamic filtering criteria, you have couple options
1) You can use stringified json and send it as String and parse it on the server in resolver
2) You can use custom scalar graphql-type-json as argument type or implement your own custom scalar.
The big downsides of this is that you are loosing strongly typed features of GraphQL. You can also implement dynamic schema, but it is a little bit overkill and in general not a good practice. I would go for dynamic arguments only if it is the only option. Usually it can be rearanged so that you do not have to use dynamic arguments.
I am not sure if it is helpful as i am not familiar with gatsby specific issues with this, but this should work in general GraphQL schemas.
Specifically I feel like I am looking for the answer to the question asked here, but it turns out the title of the question isn't a perfect match for the actual question.
What I am looking to do involves Relay, React, and GraphQL (I suppose I could have stated Relay, and you could have figured out the other two).
What I want to do, and what I can't seem to find an answer to, is to create a query that lists a variable list of query fragments based on some predefined JSON array.
If you look at my home page, jimmyvanveen.com you will see I have a list of projects I have worked on (or am working on), and each renders as a React component. I am pulling this data from Github through their REST API (v3) - but now I want to migrate to GraphQL (v4).
I can make an array containing the repo name, and owner as required by the Schema of Github's API, but I don't know how to dynamically create a query based off a simple array such as:
repos: [
{"name": "repo name", "owner": "Repo Owner"},
{"name": "other repo", "ownder": "Other Owner"}
]
I know how to make a query that can find all of this information manually, but I was hoping there was a GraphQL way (perhaps by sending vars?) to run through the entire array in one query and return the array of repo data.
I've been racking my brain for days trying to find the answer here, or anywhere, and I'm striking out.
Thanks in advance for any help you can provide!
So, in the end I figured out I am in general just submitting the wrong information to Github in order to pull Repo info from a curated list.
My objective above is still not possible exactly the way I wanted to solve the problem, however the method mentioned above is very similar to a usable method that DOES work. Simply put, supply Github with an array of unique node ID's, rather than Repo Name and Repo Owner, as I had been trying to do.
This way, you are not trying to use a 2D array, and instead you are using a single array, with an equal amount of specificity as the 2D array.
So formulating an array such as:
const repos = [
'uniqueID1',
'uniqueID2',
'uniqueID3',
...
];
...can then be passed through Relay (I had the QueryRenderer set to run on my App component) like so:
query AppQuery($repos:[ID!]!) {
projects: nodes(ids:$repos){
...projects_projects
}
}
You can obtain the node ID's in multiple ways, but the way I did this was by pulling the repo info one by one in GraphiQL with a query such as this:
query {
repository(owner:"JimmayVV", name:"JimmyVanVeen.com") {
id
}
}
I then consolidated all of these ID's into an array, and passed that through to Relay and got the desired results.
I'm sure there are other ways, perhaps using a simple Node script to formulate the array for you, but this was simple enough for me, that I am happy with it.
I am experimenting IBM watsons' Discovery API to get data insights. I want to query using multiple filters. I am using python to accomplish the task. I have tried this for now, but this is not working.
qopts = {'filter':[{'enriched_text.entities.text:Recurrent Neural
Networks,Machine Learning classifiers'}]}
my_query = discovery.query(env_id, coll_id, qopts)
with only single entity : 'recurrent Neural Networks' through the discovery UI and through my python query, I get 3 documents from the collection.
but with two entities, 'Recurrent Neural Networks,Machine Learning classifiers', in the UI I get 2 documents but through my code, I get 2 documents.
Below is then right format which works for me. with multiple concept and keyword filters, I get a total of 2 search results, which match with the UI query
qopts = {'filter':{'enriched_text.concepts.text:"Neural network",enriched_text.keywords.text:"Neural Network",enriched_text.keywords.text:"generative conversational models"'}}
with only entity I get 3 match results
qopts = {'filter':{'enriched_text.concepts.text:"Neural network"'}}
in this example I am querying the documents with concept 'Neural network' , keywords 'Neural Network' and 'generative conversational models'
Inside Watson discovery documentation, inside the UI you'll use (according to the documentation):
But obviously, without the ! operator inside the second text.
and I think inside your code you need to use , between the values.
Not sure because I don't use the enriched_text.entities.textinside my filter, just the Strings.
One possible reference for another example to test:
filter=field1:some value,field2:another value
Official reference documentation: here.