ghgql

ghgql provides a thin wrapper library to query the Github GraphQL API.

Submodules

Package Contents

Classes

GithubGraphQL

A lightweight Github GraphQL API client.

Attributes

__version__

ghgql.__version__
class ghgql.GithubGraphQL(token: str = '', endpoint: str = 'https://api.github.com/graphql', **kwargs)[source]

A lightweight Github GraphQL API client.

In order to properly close the session, use this class as a context manager:

with GithubGraphQL(token=”<GITHUB_API_TOKEN>”) as g:

g.query_from_file(filename=”query.graphql”, variables=None)

or call the close() method manually

g = GithubGraphQL(token=”<GITHUB_API_TOKEN>”) g.close()

property token: str

Returns the bearer token.

property encoding: str

Returns the default encoding to be expected from query files.

property session_headers: requests.structures.CaseInsensitiveDict

Returns the HTTP headers used for the session.

query_from_file(filename: str, variables: Dict[str, Union[str, int]] = None, **kwargs) Any[source]

Read the query from the given file and execute it with the variables applied. If not requested otherwise the plain result is returned. If you want to raise an exception in case of an error you can set raise_on_error to True.

See also: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql https://docs.github.com/en/graphql/overview/explorer

Parameters
  • filename (str) – The filename of the query file.

  • variables (dict) – The variables to be applied to the query.

  • **kwargs – key-value pairs (e.g. {raise_on_error=True})

__enter__()[source]
__exit__(exc_type, exc_value, traceback)[source]
close()[source]

Closes the session.

query(query: str, variables: Dict[str, Union[str, int]] = None, **kwargs) Dict[source]

Execute the query with the variables applied. If not requested otherwise the plain result is returned. If you want to raise an exception in case of an error you can set raise_on_error to True.

Parameters
  • query (str) – The GraphQL query.

  • variables (dict) – The variables to be applied to the query.

  • **kwargs – key-value pairs (e.g. {raise_on_error=True})

Raises

RuntimeError – In case of an error when raise is True.

Returns

The result of the query. Inspect the result for errors!

Return type

Result