Search code examples
node.jsgraphqlsequelize.jsapollo

GraphQL Apollo Sequelize, id field isnt defined but Squelize still called?


This is my first real java and GraphQL server project. I copied a user table from a php project to learn with and couldn't get the app to serve it out. The id field is being magically added to the sql query some how. So I tried adding a new table called Abc and it original had four fields aa, bb, cc and dd. I had the same issues as with the users table, but when I added the id field the server worked as expected. I don't understand why the id field added or how. Any advise would be greatly appreciated.

Thanks for reading

The SQL query that is called by Squelize

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`;

I used squelize-auto to generate the Model code, had to remove "autoIncrement: true," from the userid field to get the file compile.

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`),
)

My GraphQL DataTypes

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;

My Model for the Users Table

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;
  }
}


Solution

  • You should use field option instead of fieldName to indicate a field name for a field.

    id: {
          primaryKey: true,
          type: DataTypes.BIGINT.UNSIGNED,
          field: `userid`
        },
    

    Also I suppose you don't need to describe id model field as userid field name in a table. Just define userid field as a primary key and also if you wish userid to be autogenerated by a DB then you should indicate autoIncrement: true

    super.init({
        userid: {
          primaryKey: true,
          autoIncrement: true,
          type: DataTypes.BIGINT.UNSIGNED,
          allowNull: false
        },
        groupid: {
          type: DataTypes.BIGINT.UNSIGNED,
          allowNull: false,
          defaultValue: 0
        },
        ...