Skip to content

Commit a2fcd70

Browse files
committed
Make tests work again
1 parent 752a4af commit a2fcd70

File tree

5 files changed

+1082
-3934
lines changed

5 files changed

+1082
-3934
lines changed

src/common/context.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,40 @@ import { downcaseFirstLetter } from '../support/utils';
66
import Apollo from '../graphql/apollo';
77
import Database from '@vuex-orm/core/lib/database/Database';
88
import { Data, Options, Schema } from '../support/interfaces';
9-
import { introspectionQuery } from 'graphql';
109
const inflection = require('inflection');
1110

11+
const introspectionQuery = `
12+
query {
13+
__schema {
14+
types {
15+
name
16+
description
17+
18+
fields(includeDeprecated: true) {
19+
name
20+
description
21+
22+
type {
23+
name
24+
kind
25+
}
26+
}
27+
28+
29+
inputFields {
30+
name
31+
description
32+
33+
type {
34+
name
35+
kind
36+
}
37+
}
38+
}
39+
}
40+
}
41+
`;
42+
1243
/**
1344
* Internal context of the plugin. This class contains all information, the models, database, logger and so on.
1445
*
@@ -121,7 +152,12 @@ export default class Context {
121152
if (!this.schema) {
122153
this.logger.log('Fetching GraphQL Schema initially ...');
123154

124-
const result = await this.apollo.simpleQuery(introspectionQuery, {}, true);
155+
// We send a custom header along with the request. This is required for our test suite to mock the schema request.
156+
const context = {
157+
headers: { 'X-GraphQL-Introspection-Query': 'true' }
158+
};
159+
160+
const result = await this.apollo.simpleQuery(introspectionQuery, {}, true, context);
125161
this.schema = result.data.__schema;
126162

127163
this.logger.log('GraphQL Schema successful fetched', this.schema);

src/graphql/apollo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ export default class Apollo {
6969
return Transformer.transformIncomingData(response.data as Data, model, mutation);
7070
}
7171

72-
public async simpleQuery (query: string, variables: Arguments, bypassCache: boolean = false): Promise<any> {
72+
public async simpleQuery (query: string, variables: Arguments, bypassCache: boolean = false, context?: Data): Promise<any> {
7373
const fetchPolicy: FetchPolicy = bypassCache ? 'network-only' : 'cache-first';
74-
return this.apolloClient.query({ query: gql(query), variables, fetchPolicy });
74+
return this.apolloClient.query({ query: gql(query), variables, fetchPolicy, context });
7575
}
7676

77-
public async simpleMutation (query: string, variables: Arguments): Promise<any> {
78-
return this.apolloClient.mutate({ mutation: gql(query), variables });
77+
public async simpleMutation (query: string, variables: Arguments, context?: Data): Promise<any> {
78+
return this.apolloClient.mutate({ mutation: gql(query), variables, context });
7979
}
8080
}

test/support/helpers.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Vuex from 'vuex';
44
import VuexORM, { Database, Model } from '@vuex-orm/core';
55
import fetchMock from 'fetch-mock';
66
import VuexORMGraphQLPlugin from "app";
7+
import {introspectionResult} from "./mock-data";
78

89
Vue.use(Vuex);
910

@@ -37,7 +38,15 @@ export function createStore (entities) {
3738

3839
export async function sendWithMockFetch(response, callback, dontExpectRequest = false) {
3940
fetchMock.config.overwriteRoutes = true;
40-
fetchMock.post('/graphql', response);
41+
42+
fetchMock.post('/graphql', (url, opts) => {
43+
if (opts.headers['X-GraphQL-Introspection-Query'] === 'true') {
44+
return introspectionResult;
45+
} else {
46+
return response;
47+
}
48+
});
49+
4150

4251
try {
4352
await callback();

0 commit comments

Comments
 (0)