当我运行以下查询时:
{
viewer {
id
firstName
lastName
}
}
我得到以下对象,显示错误的查询结果:
{
"data": {
"viewer": {
"id": "VXNlcjo=",
"firstName": null,
"lastName": null
}
}
}
我已经设置了以下database.js:
import Sequelize from 'sequelize';
import bcrypt from 'bcrypt';
// Create a Sequelize instance
var connection = new Sequelize('dev', 'mary', 'password', {
host: 'localhost',
dialect: 'postgres',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
// User model
var User = connection.define('user', {
// First name
firstName: {
type: Sequelize.STRING,
allowNull: false,
validate: {
is: {
args: /^[a-z]+$/i,
msg: 'First name is invalid'
}
}
},
// Last name
lastName: {
type: Sequelize.STRING,
allowNull: false,
validate: {
is: {
args: /^[a-z]+$/i,
msg: 'Last name is invalid'
}
}
},
// Username
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
validate: {
is: {
args: /^@?(\w){3,15}$/,
msg: 'Username can not exceed 15 characters or contain spaces.'
}
}
},
// Email
email: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
// Password
password: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
validate: {
// Validate Password
}
}
}, {
// Use plural names in database.
freezeTableName: false,
// Timestamps
timestamps: true,
// hooks
hooks: {
afterValidate: user => {
user.password = bcrypt.hashSync(user.password, 8);
}
}
});
// Group model
var Group = connection.define('group', {
// Owner Id
ownerId: {
type: Sequelize.INTEGER,
allowNull: false
},
// Group name
name: {
type: Sequelize.STRING,
allowNull: false
},
// Domain
domain: {
type: Sequelize.STRING,
allowNull: false
},
// Topic
topic: {
type: Sequelize.STRING,
validate: {
len: {
args: [0, 150],
msg: 'Topic can not exceed 150 characters.'
}
}
},
// Access
private: {
type: Sequelize.BOOLEAN
},
// Password
password: {
type: Sequelize.STRING,
defaultValue: null,
validate: {
// Validate Password
}
}
}, {
freezeTableName: false,
// Timestamps
timestamps: true,
// hooks
hooks: {
afterValidate: user => {
if (user.password) {
user.password = bcrypt.hashSync(user.password, 8);
}
}
}
});
// Relationships
User.belongsToMany(Group, { through: 'UserGroup' });
Group.belongsToMany(User, { through: 'UserGroup' });
// Insert some data by calling sync
connection.sync({
force: true
}).then ( () => {
// First user dummy
User.create( {
firstName: 'Sam',
lastName: 'Smith',
username: 'samsmith',
email: 'samsith@some.com',
password: 'somepassword'
})
.then(user => {
user.createGroup({
ownerId: user.id,
name: 'Engineering',
topic: 'A group',
domain: user.email.split('@')[1],
private: true,
password: ''
});
});
// Second user dummy
User.create( {
firstName: 'Sam',
lastName: 'Joshua',
username: 'samjoshua',
email: 'samjoshua@gmail.com',
password: 'somepassword'
})
.then(user => {
user.createGroup({
ownerId: user.id,
name: 'Design',
topic: 'This is some group',
domain: user.email.split('@')[1],
private: false,
password: ''
});
});
})
.catch(error => {
console.log(error);
});
export default connection;
以及以下schema.js文件:
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from 'graphql';
import {
connectionArgs,
connectionDefinitions,
connectionFromArray,
fromGlobalId,
globalIdField,
mutationWithClientMutationId,
nodeDefinitions,
} from 'graphql-relay';
import db from './database';
/**
* We get the node interface and field from the Relay library.
*
* The first method defines the way we resolve an ID to its object.
* The second defines the way we resolve an object to its GraphQL type.
*/
var {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
var {type, id} = fromGlobalId(globalId);
if (type === 'User') {
return db.models.user.getUser(id);
} else if (type === 'Group') {
return db.models.group.getGroup(id);
} else {
return null;
}
},
(obj) => {
if (obj instanceof User) {
return userType;
} else if (obj instanceof Group) {
return groupType;
} else {
return null;
}
}
);
/**
* We define our own types here.
*/
var userType = new GraphQLObjectType({
name: 'User',
description: 'A person who users our app',
fields: () => ({
id: globalIdField('User'),
firstName: {
type: GraphQLString,
description: 'A users first name',
resolve(user) {
return user.firstName;
}
},
lastName: {
type: GraphQLString,
resolve(user) {
return user.lastName;
}
},
username: {
type: GraphQLString,
resolve(user) {
return user.username;
}
},
email: {
type: GraphQLString,
resolve(user) {
return user.email;
}
},
groups: {
type: new GraphQLList(groupType),
resolve(user) {
return user.getGroups();
}
},
}),
interfaces: [nodeInterface],
});
// Group query
var groupType = new GraphQLObjectType({
name: 'Group',
description: 'A users group',
fields: () => ({
id: globalIdField('Group'),
ownerId: {
type: GraphQLInt,
resolve(group) {
return group.ownerId;
}
},
name: {
type: GraphQLString,
resolve(group) {
return group.name;
}
},
topic: {
type: GraphQLString,
resolve(group) {
return group.topic;
}
},
domain: {
type: GraphQLString,
resolve(group) {
return group.domain;
}
},
private: {
type: GraphQLBoolean,
resolve(group) {
return group.private;
}
},
users: {
type: new GraphQLList(userType),
resolve(group) {
return group.getUsers();
}
}
}),
interfaces: [nodeInterface],
});
/**
* This is the type that will be the root of our query,
* and the entry point into our schema.
*/
var queryType = new GraphQLObjectType({
name: 'Query',
description: 'This is the root query',
fields: () => ({
node: nodeField,
// Add our own root fields here
viewer: {
type: userType,
args: {
id: {
type: GraphQLInt
},
email: {
type: GraphQLString
}
},
resolve: (root, args) => {
return db.models.user.findAll({ where: args });
}
},
}),
});
/**
* This is the type that will be the root of our mutations,
* and the entry point into performing writes in our schema.
*/
var mutationType = new GraphQLObjectType({
name: 'Mutation',
description: 'Data insertion',
fields: () => ({
addUser: {
type: userType,
args: {
firstName: {
type: new GraphQLNonNull(GraphQLString),
},
lastName: {
type: new GraphQLNonNull(GraphQLString),
},
username: {
type: new GraphQLNonNull(GraphQLString),
},
email: {
type: new GraphQLNonNull(GraphQLString),
},
password: {
type: new GraphQLNonNull(GraphQLString),
}
},
resolve(_, args) {
return Db.models.user.create({
firstName: args.firstName,
lastName: args.lastName,
username: args.username,
email: args.email.toLowerCase(),
password: args.password
});
}
}
})
});
/**
* Finally, we construct our schema (whose starting query type is the query
* type we defined above) and export it.
*/
export var Schema = new GraphQLSchema({
query: queryType,
mutation: mutationType
});
我似乎无法找到我犯错误的地方。数据库文件看起来很好,因为我可以看到开发表中的数据,所以我猜我的错误是在schema.js文件中的某个地方。谢谢你的帮助。