Skip to content

Commit

Permalink
feat(schema): add schema autoloading via introspection query
Browse files Browse the repository at this point in the history
Schema can be auto-loaded by providing schemaUrl option
  • Loading branch information
matrunchyk committed May 23, 2020
1 parent f88c256 commit 469cda9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-oop",
"version": "0.7.0-beta.2",
"version": "0.7.0",
"description": "A library based on Model-Repository patterns for Vue components. Usable for GraphQL and REST APIs.",
"keywords": [
"collections",
Expand Down Expand Up @@ -40,7 +40,7 @@
"publish-github": "npm publish --registry https://npm.pkg.github.com/@matrunchyk",
"release": "release-it",
"snyk-protect": "snyk protect",
"prepare": "npm run snyk-protect"
"prepare": "exit 0 || npm run snyk-protect"
},
"dependencies": {
"collect.js": "^4.25.0",
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import Model from './models/Model';
import Repository from './repositories/Repository';
import Registry from './Registry';
import * as Utils from './utils';
import { DocumentNode, buildClientSchema } from "graphql";
import { DocumentNode, buildClientSchema, printSchema } from 'graphql';
import { parse } from 'graphql/language/parser';
import { fetchIntrospectionSchema } from './utils';

const registry = Registry.getInstance();
Expand Down Expand Up @@ -73,9 +74,10 @@ async function VueOOP<VueOOPOptions>(Vue: typeof _Vue, options?: VueOOPOptions):
} as IVueOOPOptions;

if (config.schemaUrl && config.debug) {
const test = await fetchIntrospectionSchema(config.schemaUrl).then(buildClientSchema.bind(null));
console.log(test);
console.log(config.schema);
config.schema = await fetchIntrospectionSchema(config.schemaUrl)
.then(buildClientSchema.bind(null))
.then(printSchema.bind(null))
.then(parse.bind(null));
}

registry.set('Config', config);
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ export function stripObject(obj) {
}

export function fetchIntrospectionSchema(url: string): Promise<IntrospectionQuery> {
const body = JSON.stringify({ query: getIntrospectionQuery() });

return fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: getIntrospectionQuery })
body
})
.then(res => res.json())
.then(res => res.data);
Expand Down

0 comments on commit 469cda9

Please sign in to comment.