@qualve/graphql 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ package-lock.json
2
+ package.json
package/.prettierrc ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "plugins": [
3
+ "prettier-plugin-brace-style",
4
+ "prettier-plugin-space-before-function-paren",
5
+ "prettier-plugin-merge"
6
+ ],
7
+ "braceStyle": "stroustrup",
8
+ "arrowParens": "avoid",
9
+ "bracketSpacing": true,
10
+ "endOfLine": "auto",
11
+ "semi": true,
12
+ "singleQuote": false,
13
+ "tabWidth": 4,
14
+ "useTabs": true,
15
+ "trailingComma": "all",
16
+ "printWidth": 100
17
+ }
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @qualve/graphql
2
+
3
+ GraphQL inputs for Qualve Tasks.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @qualve/graphql
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ In your `qualve.config.js`, import the `graphql` task:
14
+
15
+ ```js
16
+ import "@qualve/graphql";
17
+
18
+ export default {
19
+ graphql: {
20
+ endpoint: "https://api.example.com/graphql",
21
+ },
22
+ model: {
23
+ // ...
24
+ },
25
+ };
26
+ ```
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@qualve/graphql",
3
+ "version": "0.0.1",
4
+ "description": "GraphQL inputs for Qualve Tasks",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/qualve/graphql.git"
13
+ },
14
+ "author": "Lea Verou",
15
+ "contributors": [
16
+ "Dmitry Sharabin"
17
+ ],
18
+ "scripts": {
19
+ "release": "npm login && release-it"
20
+ },
21
+ "devDependencies": {
22
+ "prettier": "^3.8.1",
23
+ "prettier-plugin-brace-style": "^0.10.0",
24
+ "prettier-plugin-merge": "^0.10.0",
25
+ "prettier-plugin-space-before-function-paren": "^0.1.0"
26
+ },
27
+ "peerDependencies": {
28
+ "qualve": "*"
29
+ },
30
+ "dependencies": {
31
+ "release-it": "^19.2.4"
32
+ }
33
+ }
package/src/index.js ADDED
@@ -0,0 +1,48 @@
1
+ import Task from "qualve/task";
2
+ import { writeJSONSync } from "qualve/util";
3
+ import { stringifyQuery, runQuery } from "./util.js";
4
+
5
+ export default class GraphQLTask extends Task {
6
+ static type = "graphql";
7
+
8
+ async postInit () {
9
+ await super.postInit();
10
+
11
+ this.debug.endpoint = this.config.graphql?.endpoint;
12
+ }
13
+
14
+ get path () {
15
+ let path = this.config.graphql?.getPath?.call(this) ?? [];
16
+ Object.defineProperty(this, "path", { value: path, configurable: true });
17
+ return this.path;
18
+ }
19
+
20
+ /** Build the full GraphQL query string by wrapping `this.fields` in `this.path`. */
21
+ get query () {
22
+ let fields = this.path.reduceRight((acc, key) => ({ [key]: acc }), this.fields);
23
+ return stringifyQuery(fields, "query");
24
+ }
25
+
26
+ async runTask () {
27
+ let query = this.query;
28
+ let outputPath = this.output?.filePath;
29
+
30
+ if (this.dryRun) {
31
+ Object.assign(this.debug, { query, outputPath });
32
+ return;
33
+ }
34
+
35
+ let result = await runQuery(query, this.config.graphql?.endpoint);
36
+
37
+ if (result) {
38
+ result = this.path.reduce((acc, key) => acc[key], result.data);
39
+ result = this.handleResult?.(result) ?? result;
40
+
41
+ var size = outputPath ? writeJSONSync(outputPath, result)?.length : undefined;
42
+ }
43
+
44
+ return { result, query, outputPath, size };
45
+ }
46
+ }
47
+
48
+ Task.register(GraphQLTask);
package/src/util.js ADDED
@@ -0,0 +1,50 @@
1
+ export function stringifyQuery (value, key) {
2
+ if (key) {
3
+ if (typeof value === "object") {
4
+ return `${key} { ${stringifyQuery(value)} }`;
5
+ }
6
+ else if (typeof value === "string") {
7
+ return `${key} { ${value} }`;
8
+ }
9
+
10
+ return key;
11
+ }
12
+
13
+ if (Array.isArray(value)) {
14
+ return value.map(item => stringifyQuery(item)).join(" ");
15
+ }
16
+ if (typeof value === "object") {
17
+ return Object.entries(value)
18
+ .map(([key, value]) => stringifyQuery(value, key))
19
+ .join(" ");
20
+ }
21
+
22
+ return value;
23
+ }
24
+
25
+ export async function runQuery (query, endpoint) {
26
+ const response = await fetch(endpoint, {
27
+ method: "POST",
28
+ headers: {
29
+ "Content-Type": "application/json",
30
+ },
31
+ body: JSON.stringify({ query }),
32
+ });
33
+
34
+ try {
35
+ var json = await response.json();
36
+ }
37
+ catch (e) {
38
+ var text = await response.text();
39
+ }
40
+
41
+ if (!response.ok) {
42
+ let errors = json?.errors ?? [{ message: text }];
43
+ for (const error of errors) {
44
+ console.error(`GraphQL error: ${error.message}. Query: ${query}`);
45
+ }
46
+ return null;
47
+ }
48
+
49
+ return json;
50
+ }