Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

SDLReader for Graphql #461

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions llama_hub/file/sdl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SDL Loader

This loader extracts definitions from a Schema Definition Language file, used to specify the data for a GraphQL endpoint

## Usage

To use this loader, pass in the filename for the SDL file.

This tool has a more extensive example usage documented in a Jupyter notebook [here](https://github.com/emptycrown/llama-hub/tree/main/llama_hub/tools/notebooks/shopify.ipynb) and [here](https://github.com/emptycrown/llama-hub/tree/main/llama_hub/tools/notebooks/shopify.ipynb)

```python
from llama_hub.file.sdl.base import SDLReader

loader = SDLReader()
documents = loader.load_data('./data/shopify.graphql')
```

This loader is designed to be used as a way to load data into [LlamaIndex](https://github.com/jerryjliu/gpt_index/tree/main/gpt_index) and/or subsequently used as a Tool in a [LangChain](https://github.com/hwchase17/langchain) Agent. See [here](https://github.com/emptycrown/llama-hub/tree/main) for examples.
1 change: 1 addition & 0 deletions llama_hub/file/sdl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## init
28 changes: 28 additions & 0 deletions llama_hub/file/sdl/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List

from llama_index.readers.base import BaseReader
from llama_index.readers.schema.base import Document

class SDLReader(BaseReader):
"""Schema definition langauge reader

Reads GraphQL Schema files

"""

def load_data(
self, filename: str
) -> List[Document]:
"""Parse file."""
try:
import graphql
except ImportError:
raise ImportError("Please install graphql 'pip install graphql-core' ")
with open(filename, 'r') as f:
txt = f.read()

ast = graphql.parse(txt)
chunks = []
for definition in ast.definitions:
chunks.append(txt[definition.loc.start:definition.loc.end])
return [Document(text=chunk) for chunk in chunks]
1 change: 1 addition & 0 deletions llama_hub/file/sdl/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
graphql-core
5 changes: 5 additions & 0 deletions llama_hub/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
"author": "ephe-meral",
"keywords": ["spreadsheet"]
},
"SDLReader": {
"id": "file/sdl",
"author": "ajhofmann",
"keywords": ["graphql", "schema"]
},
"SimpleWebPageReader": {
"id": "web/simple_web",
"author": "thejessezhang"
Expand Down
Loading