How to generate GraphQL queries using Google Gemini 1.5 Pro
I volunteer at Teia, a community-run NFT marketplace for artists worldwide. The Teia NFT metadata is stored on the Tezos blockchain. This data is a goldmine of trends and insights, but getting it is a technical challenge for ordinary users.
GraphQL, the tool for unlocking this Teia treasure trove, is powerful. But let’s be honest: The syntax can be a headache. Wouldn’t it be awesome to ask for the data you want in plain English?
Get ready because Google Gemini 1.5 Pro is here to be your magical translator! Imagine asking things like “Show me the top-selling Teia artists.” or “What are the most popular image tags for the past week?” and getting an instant, ready-to-use GraphQL query as the answer. That’s the power we’re going to tap into!
Note: This article is more techy than my previous articles about Google Gemini Advanced. However, you don’t need to be a developer to use any of the tools mentioned. This article focuses on the LLM aspects and not so much on GraphQL.
GraphQL Fundamentals
Let’s break down GraphQL without getting too technical. Here’s the core idea:
- The Librarian Analogy: GraphQL is like a knowledgeable librarian managing a massive information collection. Instead of digging through the whole library, you ask for a specific book by title or request a few specific pages about a particular topic. GraphQL is that helpful librarian who finds what you need and brings it right.
- Data Requests, Not Database Dives: With traditional databases, you often need to know the exact structure or relationships between data bits to find what you want. GraphQL simplifies this! You describe the information you’re after, and GraphQL does the hard work of retrieving it.
The Bottom Line: GraphQL is about flexibility and precision. You avoid overloading with excess data and have more control over the information you receive.
First Steps
Ready to dive into Gemini 1.5 Pro? You can use Google AI Studio for free to use this super-powered language model. Think of it as the big sibling to Gemini Advanced, the chatty bot you might already know. Gemini 1.5 Pro can do similar things but unlocks new possibilities with its vast context window.
Let’s see what Gemini already knows about GraphQL and the Teia marketplace. After signing into Google AI Studio with your Google account, create a new project and choose “Freeform Prompt.” Then, select the Gemini 1.5 Pro model from the dropdown on the right. Now, enter this in the prompt editor:
“User: Provide the GraphQL query for the total sales volume this past week on the Teia marketplace.”
Hit that “Run” button and hang tight for a moment!
The response might look like GraphQL, but it must be corrected. Gemini has been trained on much information but needs Teia’s specifics. Think of it like having a bright friend who has yet to visit your favorite neighborhood spot.
To help Gemini out, we’ll do a process called “grounding.” That’s giving it the local knowledge it needs — information about Teia and how its data is organized. This helps Gemini give accurate and helpful answers, not just general guesses.
The Process
Think of prompt design as a secret language you share with your LLM assistant. The better you communicate your needs, the wiser and more helpful the responses will become! That’s what prompt engineering is all about.
From my first experiment, it was clear I couldn’t just ask Gemini for Teia-related GraphQL and expect perfect results. Here’s why:
- Need a Map: It’s like asking for directions without specifying where you want to go! I needed to give Gemini the Teia GraphQL schema, a detailed map of all the data points it can access.
- Examples are Key: I dug up a bunch of sample GraphQL queries, some from Teia’s code and some from my tinkering. Then, I translated these into the kinds of regular, everyday questions someone might ask. This showed Gemini how to bridge the gap between human-speak and code-speak.
The rest was all about practice! I threw a bunch of test questions at Gemini and tweaked the wording of my prompts along the way to keep improving those results.
Designing the Prompt
Okay, let’s talk about the super-powered prompt we will build. Here’s a breakdown of the key ingredients:
Gemini’s Persona
We’re turning Gemini into a GraphQL wizard! I’ll tell it, “You are a GraphQL sorcerer. You aim to generate GraphQL queries for the Teia NFT marketplace indexer.” This keeps it focused on the task at hand.
Teia 101
We’ll arm Gemini with background info on Teia’s community and marketplace. This way, I can use words artists and collectors are familiar with, skipping overly technical language. The Teia context summarizes its history, governance, technical aspects, the nature of its NFTs, and smart contracts.
The Schema Map
The GraphQL schema is like the legend for Teia’s data treasure map. I’ll ensure Gemini has a copy to reference: The Teia community runs its instance of TezTok, an NFT indexer for the Tezos blockchain. Using a free web interface, one can query Teztok with GraphQL. In that web interface, you can click on the “Docs” link on the right side of the UI and then “query_root” to get the complete schema for the indexer data. The schema was copied and added to the Gemini prompt as a file: “Assume a GraphQL schema representing the data stored for the Teia NFT marketplace are listed in the file teiagraphqlschema.txt”
Documentation is Key
The more Gemini knows how the Teia data is organized, the better. We’ll give it the TezTok indexer documentation — think of it as the ancient spellbook for unlocking those insights. I downloaded the TezTok documentation as PDFs and uploaded it to the Gemini prompt as a folder of files:
“The database schema is based on TezTok, a Tezos indexer that allows developers to access NFT data across Tezos marketplaces through a GraphQL API. The indexer aims to align data, including sales, listings, and offers, from different NFT marketplaces, making it more accessible for developers to build dApps. This TezTok database only keeps track of Teia data. TezTok is documented in the following folder of PDFs: …”
The Power of Examples
We’ll show Gemini actual Teia GraphQL queries alongside everyday questions someone might ask. This is like the Rosetta Stone for an LLM — it learns how to translate our language into the language of data! Here is one:
“Here is an example prompt to generate a graphQL query: Determine if the buyer account has bought an NFT from the seller account (there are two variables: buyer and seller).”
Output Control
We want a shorter response, just the goods! I’ll tell Gemini to give me a clean GraphQL output, which will make it much easier to use the results later:
“Generate a GraphQL response for the user’s natural language request. The response should have valid GraphQL syntax. Don’t add more text or formatting to your response.”
It might seem like a lot of info to give Gemini, but that’s the beauty of Gemini 1.5 Pro. It can handle much more context than regular chatbots.
Note: I’ve copied all the files in the Gemini prompt to Google Drive if you want to replicate the design.
Testing
Now, we need to test the prompt using Google AI Studio. Create a new project and choose “Freeform Prompt.” Now paste the complete prompt into the editor and append a user query:
“User: The total NFTs sold this past week on the Teia marketplace.”
Hit that “Run” button and hang tight for a moment!
Now, you should have a GraphQL result that looks like this:
Note: The timestamp is probably wrong, and you need to manually adjust it since Gemini doesn’t have a real-time clock and defaults to its model training date (You could provide the current date as part of your prompt).
Now, we will confirm that the syntax is correct and the query works using the free online GraphQL tool. Copy the query into the editor and press the “Execute Query” button (it looks like a play button).
You know you are good to go if you see the data in the right-hand panel!
GraphQL Error Handling
Even Gemini might get the GraphQL query wrong. Here’s what to keep in mind when things go a little wonky:
- Logical Snafus: Double-check for the right conditions (like and/or). Gemini sometimes doesn’t get them right, and you might need to fix the query manually. You can also just rerun the prompt for a proper response. If this is persistent, consider adding more example queries to the prompt.
- Unexpected Answers: Examine your wording if the query runs, but the results seem baffling. Did Gemini misunderstand your question? Can you make things more transparent? Can you add more context to your user query?
- Missing Data: You might ask for data that doesn’t exist on the Teia blockchain. For instance, sales may not have occurred within a specific timeframe, or an artist may not have created works that match your criteria.
Troubleshooting Tip: Breaking down your query piece by piece can help isolate the issue. Test smaller sections first, then gradually build them back up. And don’t underestimate the power of those error messages — they often hold clues!
Don’t get discouraged by errors — view them as stepping stones on your journey to GraphQL mastery!
Lessons Learned
While building this ultimate Teia GraphQL prompt with Gemini felt like a magical adventure, it was only sometimes smooth sailing! Here are some key takeaways to keep in mind:
- Testing Reveals the Flaws: Even the most powerful wizards need practice. Feel free to bombard Gemini with different Teia-related questions and see how it translates them into queries. Sometimes, a slight change in wording can significantly impact the final result.
- Examples are Your Teaching Tools: The quality and quantity of examples you provide are like training wheels for Gemini. The more diverse the set of prompts and corresponding results you feed, the better it grasps the structure of good queries and the kind of data you want.
- Iteration is the Name of the Game: Don’t get discouraged if the first prompt you create isn’t perfect. Think of it as a conversation — you ask a question, see what Gemini comes up with, and then refine your approach based on the response. Remember, Gemini might not always get it right on the first try. If that happens, try adjusting the wording of your prompt or providing more context about what you’re trying to achieve.
The Takeaway: Building a powerful LLM assistant takes a little effort, but the results can be truly transformative. Once your Teia GraphQL prompt is finely tuned, imagine all the fascinating questions you can answer about the Teia marketplace!
Now, Make It Yours
While we’ve been diving deep into Teia NFTs, the magic doesn’t stop there! The process we explored for building a Teia-specific GraphQL prompt with Gemini can be a robust template for tackling other GraphQL use cases. Here’s how you can adapt it:
- Swap the Target: Instead of focusing on Teia’s NFT marketplace, consider the data you want to explore with another GraphQL API. This could be anything from social media metrics to product information in an e-commerce store.
- Craft Your Context: Like we provided background info on Teia, tailor the context to your chosen platform. This helps Gemini understand the relevant terminology and data structure.
- Gather Your Examples: Find or create real-world GraphQL queries related to your target platform. The more examples you provide, the better Gemini learns how to translate your questions.
- Adjust the Ask: Instead of requesting the total sales volume for Teia, rephrase your prompt to match your new data source. What questions are you curious about?
Providing Gemini with enough context and examples to understand your specific needs is critical. With some creativity, you can transform Gemini into your GraphQL sorcerer, ready to unlock a treasure trove of data from various platforms!
Conclusion
Think of this Teia exploration as the first chapter of your AI-powered data wizardry journey. You’ve unlocked the power of translating natural language questions into ready-to-use GraphQL queries — no more headaches trying to handcraft queries yourself!
Whether you’re a Teia enthusiast, a dApp developer, or just someone fascinated by the world of data, the possibilities are endless. Armed with Gemini 1.5 Pro and the principles we’ve covered, you’re ready to tackle any GraphQL-powered platform that sparks your curiosity.
Check out my reading list of other Google Gemini articles.
This post was created with the help of AI writing tools, carefully reviewed, and polished by the human author.