diff --git a/src/hasura/gql.js b/src/hasura/gql.js index 1d0d664..299c7bc 100644 --- a/src/hasura/gql.js +++ b/src/hasura/gql.js @@ -20,27 +20,15 @@ class Gql { throw new TypeError('graphqlUrl must be Url format'); } - let headers = { - 'Content-Type': 'application/json', - }; - if (this.params.adminSecret) { - headers = { - ...headers, - 'X-Hasura-Role': 'admin', - 'x-hasura-admin-secret': this.params.adminSecret, - }; - } else if (this.params.jwt) { - headers = { - ...headers, - 'X-Hasura-Role': this.params.hasuraRole || 'user', - Authorization: `Bearer ${this.params.jwt}`, - }; - } + this.$http = axios.create(this.__generateAxiosConfig()); + } - this.$http = axios.create({ - baseURL: this.params.graphqlUrl, - headers, - json: true, + updateParams(parameters) { + this.params = __.mergeDeep({}, this.params, parameters); + + const newAxiosConfig = this.__generateAxiosConfig(); + Object.keys(newAxiosConfig).forEach((key) => { + this.$http.defaults[key] = newAxiosConfig[key]; }); } @@ -64,6 +52,31 @@ class Gql { return [null, data.data]; } + + __generateAxiosConfig() { + let headers = { + 'Content-Type': 'application/json', + }; + if (this.params.adminSecret) { + headers = { + ...headers, + 'X-Hasura-Role': 'admin', + 'x-hasura-admin-secret': this.params.adminSecret, + }; + } else if (this.params.jwt) { + headers = { + ...headers, + 'X-Hasura-Role': this.params.hasuraRole || 'user', + Authorization: `Bearer ${this.params.jwt}`, + }; + } + + return { + baseURL: this.params.graphqlUrl, + headers, + json: true, + }; + } } module.exports = Gql; diff --git a/src/hasura/query.js b/src/hasura/query.js index 4c4ddae..1ad0404 100644 --- a/src/hasura/query.js +++ b/src/hasura/query.js @@ -18,14 +18,15 @@ class Query { throw new TypeError('queryUrl must be Url format'); } - this.$http = axios.create({ - baseURL: this.params.queryUrl, - headers: { - 'Content-Type': 'application/json', - 'X-Hasura-Role': 'admin', - 'x-hasura-admin-secret': this.params.adminSecret, - }, - json: true, + this.$http = axios.create(this.__generateAxiosConfig()); + } + + updateParams(parameters) { + this.params = __.mergeDeep({}, this.params, parameters); + + const newAxiosConfig = this.__generateAxiosConfig(); + Object.keys(newAxiosConfig).forEach((key) => { + this.$http.defaults[key] = newAxiosConfig[key]; }); } @@ -63,6 +64,18 @@ class Query { return [null, result]; } + + __generateAxiosConfig() { + return { + baseURL: this.params.queryUrl, + headers: { + 'Content-Type': 'application/json', + 'X-Hasura-Role': 'admin', + 'x-hasura-admin-secret': this.params.adminSecret, + }, + json: true, + }; + } } module.exports = Query; diff --git a/src/hasura/wsgql.js b/src/hasura/wsgql.js index 79aeabf..e5a6e15 100644 --- a/src/hasura/wsgql.js +++ b/src/hasura/wsgql.js @@ -73,6 +73,10 @@ class WsGql extends EventEmitter { }); } + updateParams(parameters) { + this.params = mergeDeep({}, this.params, parameters); + } + run({query, variables, callback, flat = (data) => data}) { if (typeof query !== 'string') { throw new TypeError('query must be a string'); diff --git a/src/orm/hasura.js b/src/orm/hasura.js index 764e180..8afaf02 100644 --- a/src/orm/hasura.js +++ b/src/orm/hasura.js @@ -92,6 +92,30 @@ class Hasura { this.tables = {}; } + updateParams(parameters) { + this.params = __.mergeDeep({}, this.params, parameters); + + this.$query.updateParams({ + queryUrl: this.params.queryUrl, + adminSecret: this.params.adminSecret, + settings: this.params.sqlConnectionSettings, + }); + this.$gql.updateParams({ + graphqlUrl: this.params.graphqlUrl, + adminSecret: this.params.adminSecret, + hasuraRole: this.params.hasuraRole, + jwt: this.params.jwt, + settings: this.params.gqlConnectionSettings, + }); + this.$ws.updateParams({ + wsUrl: this.params.wsUrl, + adminSecret: this.params.adminSecret, + hasuraRole: this.params.hasuraRole, + jwt: this.params.jwt, + settings: this.params.wsConnectionSettings, + }); + } + async init() { console.warn('this method is changed! Please use generateTablesFromAPI instead'); await this.generateTablesFromAPI(); diff --git a/tests/request-libs.js b/tests/request-libs.js index df31d0d..cef477d 100644 --- a/tests/request-libs.js +++ b/tests/request-libs.js @@ -46,6 +46,27 @@ test('Sql success query', async (t) => { t.deepEqual(data, [{test: '1'}]); }); +test('Sql success query after config update', async (t) => { + const request = new Query({ + queryUrl: process.env.GQL_ENDPOINT.replace('/v1/graphql', '/v1/query'), + adminSecret: process.env.GQL_SECRET, + }); + + request.updateParams({ + queryUrl: process.env.GQL_ENDPOINT.replace('/v1/graphql', '/v1/query'), + settings: { + test: 1, + }, + }); + + const [err, data] = await request.run('run_sql', { + sql: 'SELECT 1 as test', + }); + t.is(err, null); + t.deepEqual(data, [{test: '1'}]); + t.is(request.params.settings.test, 1); +}); + test('Gql throws without params', (t) => { t.throws( () => { @@ -105,6 +126,33 @@ test('Gql success query', async (t) => { t.true(data._om_test.length >= 0); }); +test('Gql success query after config update', async (t) => { + const request = new Gql({ + graphqlUrl: process.env.GQL_ENDPOINT, + adminSecret: process.env.GQL_SECRET, + }); + + request.updateParams({ + graphqlUrl: process.env.GQL_ENDPOINT, + settings: { + test: 1, + }, + }); + + const [err, data] = await request.run({ + query: ` + query TestQuery { + _om_test(limit: 1) { + id + } + } + `, + }); + t.is(err, null); + t.true(data._om_test.length >= 0); + t.is(request.params.settings.test, 1); +}); + test('Wsgql throws without params', (t) => { t.throws( () => {