Skip to content
Open
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
52 changes: 32 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,54 @@ jraph is written in typescript, but can be used in both a VanillaJS and a TypeSc

#### Queries
```js
import {jraph, JraphQuery} from 'jraph';
import {jraph} from 'jraph';

async function getItems(){
const JraphConfig = new JraphQuery({
url: "https://csb-xpwq1o2824-alczxhlphl.now.sh/",
options: {
const jql = jraph(
"https://csb-xpwq1o2824-alczxhlphl.now.sh/",
{
method: "POST"
},
query:`{
}
});
return (
jql`{
items{
title
info
}
}`
});
return jraph(JraphConfig);
);
}

async function showStuff(){
console.log(await getItems())
}
```

#### Mutations
```js
import {jraph, JraphMutation} from 'jraph';
import {jraph} from 'jraph';

async function addItems(){
const JraphConfig = new JraphMutation({
url: "https://csb-xpwq1o2824-alczxhlphl.now.sh/",
options: {
async function addItem(){
const jql = jraph(
"https://csb-xpwq1o2824-alczxhlphl.now.sh/",
{
method: "POST"
},
mutation:`{
addItem(title: "Eggs", info: "Cumberbatch"){
content
}
}`;
}
});
return jraph(JraphConfig);
return (
jql`
mutation{
addItem(title: "egg", info: "salad"){
title
info
}
}`
);
}

async function showStuff(){
console.log(await addItem())
}
```

Expand Down
93 changes: 55 additions & 38 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,65 @@
import es6 from 'es6-promise';
es6.polyfill();

import 'isomorphic-fetch';

interface JraphBasic{
url: string | Request;
options: any;
}
abstract class JraphQuery implements JraphBasic{
url: string = "";
options: any = {};
query: string = "";
constructor(args: any){
Object.assign(this, args);
if(typeof global !== 'undefined'){
const realFetch = require('node-fetch');
let fetch = function(url:any, options:any) {
if (/^\/\//.test(url)) {
url = 'https:' + url;
}
// @ts-ignore
return realFetch.call(this, url, options);
};
// @ts-ignore
if (!global.fetch) {
// @ts-ignore
global.fetch = fetch;
// @ts-ignore
global.Response = realFetch.Response;
// @ts-ignore
global.Headers = realFetch.Headers;
// @ts-ignore
global.Request = realFetch.Request;
}
}
abstract class JraphMutation implements JraphBasic{
url: string = "";
options: any = {};
mutation: string = "";
constructor(args: any){
Object.assign(this, args);
}

function cleanQueryString(string: string):string{
//This removes whitespaces and slashes and '\n's
return string.replace(/([\\][n])?([\s])+/g, " ");
}

type JraphOptions = JraphQuery | JraphMutation;
function prepareFetchBody(queryString: string):string{
return JSON.stringify({ query: queryString });
}

async function jraph(argv: JraphOptions){
const url : string | Request = argv.url;
function jraph(uri: string, args: object): any{
const headers = { 'Content-Type': 'application/json' };
const body = JSON.stringify( { query: ( (argv instanceof JraphQuery) ? argv.query : `mutation ${argv.mutation}`).replace(/([\\][n])?([\s])+/g, " ") } );
//(argv instanceof JraphQuery) ? {query: argv.query} : { query: `mutation ${argv.mutation.replace(/([\\][n])?([\s])+/g, " ")}`} );
const fetch_options = {
let fetch_options = {
method: "POST",
body: "",
...args,
headers,
body: body,
...argv.options
};
console.log(body);
const request = await fetch(url, fetch_options).then(res=>res.json());
return request;
return async (args: string[], ...values:any)=>{
let queryString = "";
args.forEach( (string, i) => {
queryString += string + (values[i] || '');
});
queryString = cleanQueryString(queryString);
let url = new URL(uri);
if(fetch_options.method && (fetch_options.method == "GET" || fetch_options.method == "get")) {
let searchParams = url.searchParams;
searchParams.append("query", queryString);
}else {
let body = prepareFetchBody(queryString);
fetch_options = {...fetch_options, body};
}
const request = await fetch(String(url), fetch_options).then(res=>res.text());
try{
let json = JSON.parse(request);
return json;
}catch(e){
return request;
}
};
}
/*
* Some notes for the next version
* - Nevermind, I had notes but they were redundant.
*/
export default jraph;
export { jraph, JraphOptions, JraphQuery, JraphMutation }

export { jraph }
34 changes: 4 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "jraph",
"version": "1.1.9",
"version": "2.0.1",
"description": "A fetch wrapper for GraphQL",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"version": "tsc && npm publish",
"test": "mocha --recursive \"./test/**/*.js\""
"pretest": "npm install && npm run build",
"test": "mocha --recursive \"./test/**/*.js\"",
"preversion": "npm test",
"version": "npm run build && git add -A .",
"postversion": "git push && git push --tags && npm publish"
},
"keywords": [
"graphql",
Expand All @@ -17,15 +20,14 @@
"author": "Ian Fabs",
"license": "MIT",
"repository": {
"url": "https://github.com/ianfabs/jraph.git",
"url": "https://github.com/ianfabs/jraph/tree/gql",
"type": "github"
},
"dependencies": {
"typescript": "^3.1.6",
"es6-promise": "^4.2.5",
"isomorphic-fetch": "^2.2.1"
"node-fetch": "^1.0.1"
},
"devDependencies": {
"typescript": "^3.1.6",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"mocha": "^5.2.0"
Expand Down
33 changes: 0 additions & 33 deletions test/examples/vanilla.mutation.js

This file was deleted.

33 changes: 0 additions & 33 deletions test/examples/vanilla.query.js

This file was deleted.

Loading