【发布时间】:2021-04-07 08:33:36
【问题描述】:
这是我的第一个真正的 java 和 GraphQL 服务器项目。我从一个 php 项目中复制了一个用户表来学习,但无法让应用程序提供它。 id 字段以某种方式神奇地添加到 sql 查询中。所以我尝试添加一个名为 Abc 的新表,它原来有四个字段 aa、bb、cc 和 dd。我遇到了与 users 表相同的问题,但是当我添加 id 字段时,服务器按预期工作。我不明白为什么要添加 id 字段或如何添加。任何建议将不胜感激。
感谢阅读
Squelize 调用的 SQL 查询
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, args, context, 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,
type: DataTypes.BIGINT.UNSIGNED,
allowNull: false
},
groupid: {
type: DataTypes.BIGINT.UNSIGNED,
allowNull: false,
defaultValue: 0
},
clientid: {
type: DataTypes.BIGINT.UNSIGNED,
allowNull: false,
defaultValue: 0
},
firstname: {
type: DataTypes.STRING(255),
allowNull: true
},
lastname: {
type: DataTypes.STRING(255),
allowNull: true
},
username: {
type: DataTypes.STRING(255),
allowNull: true,
unique: "username"
},
password: {
type: DataTypes.STRING(255),
allowNull: true
},
email: {
type: DataTypes.STRING(255),
allowNull: true
},
registerdate: {
type: DataTypes.DATE,
allowNull: true
},
lastvisitdate: {
type: DataTypes.DATE,
allowNull: true
},
usersignature: {
type: DataTypes.STRING(255),
allowNull: true
},
blockaccess: {
type: DataTypes.TINYINT,
allowNull: false,
defaultValue: 0
},
lastip: {
type: DataTypes.CHAR(15),
allowNull: true
},
hashomepage: {
type: DataTypes.TINYINT,
allowNull: false,
defaultValue: 0
},
homepage: {
type: DataTypes.STRING(255),
allowNull: true,
unique: "homepage"
},
template: {
type: DataTypes.TEXT,
allowNull: true
},
css: {
type: DataTypes.TEXT,
allowNull: true
},
defaultquery: {
type: DataTypes.STRING(255),
allowNull: true
},
logo: {
type: DataTypes.STRING(255),
allowNull: true
},
slogan: {
type: DataTypes.TEXT,
allowNull: true
},
checksum: {
type: DataTypes.STRING(255),
allowNull: true
},
allowcontent: {
type: DataTypes.STRING(255),
allowNull: true
},
blockcontent: {
type: DataTypes.STRING(255),
allowNull: true
},
isEnabled: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
sequelize,
tableName: 'Users',
timestamps: false,
indexes: [
{
name: "userid",
unique: true,
using: "BTREE",
fields: [
{ name: "userid" },
]
},
{
name: "homepage",
unique: true,
using: "HASH",
fields: [
{ name: "homepage" },
]
},
{
name: "username",
unique: true,
using: "HASH",
fields: [
{ name: "username" },
]
},
]
});
return Users;
}
}
【问题讨论】:
标签: node.js graphql sequelize.js apollo