Skip to content

Commit

Permalink
update params
Browse files Browse the repository at this point in the history
  • Loading branch information
mrspartak committed May 29, 2020
1 parent bfbd27a commit 2e9a963
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 28 deletions.
53 changes: 33 additions & 20 deletions src/hasura/gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
}

Expand All @@ -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;
29 changes: 21 additions & 8 deletions src/hasura/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
}

Expand Down Expand Up @@ -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;
4 changes: 4 additions & 0 deletions src/hasura/wsgql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
24 changes: 24 additions & 0 deletions src/orm/hasura.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
48 changes: 48 additions & 0 deletions tests/request-libs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() => {
Expand Down Expand Up @@ -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(
() => {
Expand Down

0 comments on commit 2e9a963

Please sign in to comment.