Using external APIs and custom input components for unstructured content in Sanity
9 replies
Last updated: May 30, 2022
S
Is this possible with Sanity? One client asked this.
May 28, 2022, 4:04 PM
K
I am pretty sure not.
May 29, 2022, 6:47 AM
J
You could use the http api to insert custom object data to some degree. But this isn’t going to be readable in the studio unless it’s defined. This would also be the most expensive way to read/write your data. You could mix and match sanity tools for publishing more-structured content meant for publisher interactions while referencing an external API for the more messy content. As block editors are themselves unstructured schemas in a sense, you could use this for a variety of less-structured blocks, but for the studio to work as an editing platform, you would need to define all possible interactions. You could create custom input components of your own to inject blob content with custom ui tools that you can insert in your app that is set up to interpret the blobs.
You’re going to need some object schema defined to reference whatever content you want to store, and while all of these sound like fun and unique ways to create things with the sanity tools, I don’t know that I would advise any of them exactly beyond play and very small projects.
But I don’t want to say
no to trying unique ideas outright, as long as they aren’t malicious. Just be aware of the impact to billed usage, and limitations of content and structure defined in the documentation.
You’re going to need some object schema defined to reference whatever content you want to store, and while all of these sound like fun and unique ways to create things with the sanity tools, I don’t know that I would advise any of them exactly beyond play and very small projects.
But I don’t want to say
no to trying unique ideas outright, as long as they aren’t malicious. Just be aware of the impact to billed usage, and limitations of content and structure defined in the documentation.
May 29, 2022, 8:51 PM
J
I’m not sure I understand exactly what is being requested. Maybe the GraphQL API could be used to reference schema structure? Or use documents as templates for each structure type. I would need to know more about the use case details to really say.
May 29, 2022, 8:56 PM
J
There are a ton of things you could possibly do - that wouldn’t be good or advised things to do. And I don’t want my own playful pondering to be mistaken for advice or instruction.
May 29, 2022, 9:00 PM
S
The client wants to get the schema structure using an APi so he can use it on an external site. So if I have a string with 3 options. then if I add one more option to the list, his API will get that new option as well.
make sense?
make sense?
May 30, 2022, 2:51 AM
J
This is some play of my own and there may be a much better method. But it seems like it MIGHT have potential for what they want. I’m not sure of a similar method to do this with groq but will keep a lookout for it.
(* don’t do this on a production project or dataset or otherwise this is at your own risk)
•
deploy the GraphQL API to your project (note that there are a number of caveats that may require some schema tweaks if you’re coming from just groq)• for testing start
playground • try
this query your data structure in graphql (know that I am not great with graphql and cobbled this example together from a variety of references but it appears to perhaps give what you’re looking for - warning, it’s a LOT of data returned and once again this may adversely impact your api usage)
(* don’t do this on a production project or dataset or otherwise this is at your own risk)
•
deploy the GraphQL API to your project (note that there are a number of caveats that may require some schema tweaks if you’re coming from just groq)• for testing start
playground • try
this query your data structure in graphql (know that I am not great with graphql and cobbled this example together from a variety of references but it appears to perhaps give what you’re looking for - warning, it’s a LOT of data returned and once again this may adversely impact your api usage)
May 30, 2022, 4:30 AM
J
I was able to find a graphql query that deeply returns all schema objects. I hesitate to post it, as it may have adverse impacts. But if you would like to explore the api , please do (test project+dataset advised). I will keep a lookout for a good groq solution for something similar.
The deep query I was able to test returned a LOT of extraneous data. And remotely generating documents is going to very taxing on your API usage and may cause the studio to behave very unpredictably. It may be more efficient overall for this to generate the documents externally and then query content from them into a sanity/studio schema.
The deep query I was able to test returned a LOT of extraneous data. And remotely generating documents is going to very taxing on your API usage and may cause the studio to behave very unpredictably. It may be more efficient overall for this to generate the documents externally and then query content from them into a sanity/studio schema.
May 30, 2022, 4:38 AM
J
In a nutshell, Kitty’s advice is likely the most sound for any production project for now.
May 30, 2022, 4:41 AM
S
noted. thank you
May 30, 2022, 5:29 AM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.