gpt4 book ai didi

node.js - Apollo/Graphql、Postgres - 为什么此查询返回空值?

转载 作者:行者123 更新时间:2023-11-29 12:50:20 25 4
gpt4 key购买 nike

<分区>

enter image description here enter image description here我正在将 apollo-graphql 与 postgres 一起使用,现在我希望能够将我的后端数据提取到 apollo 客户端中。这是我第一次尝试 graphql,我做了以下事情:i.) 在 localhost:4000 上创建了 apollo-graphql 服务器,它也有 apollo graphql Playground 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。

我的文件列表如下:

服务器.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;

架构.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;

求助!提前致谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com