Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Publish v0.2.13 add first graphql version
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmeier committed Oct 28, 2023
1 parent 3a22739 commit 0d816d8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
## Fire Js
This contains various helpers and types.

## Unstable
the graphql module is unstable.

## Usage
```json
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fire-lib-js",
"version": "0.2.12",
"version": "0.2.13",
"author": "Sören Meier <[email protected]>",
"type": "module",
"scripts": {
Expand Down
58 changes: 58 additions & 0 deletions src/graphql/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import ApiError from '../api/error.js';

export default class GraphQlError {
/*
fields:
- kind: str,
- errors: should be one of the following
"error message"
{ message, ?extensions, ?locations }
[{ message, ?extensions, ?locations }]
- data: graphql data if it is available
extensions might contain apiError
*/
constructor(kind, errors, data = null) {
this.kind = kind;

if (typeof errors === 'string')
errors = [{ message: errors }];
if (Array.isArray(errors))
errors = errors;
else
errors = [errors];

this.errors = errors;
this.data = data;
}

static fromJson(json) {
let apiError = json.errors.find(e => !!e.extensions?.apiError);
if (apiError)
apiError = ApiError.fromJson(apiError.extensions.apiError);

return new GraphQlError(
apiError?.kind ?? 'Unknown',
json.errors,
json.data
);
}

static newOther(msg) {
return new GraphQlError('Other', msg);
}

get msg() {
return this.errors.map(e => e.message).join(', ');
}

toString() {
return `${this.kind}: ${this.msg}`;
}

__isGraphQlErrorObject__() {}
}

export function isGraphQlErrorObject(val) {
return typeof (val ? val.__isGraphQlErrorObject__ : null) === 'function';
}
50 changes: 50 additions & 0 deletions src/graphql/graphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import GraphQlError, { isGraphQlErrorObject } from './error.js';

export default class GraphQl {
constructor(url = null) {
this.url = url;
}

async request(query, variables = {}, headers = {}) {
if (!this.url)
throw GraphQlError.newOther('GraphQl url not defined');

headers['content-type'] = 'application/json';

let resp;
try {
resp = await fetch(this.url, {
headers,
method: 'POST',
body: JSON.stringify({
query, variables
})
});

const json = await resp.json();
if (
'errors' in json &&
Array.isArray(json.errors) &&
json.errors.length
)
throw GraphQlError.fromJson(json);

if (!resp.ok)
throw new GraphQlError('NotOk', 'response code ' + resp.status);

return json?.data ?? null;
} catch (e) {
if (isGraphQlErrorObject(e)) {
console.log('graphql Errors', e.toString());
throw e;
}

throw new GraphQlError('Unknown', {
message: e.toString(),
extensions: {
nativeError: e
}
});
}
}
}

0 comments on commit 0d816d8

Please sign in to comment.