Skip to content

Commit 3cb7247

Browse files
committed
Basic logic to load the schema
1 parent 8691357 commit 3cb7247

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

src/actions/action.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export default class Action {
2626
protected static async mutation (name: string, variables: Data | undefined, dispatch: DispatchFunction,
2727
model: Model, multiple: boolean = false): Promise<any> {
2828
if (variables) {
29+
const context = Context.getInstance();
30+
await context.loadSchema();
31+
2932
const query = QueryBuilder.buildQuery('mutation', model, name, variables, multiple);
3033

3134
// Send GraphQL Mutation

src/actions/fetch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export default class Fetch extends Action {
1818
*/
1919
public static async call ({ state, dispatch }: ActionParams, params?: ActionParams): Promise<Data> {
2020
const context = Context.getInstance();
21+
await context.loadSchema();
22+
2123
const model = this.getModelFromState(state);
2224

2325
// Filter

src/actions/query.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default class Query extends Action {
2323
{ name, multiple, filter, bypassCache }: ActionParams): Promise<Data> {
2424
if (name) {
2525
const context = Context.getInstance();
26+
await context.loadSchema();
27+
2628
const model = this.getModelFromState(state);
2729

2830
// Filter

src/common/context.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Components } from '@vuex-orm/core/lib/plugins/use';
55
import { downcaseFirstLetter } from '../support/utils';
66
import Apollo from '../graphql/apollo';
77
import Database from '@vuex-orm/core/lib/database/Database';
8-
import { Options } from '../support/interfaces';
8+
import {Data, Options, Schema} from '../support/interfaces';
9+
import { introspectionQuery } from 'graphql';
910
const inflection = require('inflection');
1011

1112
/**
@@ -61,6 +62,11 @@ export default class Context {
6162
*/
6263
public apollo!: Apollo;
6364

65+
/**
66+
* The graphql schema. Is null until the first request.
67+
*/
68+
private schema: Schema | undefined;
69+
6470
/**
6571
* Private constructor, called by the setup method
6672
*
@@ -111,6 +117,17 @@ export default class Context {
111117
return this.instance;
112118
}
113119

120+
public async loadSchema () {
121+
if (!this.schema) {
122+
this.logger.log('Fetching GraphQL Schema initially ...');
123+
124+
const result = await this.apollo.simpleQuery(introspectionQuery, {}, true);
125+
this.schema = result.data.__schema;
126+
127+
this.logger.log('GraphQL Schema successful fetched', this.schema);
128+
}
129+
}
130+
114131
/**
115132
* Returns a model from the model collection by it's name
116133
*

src/support/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export interface Arguments extends Object {
4343
[index: string]: any;
4444
}
4545

46+
export interface Schema extends Object {
47+
[index: string]: any;
48+
}
49+
4650
export interface Field {
4751
related?: ORMModel;
4852
parent?: ORMModel;

0 commit comments

Comments
 (0)