Skip to content

Commit 4826e07

Browse files
LoicMahieumickhansen
authored andcommitted
mysql support (#255)
* travis: node@6 and cache `node_modules` * test: rewrite helper * test: run tests on mysql * test: mysql: prepare tests * mysql: fix limit: should be a integer * test: relay: test for all dialect * relay: full_count for mysql * test: mysql: fix order by * test: relay: fix random failing
1 parent 39ada4a commit 4826e07

19 files changed

+982
-875
lines changed

.travis.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
language: node_js
1+
sudo: false
22

3+
language: node_js
34
node_js:
45
- "4.0"
6+
- "6.0"
7+
8+
cache:
9+
directories:
10+
- node_modules
11+
12+
env:
13+
- DIALECT=mysql
14+
- DIALECT=postgres
515

16+
services:
17+
- mysql
618
addons:
719
postgresql: "9.4"
820

921
before_script:
10-
- psql -c 'create database graphql_sequelize_test;' -U postgres
22+
- if [[ "$DIALECT" == "postgres" ]]; then psql -c "drop database if exists test;" -U postgres; fi
23+
- if [[ "$DIALECT" == "postgres" ]]; then psql -c "create database test;" -U postgres; fi
24+
- if [[ "$DIALECT" == "mysql" ]]; then mysql -e "create database IF NOT EXISTS test;" -uroot; fi
1125

1226
script:
1327
- "npm run check && npm run test-integration"

docker-compose.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
dev:
22
build: .
33
links:
4-
- db
4+
- postgres
5+
- mysql
56
volumes:
67
- .:/src/graphql-sequelize
78
environment:
8-
DB_DATABASE: graphql_sequelize_test
9-
DB_USER: graphql_sequelize_test
10-
DB_PASSWORD: graphql_sequelize_test
9+
DIALECT: "${DIALECT}"
1110

12-
db:
11+
postgres:
1312
image: postgres:9.4
1413
environment:
1514
POSTGRES_USER: graphql_sequelize_test
16-
POSTGRES_PASSWORD: graphql_sequelize_test
15+
POSTGRES_PASSWORD: graphql_sequelize_test
16+
17+
mysql:
18+
image: mysql:5.6
19+
environment:
20+
MYSQL_USER: test
21+
MYSQL_PASSWORD: test
22+
MYSQL_DATABASE: test
23+
MYSQL_ALLOW_EMPTY_PASSWORD: 1

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test-watch": "npm run test-unit -- --watch",
1616
"test-unit": "mocha $npm_package_options_mocha test/unit/*.test.js test/unit/**/*.test.js",
1717
"build-docker": "docker-compose build",
18-
"test-docker": "docker-compose run dev /bin/sh -c \"npm run test-integration\"",
18+
"test-docker": "DIALECT=${DIALECT:=postgres} docker-compose run dev /bin/sh -c \"npm run test-integration\"",
1919
"test-integration": "mocha $npm_package_options_mocha test/integration/*.test.js test/integration/**/*.test.js"
2020
},
2121
"repository": {
@@ -51,8 +51,10 @@
5151
"chai": "^3.0.0",
5252
"chai-as-promised": "^5.1.0",
5353
"eslint": "^1.7.3",
54+
"graphql": "^0.6.0",
5455
"istanbul": "^0.4.0",
5556
"mocha": "^2.2.5",
57+
"mysql": "^2.11.1",
5658
"pg": "^5.0.0",
5759
"pg-hstore": "^2.3.2",
5860
"sequelize": "^3.16.0",

src/argsToFindOptions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export default function argsToFindOptions(args, target) {
1212
}
1313

1414
if (key === 'limit' && args[key]) {
15-
result.limit = args[key];
15+
result.limit = parseInt(args[key], 10);
1616
}
1717

1818
if (key === 'offset' && args[key]) {
19-
result.offset = args[key];
19+
result.offset = parseInt(args[key], 10);
2020
}
2121

2222
if (key === 'order' && args[key]) {

src/generateIncludes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export default function generateIncludes(simpleAST, type, context, options) {
8686

8787
let separate = includeOptions.limit && association.associationType === 'HasMany';
8888

89+
if (includeOptions.limit) {
90+
includeOptions.limit = parseInt(includeOptions.limit, 10);
91+
}
92+
8993
if (include && (!includeOptions.limit || separate)) {
9094
if (includeOptions.order && !separate) {
9195
includeOptions.order.map(function (order) {

src/relay.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnu
247247
options.attributes = _.uniq(options.attributes);
248248
return before(options, args, context, info);
249249
},
250-
after: function (values, args, context, {source}) {
250+
after: async function (values, args, context, {source}) {
251251
var cursor = null;
252252

253253
if (args.after || args.before) {
@@ -265,9 +265,14 @@ export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnu
265265
if (!values[0]) {
266266
fullCount = 0;
267267
}
268-
if (model.sequelize.dialect.name === 'postgres' && (args.first || args.last)) {
269-
if (fullCount === null || fullCount === undefined) throw new Error('No fullcount available');
268+
269+
if ((args.first || args.last) && (fullCount === null || fullCount === undefined)) {
270+
// In case of `OVER()` is not available, we need to get the full count from a second query.
271+
fullCount = await model.count({
272+
where: argsToWhere(args)
273+
});
270274
}
275+
271276
let hasNextPage = false;
272277
let hasPreviousPage = false;
273278
if (args.first || args.last) {

test/helper.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/integration/helper.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/integration/relay.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

3+
import { sequelize, Promise, beforeRemoveAllTables } from '../support/helper'
4+
35
var chai = require('chai')
46
, expect = chai.expect
57
, resolver = require('../../src/resolver')
6-
, helper = require('./helper')
7-
, sequelize = helper.sequelize
88
, Sequelize = require('sequelize')
9-
, Promise = helper.Promise
109
, sinon = require('sinon')
1110
, attributeFields = require('../../src/attributeFields')
1211
, _ = require('lodash');
@@ -52,6 +51,8 @@ const generateCustom = Promise.method(id => {
5251
});
5352

5453
describe('relay', function () {
54+
beforeRemoveAllTables();
55+
5556
var User
5657
, Task
5758
, userType
@@ -264,7 +265,7 @@ describe('relay', function () {
264265
, projectId = 1
265266
, taskId = 1;
266267

267-
return this.sequelize.sync({force: true}).bind(this).then(function () {
268+
return sequelize.sync({force: true}).bind(this).then(function () {
268269
return Promise.join(
269270
Project.create({
270271
id: projectId++,

0 commit comments

Comments
 (0)