Query GraphQL APIs with AI Agents
Execute GraphQL queries against any server from a LlamaIndex agent.
Why it matters
Empower AI agents to seamlessly query and extract data from GraphQL endpoints. Integrate with existing systems to unlock structured data for analysis and automation.
Outcomes
What it gets done
Execute GraphQL queries against specified servers.
Retrieve structured data from GraphQL APIs.
Integrate GraphQL data access into AI agent workflows.
Configure queries with necessary headers and server URLs.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-graphql | bash Overview
GraphQL Tool
A LlamaIndex tool that runs GraphQL queries against a configured server, best used alongside the server's own schema. Use when an agent needs to query a GraphQL API directly, ideally with schema access for reliable query construction.
What it does
The GraphQL Tool gives a LlamaIndex agent the ability to execute GraphQL queries against a server. GraphQLToolSpec is initialized with the server's url and any required headers (such as a content-type or auth header), and once attached to an agent's tool list, the agent can query that server directly.
A single function, graphql_request, runs a GraphQL query against the configured server. The source's own worked example points the tool at a public SpaceX GraphQL API and asks the agent to "get the id, model, name and type of the Ships from the graphql endpoint" - showing the agent constructing and running a real query against a live schema rather than a mocked or hardcoded response.
When to use - and when NOT to
Use it when an agent needs to query any GraphQL server directly, ideally when the agent also has access to that server's GraphQL schema - the source notes the tool works best that way, and suggests pairing it with a file loader so the agent can read the schema alongside querying the API for more capable results. Do not use it against a GraphQL server the agent has no schema knowledge of and expect reliable query construction; without schema context, the agent is guessing at field names and structure.
Capabilities
graphql_request runs a GraphQL query against the configured server URL and headers, returning the response for the agent to use.
How to install
from llama_index.tools.graphql import GraphQLToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
tool_spec = GraphQLToolSpec(
url="https://spacex-production.up.railway.app/",
headers={
"content-type": "application/json",
},
)
agent = FunctionAgent(
tools=tool_spec.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
Who it's for
Developers building LlamaIndex agents that need to query a GraphQL API directly, ideally with the server's schema also available to the agent for more reliable query construction.
Source README
GraphQL Tool
This tool provides agents the ability to easily execute GraphQL queries against a server. The tool can be initialized with the server url and any required headers and thereafter perform queries against the server
Usage
This tool has a more extensive example usage documented in a Jupyter notebook here
Here's an example usage of the GraphQLToolSpec.
This tool works best when the Agent has access to the GraphQL schema for the server. See here for an example of using a tool with a file loader to create even more powerful Agents.
from llama_index.tools.graphql import GraphQLToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
tool_spec = GraphQLToolSpec(
url="https://spacex-production.up.railway.app/",
headers={
"content-type": "application/json",
},
)
agent = FunctionAgent(
tools=tool_spec.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
await agent.run(
"get the id, model, name and type of the Ships from the graphql endpoint"
)
graphql_request: Runs a GraphQL query against the configured server
This loader is designed to be used as a way to load data as a Tool in a Agent.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.