-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
357 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import Sequelize from 'sequelize'; | ||
import { TEXT, INTEGER, REAL, NUMERIC, BLOB } from 'sequelize'; | ||
|
||
import { | ||
getPkFieldKey, | ||
makeCreateArgs, | ||
makeUpdateArgs, | ||
makeDeleteArgs, | ||
makePolyArgs, | ||
} from '../builders/arguments'; | ||
import { GraphQLString, GraphQLNonNull, GraphQLInt } from 'graphql'; | ||
|
||
const db = new Sequelize({ | ||
dialect: 'sqlite', | ||
storage: ':memory:', | ||
logging: false, | ||
operatorsAliases: Sequelize.Op, | ||
}); | ||
|
||
const model = db.define( | ||
'posts', | ||
{ | ||
id: { | ||
type: INTEGER, | ||
primaryKey: true, | ||
}, | ||
title: { | ||
type: TEXT, | ||
allowNull: true, | ||
}, | ||
userId: { | ||
type: INTEGER, | ||
allowNull: false, | ||
}, | ||
}, | ||
{ timestamps: false } | ||
); | ||
|
||
const model2 = db.define( | ||
'categories', | ||
{ | ||
id: { | ||
type: INTEGER, | ||
primaryKey: true, | ||
}, | ||
title: { | ||
type: TEXT, | ||
allowNull: true, | ||
}, | ||
userId: { | ||
type: INTEGER, | ||
allowNull: false, | ||
}, | ||
}, | ||
{ timestamps: false } | ||
); | ||
|
||
describe('getPkField', () => { | ||
it('detects the primary key key', () => { | ||
const pk = getPkFieldKey(model); | ||
expect(pk).toBe('id'); | ||
}); | ||
|
||
it('makeCreateArgs', () => { | ||
const args = makeCreateArgs(model); | ||
expect(args).toEqual({ | ||
title: { type: GraphQLString }, | ||
userId: { type: new GraphQLNonNull(GraphQLInt) }, | ||
}); | ||
}); | ||
|
||
it('makeUpdateArgs', () => { | ||
const args = makeUpdateArgs(model); | ||
expect(args).toEqual({ | ||
id: { type: GraphQLInt }, | ||
title: { type: GraphQLString }, | ||
userId: { type: GraphQLInt }, | ||
}); | ||
}); | ||
|
||
it('makeDeleteArgs', () => { | ||
const args = makeDeleteArgs(model); | ||
expect(args).toEqual({ | ||
id: { type: new GraphQLNonNull(GraphQLInt) }, | ||
}); | ||
}); | ||
|
||
it('makePolyArgs', () => { | ||
const args = makePolyArgs(model, model2); | ||
expect(args).toEqual({ | ||
id: { type: new GraphQLNonNull(GraphQLInt) }, | ||
categoryId: { type: new GraphQLNonNull(GraphQLInt) }, | ||
}); | ||
}); | ||
|
||
it('makePolyArgs with non-equal keys', () => { | ||
const posts = db.define( | ||
'posts', | ||
{ | ||
postId: { | ||
type: INTEGER, | ||
primaryKey: true, | ||
}, | ||
}, | ||
{ timestamps: false } | ||
); | ||
|
||
const categories = db.define( | ||
'categories', | ||
{ | ||
categoryId: { | ||
type: INTEGER, | ||
primaryKey: true, | ||
}, | ||
}, | ||
{ timestamps: false } | ||
); | ||
|
||
const args = makePolyArgs(posts, categories); | ||
expect(args).toEqual({ | ||
postId: { type: new GraphQLNonNull(GraphQLInt) }, | ||
categoryId: { type: new GraphQLNonNull(GraphQLInt) }, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { attributeFields } from 'graphql-sequelize'; | ||
import { singular } from 'pluralize'; | ||
import { GraphQLBoolean, GraphQLNonNull } from 'graphql'; | ||
import camelcase from 'camelcase'; | ||
|
||
export const getPkFieldKey = model => { | ||
return Object.keys(model.attributes).find(key => { | ||
const attr = model.attributes[key]; | ||
return attr.primaryKey; | ||
}); | ||
}; | ||
|
||
export const makeCreateArgs = model => { | ||
const fields = attributeFields(model); | ||
const pk = getPkFieldKey(model); | ||
|
||
delete fields[pk]; | ||
|
||
return fields; | ||
}; | ||
|
||
export const makeUpdateArgs = model => { | ||
const fields = attributeFields(model); | ||
|
||
return Object.keys(fields).reduce((acc, key) => { | ||
const field = fields[key]; | ||
|
||
if (field.type instanceof GraphQLNonNull) { | ||
field.type = field.type.ofType; | ||
} | ||
|
||
acc[key] = field; | ||
return acc; | ||
}, fields); | ||
}; | ||
|
||
export const makeDeleteArgs = model => { | ||
const fields = attributeFields(model); | ||
const pk = getPkFieldKey(model); | ||
|
||
return { [pk]: fields[pk] }; | ||
}; | ||
|
||
export const getPolyKeys = (model, otherModel) => { | ||
const key = getPkFieldKey(model); | ||
const otherKey = getPkFieldKey(otherModel); | ||
|
||
if (otherKey === key) { | ||
return [ | ||
key, | ||
otherKey, | ||
camelcase(`${singular(otherModel.name)}_${otherKey}`), | ||
]; | ||
} | ||
|
||
return [key, otherKey, otherKey]; | ||
}; | ||
|
||
export const makePolyArgs = (model, otherModel) => { | ||
const [key, otherKey, otherKeyFormatted] = getPolyKeys(model, otherModel); | ||
const fields = attributeFields(model); | ||
const otherFields = attributeFields(otherModel); | ||
|
||
return { | ||
[key]: fields[key], | ||
[otherKeyFormatted]: otherFields[otherKey], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.