diff options
author | John Estabrook <jestabro@vyos.io> | 2021-08-05 11:24:47 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2021-11-07 15:21:50 -0600 |
commit | f87f6c249535453b8bd3718dc7cdc84dcbbdbe13 (patch) | |
tree | 4a619ec7284294dfa8e70c608e423cb1b0674f45 /src/services/vyos-http-api-server | |
parent | 790131c0da5f2cf7274e98d20839b64e94423347 (diff) | |
download | vyos-1x-f87f6c249535453b8bd3718dc7cdc84dcbbdbe13.tar.gz vyos-1x-f87f6c249535453b8bd3718dc7cdc84dcbbdbe13.zip |
http-api: T2768: example using GraphQL for high-level config operations
(cherry picked from commit b168b4cc7da456f14714d917cdc7a1c6b8df9af5)
Diffstat (limited to 'src/services/vyos-http-api-server')
-rwxr-xr-x | src/services/vyos-http-api-server | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/services/vyos-http-api-server b/src/services/vyos-http-api-server index cbf321dc8..cb4ce4072 100755 --- a/src/services/vyos-http-api-server +++ b/src/services/vyos-http-api-server @@ -36,10 +36,16 @@ from starlette.datastructures import FormData, MutableHeaders from starlette.formparsers import FormParser, MultiPartParser from multipart.multipart import parse_options_header +from ariadne import make_executable_schema, load_schema_from_path, snake_case_fallback_resolvers +from ariadne.asgi import GraphQL + import vyos.config +import vyos.defaults from vyos.configsession import ConfigSession, ConfigSessionError +import api.graphql.state + DEFAULT_CONFIG_FILE = '/etc/vyos/http-api.conf' CFG_GROUP = 'vyattacfg' @@ -603,6 +609,25 @@ def show_op(data: ShowModel): return success(res) +### +# GraphQL integration +### + +api.graphql.state.init() + +from api.graphql.graphql.mutations import mutation +from api.graphql.graphql.directives import DataDirective + +api_schema_dir = vyos.defaults.directories['api_schema'] + +type_defs = load_schema_from_path(api_schema_dir) + +schema = make_executable_schema(type_defs, mutation, snake_case_fallback_resolvers, directives={"generate": DataDirective}) + +app.add_route('/graphql', GraphQL(schema, debug=True)) + +### + if __name__ == '__main__': # systemd's user and group options don't work, do it by hand here, # else no one else will be able to commit @@ -626,6 +651,8 @@ if __name__ == '__main__': app.state.vyos_debug = True if server_config['debug'] == 'true' else False app.state.vyos_strict = True if server_config['strict'] == 'true' else False + api.graphql.state.settings['app'] = app + try: uvicorn.run(app, host=server_config["listen_address"], port=int(server_config["port"]), |