Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setExtraHeader and setTimeout method and fixes depreciation warning #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ AxioQL.setQLEndpoint('https://graphql.server.com/graphql');
// If you have to authenticate use this method to set the header
AxioQL.setAuthHeader('Bearer #sometoken#');

// If you wish to add fields to the HTTP header use this method
AxioQL.setExtraHeader('someHeaderFieldName', 'some value');


// Create a query
const someQuery = `query ($searchText: String!) {
productSearch(title: $searchText) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "axioql",
"description": "GraphQL client that uses axios under the hood.",
"version": "1.3.2",
"version": "1.4.0",
"repository": "https://github.com/urbancvek/axioql",
"license": "MIT",
"main": "dist/index.js",
"scripts": {
"lint": "eslint .",
"flow": "flow check .",
"prepublish": "mkdir dist && babel ./src --out-dir ./dist"
"prepublish": "rm -frd ./dist && mkdir dist && babel ./src --out-dir ./dist"
},
"devDependencies": {
"@shopsterdotio/eslint-config": "^1.0.3",
Expand All @@ -21,6 +21,7 @@
},
"dependencies": {
"axios": "^0.15.3",
"graphql": "^0.10.5",
"graphql-tag": "^1.2.3"
}
}
17 changes: 16 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Request = {
class AxioQL {
endpoint: ?string;
authHeader: ?string;
extraHeaders: ?Object;
timeout: ?number;

setQLEndpoint(endpoint: string) {
this.endpoint = endpoint;
Expand All @@ -20,6 +22,18 @@ class AxioQL {
this.authHeader = header;
}

setExtraHeader(headerName: string, value: string) {
// Create extraHeaders property if it doens't exist
if (!this.extraHeaders) {
this.extraHeaders = {};
}
this.extraHeaders[headerName] = value;
}

setTimeout(seconds: number) {
this.timeout = seconds;
}

async request({ query, variables }: Request = { query: null, variables: null }) {
if (!query) throw new Error('Query is required!');
if (!this.endpoint) throw new Error('Endpoint is required. Use AxioQL.setQLEndpoint(endpoint: string) method.');
Expand All @@ -31,7 +45,8 @@ class AxioQL {
const response = await axios.post(
this.endpoint,
{ query: modifiedQuery, variables: stringifiedVariables },
this.authHeader ? { headers: { 'Authorization': this.authHeader } } : {},
this.authHeader ? { headers: { ...this.extraHeaders, Authorization: this.authHeader } } : {},
{ timeout: this.timeout ? this.timeout : 1000 }
);

return response;
Expand Down
2 changes: 1 addition & 1 deletion src/queryModifier.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import gql from 'graphql-tag';
import { print } from 'graphql-tag/printer';
import { print } from 'graphql/language/printer';

const TYPENAME_FIELD = {
kind: 'Field',
Expand Down