diff --git a/package.json b/package.json index 569becc..6f85d01 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ }, "husky": { "hooks": { - "pre-commit": "xo --fix && npx ava --tap | npx tap-dot && git add ." + "pre-commit": "xo --fix && ava --fail-fast" } }, "dependencies": { diff --git a/src/hasura/gql.js b/src/hasura/gql.js index 299c7bc..d697a71 100644 --- a/src/hasura/gql.js +++ b/src/hasura/gql.js @@ -32,7 +32,13 @@ class Gql { }); } - async run({query, variables}) { + /* + RequestSettings + headers = {} + */ + async run({query, variables, requestSettings = {}}) { + const headers = __.mergeDeep({}, this.$http.defaults.headers, requestSettings.headers || {}); + const [err, {data} = {}] = await __.to( this.$http.request({ method: 'POST', @@ -40,6 +46,7 @@ class Gql { query, variables, }, + headers, }), ); if (err) { diff --git a/src/orm/hasura.js b/src/orm/hasura.js index fb7cd61..11a549b 100644 --- a/src/orm/hasura.js +++ b/src/orm/hasura.js @@ -219,10 +219,12 @@ class Hasura { fields fragment - } + }, + { }, + { headers: { } } } */ - async query(parameters, settings = {}) { + async query(parameters, settings = {}, requestSettings = {}) { try { var [query, variables, flatSettings] = this.buildQuery(parameters); } catch (error) { @@ -236,6 +238,7 @@ class Hasura { const [err, response] = await this.$gql.run({ query, variables, + requestSettings, }); if (err) { return [err]; @@ -345,7 +348,7 @@ class Hasura { } } */ - async mutate(parameters, settings = {}) { + async mutate(parameters, settings = {}, requestSettings = {}) { try { var [query, variables, flatSettings] = this.buildMutation(parameters); } catch (error) { @@ -357,6 +360,7 @@ class Hasura { const [err, response] = await this.$gql.run({ query, variables, + requestSettings, }); if (err) { return [err]; diff --git a/tests/real-query.js b/tests/real-query.js index 75e07c1..6d93227 100644 --- a/tests/real-query.js +++ b/tests/real-query.js @@ -346,3 +346,61 @@ test.serial('simple add/update row', async (t) => { t.is(typeof response, 'object'); t.is(response.text, 'test_123213123123'); }); + +test.serial('queries with user role', async (t) => { + const orm = t.context.orm; + + // Base fragment has type field in it + var [err, response] = await orm.query( + { + _om_test: {}, + }, + {}, + { + headers: { + 'X-Hasura-User-ID': 1, + 'X-Hasura-Role': 'user', + }, + }, + ); + t.is(err.message, 'field "type" not found in type: \'_om_test\''); + t.is(response, undefined); + + var [err, response] = await orm.query( + { + _om_test: { + fields: ['id'], + }, + }, + {}, + { + headers: { + 'X-Hasura-User-ID': 1, + 'X-Hasura-Role': 'user', + }, + }, + ); + t.is(err, null); + t.true(response.length > 0); + + // Base fragment has type field in it + var [err, response] = await orm.mutate( + { + _om_test: { + insert: { + objects: { + text: 'test 4', + }, + }, + }, + }, + {}, + { + headers: { + 'X-Hasura-User-ID': 1, + 'X-Hasura-Role': 'user', + }, + }, + ); + t.is(err.message, 'no mutations exist'); +}); diff --git a/tests/request-libs.js b/tests/request-libs.js index cef477d..237ba78 100644 --- a/tests/request-libs.js +++ b/tests/request-libs.js @@ -153,6 +153,55 @@ test('Gql success query after config update', async (t) => { t.is(request.params.settings.test, 1); }); +test('Gql request settings on run', async (t) => { + const request = new Gql({ + graphqlUrl: process.env.GQL_ENDPOINT, + adminSecret: process.env.GQL_SECRET, + }); + + var [err, data] = await request.run({ + query: ` + query TestQuery { + _om_test(limit: 1) { + id + text + increment + } + } + `, + requestSettings: { + headers: { + 'X-Hasura-User-ID': 1, + 'X-Hasura-Role': 'user', + }, + }, + }); + t.is(err, null); + t.true(data._om_test.length > 0); + + // Field type in not alllowed to user role + var [err, data] = await request.run({ + query: ` + query TestQuery { + _om_test(limit: 1) { + id + text + increment + type + } + } + `, + requestSettings: { + headers: { + 'X-Hasura-User-ID': 1, + 'X-Hasura-Role': 'user', + }, + }, + }); + t.is(err.message, 'field "type" not found in type: \'_om_test\''); + t.is(data, undefined); +}); + test('Wsgql throws without params', (t) => { t.throws( () => {