Last active
December 24, 2015 14:57
-
-
Save z81/39db6f188087f6ad726f to your computer and use it in GitHub Desktop.
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
//* | |
import {resolver} from 'graphql-sequelize'; | |
import Sequelize from 'sequelize'; | |
import { | |
GraphQLInt, | |
GraphQLList, | |
GraphQLObjectType, | |
GraphQLNonNull, | |
GraphQLSchema, | |
GraphQLString, | |
} from 'graphql'; | |
import { | |
connectionArgs, | |
connectionDefinitions, | |
connectionFromArray, | |
fromGlobalId, | |
globalIdField, | |
mutationWithClientMutationId, | |
nodeDefinitions, | |
} from 'graphql-relay'; | |
//* | |
// sequelize config | |
let sequelize = new Sequelize('database', 'username', 'password', { | |
dialect: 'sqlite', | |
storage: __dirname + '/database.sqlite' | |
}) | |
// model user | |
let User = sequelize.define('user', { | |
name: Sequelize.STRING | |
}); | |
// model Task | |
let Task = sequelize.define('task', { | |
title: Sequelize.STRING | |
}); | |
User.Tasks = User.hasMany(Task, {as: 'tasks'}); | |
// sequelize.sync(); | |
// grapql | |
let taskType = new GraphQLObjectType({ | |
name: 'Task', | |
description: 'A task', | |
fields: { | |
id: { | |
type: new GraphQLNonNull(GraphQLInt), | |
description: 'The id of the task.', | |
}, | |
title: { | |
type: GraphQLString, | |
description: 'The title of the task.', | |
} | |
} | |
}); | |
let userType = new GraphQLObjectType({ | |
name: 'User', | |
description: 'A user', | |
fields: { | |
id: { | |
type: new GraphQLNonNull(GraphQLInt), | |
description: 'The id of the user.', | |
}, | |
name: { | |
type: GraphQLString, | |
description: 'The name of the user.', | |
}, | |
tasks: { | |
type: new GraphQLList(taskType), | |
resolve: resolver(User.Tasks, { | |
separate: true // load seperately, disables auto including - default: false | |
}) | |
} | |
} | |
}); | |
let mutation = new GraphQLObjectType({ | |
name: 'RootMutationType', | |
fields: { | |
updateUser: { | |
type: userType, | |
args: { | |
id: { | |
name: 'id', | |
type: new GraphQLNonNull(GraphQLString) | |
}, | |
name: { | |
name: 'name', | |
type: GraphQLString | |
} | |
}, | |
resolve: function (obj, {id, name}) { | |
User.update({name: name}, {where: { id: id }}); | |
return User.findById(id); | |
} | |
} | |
} | |
}); | |
var viewerType = new GraphQLObjectType({ | |
name: 'viewer', | |
fields: () => ({ | |
users: { | |
args: { | |
}, | |
type: new GraphQLList(userType), | |
resolve: resolver(User) | |
}, | |
}), | |
}); | |
var a = function () { | |
return {users: resolver(User)}; | |
} | |
export default new GraphQLSchema({ | |
query: new GraphQLObjectType({ | |
name: 'query', | |
fields: () => ({ | |
viewer: { | |
type: viewerType, | |
resolve: a, | |
}, | |
}), | |
}), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment