Apollo/Graphql,Postgres - 为什么此查询返回空值?
Apollo/Graphql, Postgres - Why is this query returning null?
我正在将 apollo-graphql 与 postgres 一起使用,现在我希望能够将我的后端数据提取到 apollo 客户端中。这是我第一次尝试 graphql,我做了以下事情:
i.) 在 localhost:4000 上创建了 apollo-graphql 服务器,它也有 apollo graphql 游乐场
ii.) 为我的服务器定义 typeDef 和解析器
iii.) 在 typeDefs -> 中定义了我的模式
iv.) 在解析器中 -> 刚刚添加了一个 findAll 查询(尝试使用两个属性但没有参数):
Query: {
me: () => account.findAll({attributes: ['user_id', 'username', 'email']})
}
v.) 然后我将我使用 sequelize ORM 定义的 postgres dbIndex 添加到服务器文件中(我在上面的步骤 iv 中使用它来查询我的数据库)
vi.) 在我的 dbIndex 文件中,我使用环境变量对数据库进行身份验证,获取连接消息,创建数据库模式并将其导出。
完成这 6 个步骤后,在 apollo playground 中,我看到了 null。
我的文件列表如下:
Server.js:
const {ApolloServer} = require('apollo-server');
const typeDefs = require('./schema');
const {account} = require('../database/dbIndex.js');
const resolvers = {
Query: {
me: () => account.findAll()
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({url}) => {
console.log(`Server ready at ${url}`);
});
dbIndex.js
const Sequelize = require('sequelize');
require('dotenv').config();
const sortDb = new Sequelize(
`${process.env.DATABASE}`,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
{
dialect: 'postgres',
},
);
sortDb
.authenticate()
.then(() => {
console.log('Connected to DB');
})
.catch((err) => {
console.error('Unable to connect to DB', err);
});
const account = sortDb.define('account', {
user_id: {type: Sequelize.INTEGER},
username: {type: Sequelize.STRING},
email: {type: Sequelize.STRING}
});
module.exports.account = account;
schema.js
const {gql} = require('apollo-server');
const typeDef = gql
`
type Query {
"These are the queries we define in our query type"
me(user_id: ID): User
}
"How to define the structure of user? Below is an object type that does this:"
type User {
user_id: ID,
username: String,
email: String
}
`;
module.exports = typeDef;
请帮忙!提前致谢!
findAll in sequelize return 行数组,尝试使用特定查询的 findOne 或 findById
它return null 因为类型 User 的属性可以在数组中解析。
Query: {
me: () => account.findAll({attributes: ['user_id', 'username', 'email']})
}
v.) 然后我将我使用 sequelize ORM 定义的 postgres dbIndex 添加到服务器文件中(我在上面的步骤 iv 中使用它来查询我的数据库)
vi.) 在我的 dbIndex 文件中,我使用环境变量对数据库进行身份验证,获取连接消息,创建数据库模式并将其导出。
完成这 6 个步骤后,在 apollo playground 中,我看到了 null。
我的文件列表如下:
Server.js:
const {ApolloServer} = require('apollo-server');
const typeDefs = require('./schema');
const {account} = require('../database/dbIndex.js');
const resolvers = {
Query: {
me: () => account.findAll()
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({url}) => {
console.log(`Server ready at ${url}`);
});
dbIndex.js
const Sequelize = require('sequelize');
require('dotenv').config();
const sortDb = new Sequelize(
`${process.env.DATABASE}`,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
{
dialect: 'postgres',
},
);
sortDb
.authenticate()
.then(() => {
console.log('Connected to DB');
})
.catch((err) => {
console.error('Unable to connect to DB', err);
});
const account = sortDb.define('account', {
user_id: {type: Sequelize.INTEGER},
username: {type: Sequelize.STRING},
email: {type: Sequelize.STRING}
});
module.exports.account = account;
schema.js
const {gql} = require('apollo-server');
const typeDef = gql
`
type Query {
"These are the queries we define in our query type"
me(user_id: ID): User
}
"How to define the structure of user? Below is an object type that does this:"
type User {
user_id: ID,
username: String,
email: String
}
`;
module.exports = typeDef;
请帮忙!提前致谢!
findAll in sequelize return 行数组,尝试使用特定查询的 findOne 或 findById
它return null 因为类型 User 的属性可以在数组中解析。