跳到内容

GraphQL

GraphQLReader #

基类:BaseReader

GraphQL 阅读器。

将所有 GraphQL 结果合并到 LlamaIndex 使用的 Document 中。

参数

名称 类型 描述 默认值
uri str

GraphQL uri。

headers 可选[Dict]

可选的 http headers。

源代码位于 llama-index-integrations/readers/llama-index-readers-graphql/llama_index/readers/graphql/base.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class GraphQLReader(BaseReader):
    """
    GraphQL reader.

    Combines all GraphQL results into the Document used by LlamaIndex.

    Args:
        uri (str): GraphQL uri.
        headers (Optional[Dict]): Optional http headers.

    """

    def __init__(
        self,
        uri: Optional[str] = None,
        headers: Optional[Dict] = None,
    ) -> None:
        """Initialize with parameters."""
        try:
            from gql import Client
            from gql.transport.requests import RequestsHTTPTransport

        except ImportError:
            raise ImportError("`gql` package not found, please run `pip install gql`")
        if uri:
            if uri is None:
                raise ValueError("`uri` must be provided.")
            if headers is None:
                headers = {}
            transport = RequestsHTTPTransport(url=uri, headers=headers)
            self.client = Client(transport=transport, fetch_schema_from_transport=True)

    def load_data(self, query: str, variables: Optional[Dict] = None) -> List[Document]:
        """
        Run query with optional variables and turn results into documents.

        Args:
            query (str): GraphQL query string.
            variables (Optional[Dict]): optional query parameters.

        Returns:
            List[Document]: A list of documents.

        """
        try:
            from gql import gql

        except ImportError:
            raise ImportError("`gql` package not found, please run `pip install gql`")
        if variables is None:
            variables = {}

        documents = []

        result = self.client.execute(gql(query), variable_values=variables)

        for key in result:
            entry = result[key]
            if isinstance(entry, list):
                documents.extend([Document(text=yaml.dump(v)) for v in entry])
            else:
                documents.append(Document(text=yaml.dump(entry)))

        return documents

load_data #

load_data(query: str, variables: Optional[Dict] = None) -> List[Document]

运行带有可选变量的查询,并将结果转换为文档。

参数

名称 类型 描述 默认值
query str

GraphQL 查询字符串。

必需
variables 可选[Dict]

可选的查询参数。

返回值

类型 描述
List[Document]

List[Document]: 文档列表。

源代码位于 llama-index-integrations/readers/llama-index-readers-graphql/llama_index/readers/graphql/base.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def load_data(self, query: str, variables: Optional[Dict] = None) -> List[Document]:
    """
    Run query with optional variables and turn results into documents.

    Args:
        query (str): GraphQL query string.
        variables (Optional[Dict]): optional query parameters.

    Returns:
        List[Document]: A list of documents.

    """
    try:
        from gql import gql

    except ImportError:
        raise ImportError("`gql` package not found, please run `pip install gql`")
    if variables is None:
        variables = {}

    documents = []

    result = self.client.execute(gql(query), variable_values=variables)

    for key in result:
        entry = result[key]
        if isinstance(entry, list):
            documents.extend([Document(text=yaml.dump(v)) for v in entry])
        else:
            documents.append(Document(text=yaml.dump(entry)))

    return documents