如何解决GraphQL Apollo Sequelize,id 字段未定义但 Squelize 仍被调用?
这是我的第一个真正的 Java 和 GraphQL 服务器项目。我从一个 PHP 项目中复制了一个用户表来学习,但无法让应用程序提供它。 id 字段被神奇地添加到 sql 查询中。所以我尝试添加一个名为 Abc 的新表,它原来有四个字段 aa、bb、cc 和 dd。我遇到了与用户表相同的问题,但是当我添加 id 字段时,服务器按预期工作。我不明白为什么添加 id 字段或如何添加。任何建议将不胜感激。
感谢阅读
Executing (default): SELECT `id`,`userid`,`groupid`,`clientid`,`firstname`,`lastname`,`username`,`password`,`email`,`registerdate`,`lastvisitdate`,`usersignature`,`blockaccess`,`lastip`,`hashomepage`,`homepage`,`template`,`css`,`defaultquery`,`logo`,`slogan`,`checksum`,`allowcontent`,`blockcontent`,`isEnabled` FROM `Users` AS `Users`;
我使用了 squelize-auto 来生成模型代码,必须从 userid 字段中删除“autoIncrement: true”才能编译文件。
index.js
const tdSchema = require('./GraphQL/Schema.js');
const tdUsers = require('./GraphQL/Users.js');
const db = require('./db.js');
const { gql } = require('apollo-server');
const tdAbc = gql`
type Abc {
id: Int!
aa: Int
bb: Int
cc: Int
dd: Int
}`;
const resolvers = {
Query: {
Abc: async (obj,args,context,info) => db.Abcs.findByPk(args.id),Abcs: async () => db.Abcs.findAll(),User: async (obj,info) => db.Users.findByPk(args.userid),Users: async () => db.Users.findAll(),},}
const express = require('express');
const bodyParser = require('body-parser')
const { ApolloServer } = require('apollo-server-express')
const cors = require('cors')
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors())
const server = new ApolloServer({
typeDefs: [
tdSchema,tdUsers,tdAbc,],resolvers: [resolvers],})
server.applyMiddleware({ app })
app.get('/',(req,res) =>
res.send('<B>Testing Version 0.0.1</b><br/><a href=\"http://10.11.11.8:4000/graphql\">http://10.11.11.8:4000/graphql</a>"'))
app.listen({ port: 4000 },() =>
console.log(`Server ready at http://10.11.11.8:4000/graphql`),)
我的 GraphQL 数据类型
const { gql } = require('apollo-server');
const typeDefs = gql`
type User {
userid: Int!
groupid: Int
clientid: Int
firstname: String
lastname: String
username: String
password: String
email: String
registerdate: String
lastvisitdate: String
usersignature: String
blockaccess: Int
lastip: String
hashomepage: Int
homepage: String
template: String
css: String
defaultquery: String
logo: String
slogan: String
checksum: String
allowcontent: String
blockcontent: String
isEnabled: Boolean
}
`;
module.exports = typeDefs;
我的用户表模型
const Sequelize = require('sequelize');
module.exports = (sequelize,DataTypes) => {
return Users.init(sequelize,DataTypes);
}
class Users extends Sequelize.Model {
static init(sequelize,DataTypes) {
super.init({
id: {
primaryKey: true,type: DataTypes.BIGINT.UNSIGNED,fieldName: `userid`
},userid: {
//autoIncrement: true,allowNull: false
},groupid: {
type: DataTypes.BIGINT.UNSIGNED,allowNull: false,defaultValue: 0
},clientid: {
type: DataTypes.BIGINT.UNSIGNED,firstname: {
type: DataTypes.STRING(255),allowNull: true
},lastname: {
type: DataTypes.STRING(255),username: {
type: DataTypes.STRING(255),allowNull: true,unique: "username"
},password: {
type: DataTypes.STRING(255),email: {
type: DataTypes.STRING(255),registerdate: {
type: DataTypes.DATE,lastvisitdate: {
type: DataTypes.DATE,usersignature: {
type: DataTypes.STRING(255),blockaccess: {
type: DataTypes.tinyint,lastip: {
type: DataTypes.CHAR(15),hashomepage: {
type: DataTypes.tinyint,homepage: {
type: DataTypes.STRING(255),unique: "homepage"
},template: {
type: DataTypes.TEXT,css: {
type: DataTypes.TEXT,defaultquery: {
type: DataTypes.STRING(255),logo: {
type: DataTypes.STRING(255),slogan: {
type: DataTypes.TEXT,checksum: {
type: DataTypes.STRING(255),allowcontent: {
type: DataTypes.STRING(255),blockcontent: {
type: DataTypes.STRING(255),isEnabled: {
type: DataTypes.BOOLEAN,defaultValue: false
}
},{
sequelize,tableName: 'Users',timestamps: false,indexes: [
{
name: "userid",unique: true,using: "BTREE",fields: [
{ name: "userid" },]
},{
name: "homepage",using: "HASH",fields: [
{ name: "homepage" },{
name: "username",fields: [
{ name: "username" },]
});
return Users;
}
}
解决方法
您应该使用 field
选项而不是 fieldName
来指示字段的字段名称。
id: {
primaryKey: true,type: DataTypes.BIGINT.UNSIGNED,field: `userid`
},
此外,我想您不需要将 id
模型字段描述为表中的 userid
字段名称。只需将 userid
字段定义为主键,并且如果您希望 userid
由数据库自动生成,那么您应该指出 autoIncrement: true
super.init({
userid: {
primaryKey: true,autoIncrement: true,allowNull: false
},groupid: {
type: DataTypes.BIGINT.UNSIGNED,allowNull: false,defaultValue: 0
},...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。