Skip to content

Commit

Permalink
Pass headers to query/mutation if you need to simulate users response
Browse files Browse the repository at this point in the history
  • Loading branch information
mrspartak committed Jun 3, 2020
1 parent ce752aa commit 22a9972
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 8 additions & 1 deletion src/hasura/gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ 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',
data: {
query,
variables,
},
headers,
}),
);
if (err) {
Expand Down
10 changes: 7 additions & 3 deletions src/orm/hasura.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -236,6 +238,7 @@ class Hasura {
const [err, response] = await this.$gql.run({
query,
variables,
requestSettings,
});
if (err) {
return [err];
Expand Down Expand Up @@ -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) {
Expand All @@ -357,6 +360,7 @@ class Hasura {
const [err, response] = await this.$gql.run({
query,
variables,
requestSettings,
});
if (err) {
return [err];
Expand Down
58 changes: 58 additions & 0 deletions tests/real-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
49 changes: 49 additions & 0 deletions tests/request-libs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() => {
Expand Down

0 comments on commit 22a9972

Please sign in to comment.