node.js - GraphQL Apollo Sequelize,id 字段未定义但仍调用 Sequelize?

标签 node.js graphql sequelize.js apollo

这是我的第一个真正的 Java 和 GraphQL 服务器项目。我从一个 php 项目中复制了一个用户表来学习,但无法让应用程序提供它。 id 字段被神奇地添加到 sql 查询中。所以我尝试添加一个名为 Abc 的新表,它原来有四个字段 aa、bb、cc 和 dd。我遇到了与用户表相同的问题,但是当我添加 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”才能编译文件。
索引.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;
  }
}

最佳答案

您应该使用 field 选项而不是 fieldName 来指示字段的字段名称。

id: {
      primaryKey: true,
      type: DataTypes.BIGINT.UNSIGNED,
      field: `userid`
    },
另外我想您不需要将 id 模型字段描述为表中的 userid 字段名称。只需将 userid 字段定义为主键,并且如果您希望 userid 由数据库自动生成,那么您应该指明 autoIncrement: true
super.init({
    userid: {
      primaryKey: true,
      autoIncrement: true,
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false
    },
    groupid: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      defaultValue: 0
    },
    ...

关于node.js - GraphQL Apollo Sequelize,id 字段未定义但仍调用 Sequelize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65513727/

相关文章:

node.js - bootstrap-datetimepicker 无法选择日期

github - 什么时候不应该使用 GitHub GraphQL API?

reactjs - 如何修复 `Warning: Text content did not match. Server: "一些数据“客户端: "Loading..."`

sequelize.js - Sequelize : Sum value of nested column

mysql - Sequelize - 从未创建连接表

javascript - JS 中迭代器的实现

node.js - 无服务器 : dynamodb delete where condition

javascript - Typescript nodejs - 正则表达式模式在变量内不起作用

symfony - 如何启用普通 id 查询而不是在 api_platform graphql 中使用路径?

node.js - 升级 Sequelize 包