From 1bcde6577c25974e15d772971877c79cc1f548fb Mon Sep 17 00:00:00 2001 From: Serhii Matrunchyk Date: Fri, 22 May 2020 03:58:14 +0300 Subject: [PATCH] feat(graphql): add graphql schema autoloading support --- package-lock.json | 47 ++++++++++++++++++++++++++++++++--------------- package.json | 6 ++++-- src/index.ts | 35 +++++++++++++++++++++++++++++------ src/utils.ts | 13 ++++++++++++- 4 files changed, 77 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index dc351fc..b3e315a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "vue-oop", - "version": "0.6.6", + "version": "0.6.8", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3446,7 +3446,6 @@ "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", - "dev": true, "requires": { "color-convert": "^1.9.0" } @@ -4291,7 +4290,6 @@ "version": "2.4.1", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", - "dev": true, "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -4543,7 +4541,6 @@ "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", - "dev": true, "requires": { "color-name": "1.1.3" } @@ -4551,8 +4548,7 @@ "color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", - "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", - "dev": true + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, "colors": { "version": "1.4.0", @@ -6361,8 +6357,7 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "escodegen": { "version": "1.12.0", @@ -7058,6 +7053,27 @@ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", "dev": true }, + "get-graphql-schema": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/get-graphql-schema/-/get-graphql-schema-2.1.2.tgz", + "integrity": "sha512-1z5Hw91VrE3GrpCZE6lE8Dy+jz4kXWesLS7rCSjwOxf5BOcIedAZeTUJRIeIzmmR+PA9CKOkPTYFRJbdgUtrxA==", + "requires": { + "chalk": "^2.4.1", + "graphql": "^14.0.2", + "minimist": "^1.2.0", + "node-fetch": "^2.2.0" + }, + "dependencies": { + "graphql": { + "version": "14.6.0", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-14.6.0.tgz", + "integrity": "sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg==", + "requires": { + "iterall": "^1.2.2" + } + } + } + }, "get-package-type": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/get-package-type/-/get-package-type-0.1.0.tgz", @@ -7905,8 +7921,7 @@ "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", - "dev": true + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, "has-value": { "version": "1.0.0", @@ -8890,6 +8905,11 @@ "istanbul-lib-report": "^3.0.0" } }, + "iterall": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/iterall/-/iterall-1.3.0.tgz", + "integrity": "sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==" + }, "jest": { "version": "26.0.1", "resolved": "https://registry.npmjs.org/jest/-/jest-26.0.1.tgz", @@ -12000,8 +12020,7 @@ "minimist": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", - "dev": true + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" }, "minimist-options": { "version": "3.0.2", @@ -12267,8 +12286,7 @@ "node-fetch": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", - "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==", - "dev": true + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" }, "node-int64": { "version": "0.4.0", @@ -15503,7 +15521,6 @@ "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", - "dev": true, "requires": { "has-flag": "^3.0.0" } diff --git a/package.json b/package.json index 9218d78..5f1bc23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-oop", - "version": "0.6.8", + "version": "0.7.0-beta", "description": "A library based on Model-Repository patterns for Vue components. Usable for GraphQL and REST APIs.", "keywords": [ "collections", @@ -44,6 +44,7 @@ }, "dependencies": { "collect.js": "^4.25.0", + "get-graphql-schema": "^2.1.2", "graphql": "^15.0.0", "graphql-tag": "^2.10.3", "lodash.camelcase": "^4.3.0", @@ -108,5 +109,6 @@ "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, - "snyk": true + "snyk": true, + "sideEffects": false } diff --git a/src/index.ts b/src/index.ts index 097e00b..f7fd94d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ /*! - * VueOOP.js v0.2.1 - * (c) 2017-2019 Serhii Matrunchyk + * VueOOP + * (c) 2017-2020 Serhii Matrunchyk * Released under the MIT License. */ import _Vue from 'vue'; @@ -8,15 +8,24 @@ import Model from './models/Model'; import Repository from './repositories/Repository'; import Registry from './Registry'; import * as Utils from './utils'; -import { DocumentNode } from "graphql"; +import { DocumentNode, buildClientSchema } from "graphql"; +import { fetchIntrospectionSchema } from './utils'; const registry = Registry.getInstance(); registry.set('Model', Model); registry.set('Repository', Repository); +export interface IVueOOPOptions { + rest?: boolean; + graphql?: boolean; + schema?: DocumentNode; + schemaUrl?: string; + debug?: boolean; +} + // istanbul ignore next -export class VueOOPOptions { +export class VueOOPOptions implements IVueOOPOptions { /** * Use REST plugin. * @@ -38,6 +47,13 @@ export class VueOOPOptions { */ schema: DocumentNode = null; + /** + * URL to fetch introspection GraphQL Schema from. + * + * @type {null|string} + */ + schemaUrl: string = null; + /** * Debug mode * @@ -46,14 +62,21 @@ export class VueOOPOptions { debug = false; } -function VueOOP(Vue: typeof _Vue, options?: VueOOPOptions): void { +async function VueOOP(Vue: typeof _Vue, options?: VueOOPOptions): Promise { const config = { rest: true, graphql: false, schema: null, + schemaUrl: null, debug: false, ...options, - }; + } as IVueOOPOptions; + + if (config.schemaUrl && config.debug) { + const test = await fetchIntrospectionSchema(config.schemaUrl).then(buildClientSchema.bind(null)); + console.log(test); + console.log(config.schema); + } registry.set('Config', config); diff --git a/src/utils.ts b/src/utils.ts index 7ff5749..1d33c64 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ -import { ObjectTypeDefinitionNode, DocumentNode } from 'graphql'; +import { ObjectTypeDefinitionNode, DocumentNode, IntrospectionQuery } from 'graphql'; import { Config, KeyValueUnknown, ResolvingRESTOptions } from './typings'; import { Vue } from 'vue/types/vue'; +import { getIntrospectionQuery } from 'graphql'; import Registry from './Registry'; import UnexpectedException from './models/Exceptions/UnexpectedException'; @@ -207,3 +208,13 @@ export async function getUrl(_opts: ResolvingRESTOptions) { export function stripObject(obj) { return JSON.parse(JSON.stringify(obj, (k, v) => (k === 'loading' ? undefined : v))); } + +export function fetchIntrospectionSchema(url: string): Promise { + return fetch(url, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ query: getIntrospectionQuery }) + }) + .then(res => res.json()) + .then(res => res.data); +}