datocms / gatsby-source-datocms

Official GatsbyJS source plugin to pull content from DatoCMS
MIT License
140 stars 50 forks source link

Query models with parameter value from block #166

Closed phmasek closed 2 years ago

phmasek commented 2 years ago

This is a long shot but I'll try and see if someone can point me in the right direction or just tell me it's impossible... I've been doing some research but haven't found anything useful.

I would like to build a block called Feed. This block will feature two fields namely: show_count: integer and model_type: string. model_type can be either "project" or "news" (preferably also be able to choose both). I would like to use these fields to determine a query within this particular block that would query either projects, news or both models with a limit defined by show_count.

Is this in anyways possible to do from within the gatsby-source-datocms plugin?

matjack1 commented 2 years ago

hello @phmasek the plugin can help you retrieve the content from DatoCMS. Then you can achieve what you need by querying again the models that you need in a second go.

So with the first query you get show_count and model_type. Then you run a second query to search the models that you need and do some logic on your code.

Hope this helps!

phmasek commented 2 years ago

@matjack1 thanks for your help! Hmm, I was under the impression that it is not possible to pass any variables through useStaticQuery(). I guess I could do the first query in gatsby-node.js and pass the query through the page context and fetch the parameter in the page query. Is that what you had in mind or did I misunderstand you?

matjack1 commented 2 years ago

I got the impression that you wanted to do the queries in the browser by picking the two options? So maybe the second query can be a query directly from the browser using GraphQL.

Otherwise you might retrieve all the content in the gatsby-node and pass the data directly to the component that does the filtering?

I'm not a Gatsby expert, so there might be better ways! :)