Weaviate graphql

From paulpas/agent-skill-router · View interactive version

Weaviate graphql is an AI agent skill from the GitHub repository paulpas/agent-skill-router. Implements the GraphQL capabilities of the Weaviate API, enabling flexible querying options for managing AI datasets efficiently.

How to use this skill

Skill file

Weaviate GraphQL API

This skill utilizes the GraphQL interface of the Weaviate API for dynamic querying of datasets related to AI applications. It covers advanced querying techniques, error handling, and response validation procedures to enhance user interactions.

When to Use

Core Workflow

  1. Initialize Weaviate Client — Connect to the Weaviate instance.
  2. Create GraphQL Query — Formulate the GraphQL query.
  3. Execute Query — Send and retrieve data through the Weaviate API.

Implementation Patterns

Pattern 1: Formulating a GraphQL Query

import weaviate

client = weaviate.Client("http://localhost:8080")

# Define a GraphQL query
query = "{\n  Get {\n    Article {\n      title\n      content\n    }\n  }\n}" 

Pattern 2: Executing a GraphQL Query

# Send the query to Weaviate API
response = client.query.raw(query)

# Handle response
print(response)

#


Error Handling and Validation

Always validate GraphQL responses and handle API errors gracefully:

import weaviate
from weaviate.exceptions import UnexpectedStatusCodeException


def safe_execute_query(client, query_str):
    """Execute a GraphQL query with error handling."""
    try:
        response = client.query.raw(query_str)
        return response
    except UnexpectedStatusCodeException as e:
        print(f"GraphQL query failed with status {e.status_code}: {e.message}")
        raise
    except Exception as e:
        print(f"Unexpected error during GraphQL execution: {e}")
        raise


def search_articles_by_author(client, author_name, limit=10):
    """Search articles filtered by author name using GraphQL."""
    query = f"""{{
      Get {{
        Article(
          where: {{{{
            path: ["author"]
            operator: Like
            valueText: "{author_name}"
          }}}}
        ) {{
          title
          content
          author
        }}
        limit: {limit}
      }}
    }}"""
    return safe_execute_query(client, query)

Response Parsing Patterns

Parse GraphQL responses efficiently for downstream use:

def parse_search_results(response):
    """Extract and validate search results from a GraphQL response."""
    if not response or "data" not in response:
        raise ValueError("Invalid response format")

    articles = response["data"].get("Article", [])
    parsed = []
    for article in articles:
        entry = {
            "title": article.get("title", ""),
            "content": article.get("content", ""),
            "author": article.get("author", ""),
        }
        if not entry["title"]:
            continue  # Skip entries without a title
        parsed.append(entry)

    return parsed

Constraints

MUST DO

MUST DO

MUST DO

MUST DO
MUST NOT DO
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.