Skip to content

Commit 081a59e

Browse files
authored
GraphQL server for testing (#37)
* Better object detection * Mock server for tests * Build * Remove old stuff
1 parent 5a3dd21 commit 081a59e

27 files changed

+18469
-4236
lines changed

bla.js

Whitespace-only changes.

dist/vuex-orm-graphql.esm.js

Lines changed: 17370 additions & 236 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@
6161
},
6262
"devDependencies": {
6363
"@types/sinon": "^5.0.1",
64+
"apollo-link-schema": "^1.1.0",
65+
"graphql-tools": "^3.0.4",
6466
"babel-core": "^6.26.0",
6567
"babel-loader": "^7.1.2",
6668
"babel-plugin-transform-runtime": "^6.23.0",
6769
"babel-preset-env": "^1.7.0",
6870
"babel-preset-stage-2": "^6.24.1",
6971
"codecov.io": "^0.1.6",
7072
"expect": "^23.1.0",
71-
"fetch-mock": "^6.4.4",
7273
"fork-ts-checker-webpack-plugin": "^0.4.2",
7374
"istanbul-instrumenter-loader": "^3.0.0",
7475
"mocha": "^5.2.0",

src/actions/action.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@ export default class Action {
3030
const context: Context = Context.getInstance();
3131
const schema: Schema = await context.loadSchema();
3232

33-
const multiple: boolean = schema.returnsConnection(schema.getMutation(name));
33+
const multiple: boolean = Schema.returnsConnection(schema.getMutation(name)!);
3434
const query = QueryBuilder.buildQuery('mutation', model, name, variables, multiple);
3535

3636
// Send GraphQL Mutation
37-
const newData = await Context.getInstance().apollo.request(model, query, variables, true);
37+
let newData = await Context.getInstance().apollo.request(model, query, variables, true);
3838

3939
// When this was not a destroy action, we get new data, which we should insert in the store
4040
if (name !== NameGenerator.getNameForDestroy(model)) {
41-
const insertedData: Data = await Store.insertData(newData, dispatch);
41+
newData = newData[Object.keys(newData)[0]];
42+
43+
// IDs as String cause terrible issues, so we convert them to integers.
44+
newData.id = parseInt(newData.id, 10);
45+
46+
const insertedData: Data = await Store.insertData({ [model.pluralName]: newData }, dispatch);
4247

4348
// Try to find the record to return
4449
const records = insertedData[model.pluralName];

src/actions/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default class Query extends Action {
3131
filter = filter ? Transformer.transformOutgoingData(model, filter) : {};
3232

3333
// Multiple?
34-
const multiple: boolean = schema.returnsConnection(schema.getQuery(name));
34+
const multiple: boolean = Schema.returnsConnection(schema.getQuery(name)!);
3535

3636
// Build query
3737
const query = QueryBuilder.buildQuery('query', model, name, filter, multiple, false);

src/actions/simple-mutation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default class SimpleMutation extends Action {
1717
if (query) {
1818
variables = this.prepareArgs(variables);
1919
const result = await Context.getInstance().apollo.simpleMutation(query, variables);
20-
return result.data;
20+
21+
// remove the symbols
22+
return JSON.parse(JSON.stringify(result.data));
2123
} else {
2224
throw new Error("The simpleMutation action requires the 'query' to be set");
2325
}

src/actions/simple-query.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export default class SimpleQuery extends Action {
1818
if (query) {
1919
variables = this.prepareArgs(variables);
2020
const result = await Context.getInstance().apollo.simpleQuery(query, variables, bypassCache);
21-
return result.data;
21+
22+
// remove the symbols
23+
return JSON.parse(JSON.stringify(result.data));
2224
} else {
2325
throw new Error("The simpleQuery action requires the 'query' to be set");
2426
}

src/common/context.ts

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,82 @@ import Schema from '../graphql/schema';
1010
const inflection = require('inflection');
1111

1212
const introspectionQuery = `
13-
query {
13+
query Introspection {
1414
__schema {
15-
types {
15+
types {
1616
name
1717
description
1818
1919
fields(includeDeprecated: true) {
2020
name
2121
description
22+
args {
23+
name
24+
description
25+
type {
26+
name
27+
kind
28+
29+
ofType {
30+
kind
31+
32+
name
33+
ofType {
34+
kind
35+
name
36+
37+
ofType {
38+
kind
39+
name
40+
}
41+
}
42+
}
43+
}
44+
}
2245
2346
type {
2447
name
2548
kind
49+
50+
ofType {
51+
kind
52+
53+
name
54+
ofType {
55+
kind
56+
name
57+
58+
ofType {
59+
kind
60+
name
61+
}
62+
}
63+
}
2664
}
2765
}
2866
29-
3067
inputFields {
3168
name
3269
description
3370
3471
type {
3572
name
3673
kind
74+
75+
ofType {
76+
kind
77+
78+
name
79+
ofType {
80+
kind
81+
name
82+
83+
ofType {
84+
kind
85+
name
86+
}
87+
}
88+
}
3789
}
3890
}
3991
}
@@ -210,7 +262,6 @@ export default class Context {
210262
this.connectionQueryMode = this.schema!.determineQueryMode();
211263
this.logger.log(`Connection Query Mode is ${this.connectionQueryMode} by automatic detection`);
212264
} else {
213-
console.log('---> Config!');
214265
this.logger.log(`Connection Query Mode is ${this.connectionQueryMode} by config`);
215266
}
216267
}

src/common/logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { DocumentNode } from 'graphql';
22
import { Arguments } from '../support/interfaces';
33
import { FetchPolicy } from 'apollo-client';
4-
import { isObject, prettify } from '../support/utils';
4+
import { prettify } from '../support/utils';
5+
import * as _ from 'lodash';
56

67
/**
78
* Vuex-ORM-Apollo Debug Logger.
@@ -87,7 +88,7 @@ export default class Logger {
8788
if (this.enabled) {
8889
try {
8990
let prettified = '';
90-
if (isObject(query) && (query as DocumentNode).loc) {
91+
if (_.isObject(query) && (query as DocumentNode).loc) {
9192
prettified = prettify((query as DocumentNode).loc!.source.body);
9293
} else {
9394
prettified = prettify(query as string);

src/graphql/apollo.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApolloClient, FetchPolicy } from 'apollo-client';
22
import { InMemoryCache } from 'apollo-cache-inmemory';
33
import { HttpLink } from 'apollo-link-http';
4+
import { ApolloLink } from 'apollo-link';
45
import Context from '../common/context';
56
import { Arguments, Data } from '../support/interfaces';
67
import Transformer from './transformer';
@@ -15,7 +16,7 @@ export default class Apollo {
1516
* The http link instance to use.
1617
* @type {HttpLink}
1718
*/
18-
private readonly httpLink: HttpLink;
19+
private readonly httpLink: ApolloLink;
1920

2021
/**
2122
* The ApolloClient instance
@@ -29,12 +30,17 @@ export default class Apollo {
2930
public constructor () {
3031
const context = Context.getInstance();
3132

32-
this.httpLink = new HttpLink({
33-
uri: context.options.url ? context.options.url : '/graphql',
34-
credentials: context.options.credentials ? context.options.credentials : 'same-origin',
35-
headers: context.options.headers ? context.options.headers : {},
36-
useGETForQueries: Boolean(context.options.useGETForQueries)
37-
});
33+
// This allows the test suite to pass a custom link
34+
if (context.options.link) {
35+
this.httpLink = context.options.link;
36+
} else {
37+
this.httpLink = new HttpLink({
38+
uri: context.options.url ? context.options.url : '/graphql',
39+
credentials: context.options.credentials ? context.options.credentials : 'same-origin',
40+
headers: context.options.headers ? context.options.headers : {},
41+
useGETForQueries: Boolean(context.options.useGETForQueries)
42+
});
43+
}
3844

3945
this.apolloClient = new ApolloClient({
4046
link: this.httpLink,

0 commit comments

Comments
 (0)