This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish v0.2.13 add first graphql version
- Loading branch information
1 parent
3a22739
commit 0d816d8
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
} | ||
} | ||
} |