@raynode/graphql-connector-sequelize
Advanced tools
Comparing version 0.5.3 to 0.6.0
{ | ||
"name": "@raynode/graphql-connector-sequelize", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"description": "", | ||
@@ -39,5 +39,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@raynode/graphql-connector": "^0.5.3", | ||
"@raynode/graphql-connector": "^0.6.0", | ||
"@types/lodash": "^4.14.118", | ||
"@types/sequelize": "^4.27.30", | ||
"graphql": "^14.0.2", | ||
"lodash": "^4.17.11", | ||
@@ -48,3 +49,2 @@ "sequelize": "^4.41.1" | ||
"@types/jest": "^23.3.9", | ||
"graphql": "^14.0.2", | ||
"husky": "^1.1.3", | ||
@@ -64,3 +64,3 @@ "jest": "^23.6.0", | ||
}, | ||
"gitHead": "791e98515e6643838ba347d29da4cf977204ac0f" | ||
"gitHead": "dc8c66ec43a57b717c74af65fa91679094ac8701" | ||
} |
@@ -5,77 +5,10 @@ import * as Sequelize from 'sequelize' | ||
import { graphql, GraphQLSchema, printSchema } from 'graphql' | ||
import { configuration } from './index' | ||
import { createBaseSchemaGenerator, createSchema } from '@raynode/graphql-connector' | ||
const sequelize = new Sequelize({ | ||
dialect: 'sqlite', | ||
storage: 'memory', | ||
}) | ||
import { models, initialize, uuidv4 } from './tests/sample-models' | ||
export const User = sequelize.define('User', { | ||
id: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
unique: true, | ||
comment: 'Id of the user', | ||
defaultValue: Sequelize.fn('gen_random_uuid'), | ||
}, | ||
state: { | ||
type: Sequelize.ENUM('admin', 'member', 'guest'), | ||
defaultValue: 'guest', | ||
}, | ||
nickname: { type: Sequelize.STRING, allowNull: true }, | ||
name: { type: Sequelize.STRING, allowNull: false }, | ||
email: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
}) | ||
export const Loop = sequelize.define('Loop', { | ||
id: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
unique: true, | ||
comment: 'Id of the user', | ||
defaultValue: Sequelize.fn('gen_random_uuid'), | ||
}, | ||
}) | ||
Loop.hasOne(Loop, { | ||
as: 'next', | ||
}) | ||
const initialize = async () => { try { | ||
// tslint:disable:max-line-length | ||
await sequelize.query(`DROP TABLE IF EXISTS Users;`) | ||
await sequelize.query(` | ||
CREATE TABLE Users ( | ||
id STRING PRIMARY KEY, | ||
nickname TEXT, | ||
name TEXT NOT NULL, | ||
state TEXT NOT NULL, | ||
email TEXT NOT NULL, | ||
"createdAt" timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
"updatedAt" timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL | ||
); | ||
`) | ||
await sequelize.query(` | ||
INSERT INTO Users (id,state,nickname,name,email,createdAt,updatedAt) VALUES ('1','admin','Admin','Admin','admin@example.com','2018-11-09 16:35:47.055 +00:00','2018-11-09 16:35:47.055 +00:00'); | ||
`) | ||
const db = await sequelize.query(` | ||
SELECT * FROM Users | ||
`) | ||
console.log(db) | ||
// tslint:enable:max-line-length | ||
} catch(err) { console.error(err) } } | ||
const initialized = initialize() | ||
describe('model-mapper', () => { | ||
it('should find all attributes from a model', () => { | ||
const result = modelMapper('User', User) | ||
const result = modelMapper('User', models.User) | ||
expect(result.attributes).toHaveProperty('id') | ||
@@ -92,3 +25,3 @@ expect(result.attributes).toHaveProperty('state') | ||
it('should find the attributes of a Loop', () => { | ||
const result = modelMapper('Loop', Loop) | ||
const result = modelMapper('Loop', models.Loop) | ||
expect(result.attributes).toHaveProperty('id') | ||
@@ -100,3 +33,3 @@ expect(result.attributes).toHaveProperty('createdAt') | ||
it('should find the association of a Loop', () => { | ||
const result = modelMapper('Loop', Loop) | ||
const result = modelMapper('Loop', models.Loop) | ||
expect(result.associations).toHaveProperty('next') | ||
@@ -110,7 +43,7 @@ }) | ||
beforeAll(() => initialized) | ||
beforeAll(() => initialize()) | ||
beforeEach(async () => { | ||
const baseSchemaGenerator = createBaseSchemaGenerator({ typeMapper, modelMapper }) | ||
const baseSchema = baseSchemaGenerator({ Loop, User }) | ||
const baseSchemaGenerator = createBaseSchemaGenerator(configuration) | ||
const baseSchema = baseSchemaGenerator(models) | ||
schema = createSchema(baseSchema) | ||
@@ -125,13 +58,42 @@ }) | ||
const { data } = await runQuery(`{ | ||
Users { | ||
nodes { | ||
Users { nodes { | ||
id | ||
state | ||
nickname | ||
name | ||
createdAt | ||
updatedAt | ||
} } | ||
}`) | ||
expect(data.Users.nodes).toMatchSnapshot() | ||
}) | ||
it('should create a new user', async () => { | ||
const { data } = await runQuery(` | ||
mutation { | ||
newUser: createUser(data: { | ||
name: "Jack" | ||
nickname: "Jacky" | ||
email: "jack@example.com" | ||
}) { | ||
id | ||
state | ||
name | ||
nickname | ||
name | ||
createdAt | ||
updatedAt | ||
} | ||
} | ||
`) | ||
expect(data).toMatchSnapshot() | ||
}) | ||
it('should now find Georg as well', async () => { | ||
const { data } = await runQuery(`{ | ||
Users { nodes { | ||
id | ||
state | ||
nickname | ||
name | ||
} } | ||
}`) | ||
@@ -141,14 +103,70 @@ expect(data.Users.nodes).toMatchSnapshot() | ||
it('should create a new user', async () => { | ||
console.log('Expecting to run the create resolver') | ||
const { data, errors } = await runQuery(` | ||
mutation { | ||
newUser: createUser(data: { | ||
name: "George" | ||
}) | ||
it('should find only Georg', async () => { | ||
const { data } = await runQuery(`{ | ||
georg: User(where: { | ||
name: "Jack" | ||
}) { | ||
id | ||
state | ||
nickname | ||
name | ||
} | ||
`) | ||
console.log(errors) | ||
}`) | ||
expect(data.georg).toMatchSnapshot() | ||
}) | ||
it('should find the users in ascending order of id', async () => { | ||
const { data } = await runQuery(`{ | ||
Users(order: id_ASC) { nodes { id name } } | ||
}`) | ||
expect(data.Users.nodes[0].id).toEqual(uuidv4(1)) | ||
expect(data).toMatchSnapshot() | ||
}) | ||
it('should find the users in descending order of id', async () => { | ||
const { data } = await runQuery(`{ | ||
Users(order: id_DESC) { nodes { id name } } | ||
}`) | ||
expect(data.Users.nodes[0].id).toEqual(uuidv4(4)) | ||
expect(data).toMatchSnapshot() | ||
}) | ||
it('should find all posts and their authors', async () => { | ||
const { data } = await runQuery(`{ | ||
Posts { nodes { | ||
title | ||
User { | ||
name | ||
} | ||
}} | ||
}`) | ||
expect(data).toMatchSnapshot() | ||
}) | ||
it('should find everybody except the admin', async () => { | ||
const { data } = await runQuery(`{ | ||
Users(where: { NOT: { | ||
state: admin | ||
}}) { nodes { | ||
name | ||
state | ||
}} | ||
}`) | ||
expect(data).toMatchSnapshot() | ||
}) | ||
it('should find all users that have written a post', async () => { | ||
const { data } = await runQuery(`{ | ||
Users(where: { | ||
Posts_some: { NOT: { id: "" }} | ||
}) { nodes { | ||
name | ||
Posts { nodes { | ||
title | ||
}} | ||
}} | ||
}`) | ||
expect(data).toMatchSnapshot() | ||
}) | ||
}) |
@@ -7,3 +7,7 @@ export * from './type-mapper' | ||
import { PartialGeneratorConfiguration } from '@raynode/graphql-connector' | ||
import { filterMapper } from './filter-mapper' | ||
import { filterParser } from './filter-parser' | ||
import { modelMapper, Models } from './model-mapper' | ||
import { orderMapper } from './order-mapper' | ||
import { DataTypes } from './type-guards' | ||
@@ -13,4 +17,7 @@ import { typeMapper } from './type-mapper' | ||
export const configuration: PartialGeneratorConfiguration<DataTypes, Models> = { | ||
filterMapper, | ||
filterParser, | ||
modelMapper, | ||
orderMapper, | ||
typeMapper, | ||
} |
@@ -7,7 +7,14 @@ import { AnyModel, createModelMapper, GeneratedModelMapper } from '@raynode/graphql-connector' | ||
export interface SequelizeAttributeReference { | ||
model: string | ||
key: string | ||
} | ||
export interface SequelizeAttribute { | ||
_autoGenerated?: boolean | ||
type: DataTypes | ||
allowNull: boolean | ||
primaryKey: boolean | ||
primaryKey?: boolean | ||
comment: string | ||
references?: SequelizeAttributeReference | ||
} | ||
@@ -42,2 +49,4 @@ | ||
let d = false | ||
export const modelMapper = createModelMapper<DataTypes, Models>((model, addAttribute, addAssociation) => { | ||
@@ -47,2 +56,3 @@ const rawModel: SequelizeModel = model as any | ||
const attribute = rawModel.rawAttributes[name] | ||
// if(d) console.log(JSON.stringify(attribute, null, 2)) | ||
addAttribute({ | ||
@@ -53,2 +63,3 @@ name, | ||
resolver: instance => instance[name], | ||
// changable: !(attribute._autoGenerated || attribute.primaryKey || attribute.references), | ||
}) | ||
@@ -59,8 +70,19 @@ }) | ||
const association = rawModel.associations[name] | ||
const list = association.associationType === 'HasMany' || association.associationType === 'BelongsToMany' | ||
addAssociation({ | ||
name: association.as, | ||
model: association.target.name, | ||
list: association.associationType === 'HasMany' || association.associationType === 'BelongsToMany', | ||
list, | ||
nonNull: association.associationType === 'BelongsTo' || association.associationType === 'BelongsToMany', | ||
resolver: () => null, | ||
resolver: async (instance, args, context, info) => { | ||
// @TODO | ||
// this needs to correctly submit nodes and page when done! | ||
const res = await instance[`get${association.as}`]() | ||
return list | ||
? { | ||
nodes: res, | ||
page: null, | ||
} : res | ||
}, | ||
}) | ||
@@ -70,20 +92,9 @@ }) | ||
return { | ||
create: async (_, args) => { | ||
console.log('CREATE') | ||
console.log(args) | ||
return null | ||
}, | ||
create: async (_, { data }) => model.create(data), | ||
delete: async () => null, | ||
findMany: async (_, args) => { | ||
console.log('FIND MANY') | ||
const { | ||
page = { | ||
limit: 100, | ||
offset: 0, | ||
}, | ||
} = args | ||
const { count, rows: nodes } = await model.findAndCount() | ||
findMany: async (_, { order, where: { where = {}, include = [] } = {} }) => { | ||
const nodes = await model.findAll({ include, where, order }) | ||
return { | ||
nodes, | ||
page, | ||
page: { limit: 100, offset: 0 }, | ||
} | ||
@@ -90,0 +101,0 @@ }, |
@@ -84,3 +84,3 @@ import { Attribute } from '@raynode/graphql-connector' | ||
const Constructor = Constructors[name] | ||
const attr: Attribute<DataTypes> = { | ||
const attr: Attribute<any, DataTypes> = { | ||
fieldType: 'Attribute', | ||
@@ -90,2 +90,3 @@ list: false, | ||
nonNull: false, | ||
resolver: () => null, | ||
type: new Constructor(), | ||
@@ -100,3 +101,3 @@ } | ||
const Constructor = Constructors[name] | ||
const attr: Attribute<DataTypes> = { | ||
const attr: Attribute<any, DataTypes> = { | ||
fieldType: 'Attribute', | ||
@@ -106,2 +107,3 @@ list: false, | ||
nonNull: false, | ||
resolver: () => null, | ||
type: new Constructor(), | ||
@@ -116,3 +118,3 @@ } | ||
const Constructor = Constructors[name] | ||
const attr: Attribute<DataTypes> = { | ||
const attr: Attribute<any, DataTypes> = { | ||
fieldType: 'Attribute', | ||
@@ -122,2 +124,3 @@ list: false, | ||
nonNull: false, | ||
resolver: () => null, | ||
type: new Constructor(...args), | ||
@@ -124,0 +127,0 @@ } |
Sorry, the diff of this file is not supported yet
110114
14
52
1969
6
+ Addedgraphql@^14.0.2
+ Added@raynode/graphql-connector@0.6.1(transitive)
+ Added@types/node@22.13.4(transitive)
+ Addedgraphql@14.7.0(transitive)
+ Addediterall@1.3.0(transitive)
- Removed@raynode/graphql-connector@0.5.3(transitive)
- Removed@types/node@22.13.5(transitive)
- Removedgraphql@16.10.0(transitive)