Skip to content

Commit f01d4bc

Browse files
committed
codebase: extend typeshi integration to graphql queries
1 parent 71ccea3 commit f01d4bc

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

cli/main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
import sys
3+
import json
34
import click
45
import typeshi
56
import subprocess
67
from lib.structs.proxies.dict_proxy import DictProxy
8+
from lib.structs.proxies.dir_proxy import DirProxy
79
from .config import PYTHON_COMMAND_LINE, APP_ROOT_DIR
810
from .scripts import run_help_helper
911

@@ -43,6 +45,19 @@ def generate_locale_defs():
4345
)
4446
print(f'Wrote locale schema to resources/gen/locale_schema.py')
4547

48+
@dev.command('generate-graphql-query-def')
49+
def generate_query_dirproxy_def():
50+
if not os.path.exists('resources/gen/'):
51+
os.makedirs('resources/gen/')
52+
query_names: list[str] = list(map(lambda fn: fn[:-8], filter(lambda fn: fn.endswith('.graphql'), os.listdir('resources/queries/'))))
53+
with open('tmp/gql.json', 'w+') as f:
54+
json.dump({q: '_str' for q in query_names}, f)
55+
typeshi.save_declaration_module_from_json(
56+
'GraphQLQueries', 'tmp/gql.json', 'resources/gen/gql_queries_schema.py',
57+
inherit_cls=DirProxy, literals_where_possible=False
58+
)
59+
os.remove('tmp/gql.json')
60+
print(f'Wrote query directory schema to resources/gen/gql_queries_schema.py')
4661

4762
@dev.command('update', help='Update the local code using git')
4863
def update():

lib/api/github/github.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from typing import Optional, Callable, Any, Literal, TYPE_CHECKING
1717
from gidgethub import BadRequest, QueryError
1818
import datetime
19-
from lib.structs import DirProxy, TypedCache, CacheSchema, DictProxy, SnakeCaseDictProxy
19+
from lib.structs import TypedCache, CacheSchema, DictProxy, SnakeCaseDictProxy
2020
from lib.utils.decorators import normalize_repository, validate_github_name
21-
from lib.typehints import GitHubRepository, GitHubOrganization, GitHubUser
21+
from lib.typehints import GitHubRepository, GitHubOrganization, GitHubUser, GraphQLQueriesDirProxyDef
2222
from lib.utils import get_nested_key, get_all_dict_paths, set_nested_key
2323
from cogs.backend.handle.errors._error_tools import log_error_in_discord
2424
from .transformations import *
@@ -204,7 +204,7 @@ class GitHubAPI:
204204
def __init__(self, bot: 'GitBot', token: str, session: aiohttp.ClientSession):
205205
self.bot: 'GitBot' = bot
206206
self.__token: str = token
207-
self.queries: DirProxy = DirProxy('./resources/queries/', ('.gql', '.graphql'))
207+
self.queries: GraphQLQueriesDirProxyDef = GraphQLQueriesDirProxyDef('./resources/queries/', ('.gql', '.graphql'))
208208
self.session: aiohttp.ClientSession = session
209209
self.gh: gh.GitHubAPI = gh.GitHubAPI(session=self.session, requester=self.requester, oauth_token=self.__token)
210210

lib/typehints/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from lib.typehints.db.guild.autoconv import AutomaticConversionSettings
77
from lib.typehints.generic import *
88
from lib.typehints.locale.help import *
9-
from lib.typehints.locale.localedef_wfallback import LocaleDictProxyDef
9+
from lib.typehints.resource_defs_wfallbacks import *
1010
from lib.typehints.db.guild.release_feed import *
1111
from lib.typehints.gitbot_config import *
1212

@@ -37,7 +37,8 @@
3737
'CratesIOCrate',
3838
'ReleaseFeedItemMention',
3939
'GitbotRepoConfig',
40-
'LocaleDictProxyDef'
40+
'LocaleDictProxyDef',
41+
'GraphQLQueriesDirProxyDef'
4142
)
4243

4344
AnyDict = dict | DictProxy | CaseInsensitiveDict | MaxAgeDict | FixedSizeOrderedDict

lib/typehints/locale/localedef_wfallback.py

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# coding: utf-8
22

33
from lib.structs.proxies.dict_proxy import DictProxy
4+
from lib.structs.proxies.dir_proxy import DirProxy
45

56
try:
67
from resources.gen.locale_schema import Locale as LocaleDictProxyDef
78
except ImportError:
89
LocaleDictProxyDef = DictProxy
910

1011
try:
11-
from resources.gen.env_defaults_schema import EnvDefaults as EnvDefaultsProxyDef
12+
from resources.gen.gql_queries_schema import GraphQLQueries as GraphQLQueriesDirProxyDef
1213
except ImportError:
13-
EnvDefaultsProxyDef = DictProxy
14+
GraphQLQueriesDirProxyDef = DirProxy
1415

1516

16-
__all__: tuple = ('LocaleDictProxyDef', 'EnvDefaultsProxyDef')
17+
__all__: tuple = ('LocaleDictProxyDef', 'GraphQLQueriesDirProxyDef')

0 commit comments

Comments
 (0)