graphql Sequalize dataloader问题,对于不可为null的字段,不能返回null
ip地址的架构:graphql Sequalize dataloader问题,对于不可为null的字段,不能返回null,graphql,dataloader,Graphql,Dataloader,ip地址的架构: import { gql } from 'apollo-server-express'; module.exports = gql` extend type Query { # allTxn: [SubscrTxn] cblockIp(ip_key: Int!): CblockIp } type CblockIp { ip_key: Int! ip_address: String
import { gql } from 'apollo-server-express';
module.exports = gql`
extend type Query {
# allTxn: [SubscrTxn]
cblockIp(ip_key: Int!): CblockIp
}
type CblockIp {
ip_key: Int!
ip_address: String
cblock_key: Int!
}
`;
我创建了一个数据加载器,但我得到了“错误”:
如果我需要字段ip\u键:CblockIp代码>
如果我从ip\u密钥:CblockIp中删除“!”则ip\u密钥为空:)
我的文件如下所示:
app.js
它的一部分:
import DataLoader from 'dataloader';
import { proxyBatcher } from './batchFunctions';
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({
models,
secret: process.env.JWT_SECRET,
member: getLoggedInUser(req),
me: getLoggedInUser(req),
proxyLoader: new DataLoader((keys) => proxyBatcher(keys, models))
})
});
batchFunctions.js
:
import _ from 'lodash';
import { Op } from 'sequelize';
export const proxyBatcher = async (keys, { CblockIp }) => {
const proxies = await CblockIp.findAll({
raw: true,
where: {
ip_key: {
[Op.in]: keys
}
}
});
const gp = _.groupBy(proxies, 'ip_key');
console.log(proxies);
console.log(gp);
console.log(keys.map((k) => gp[k] || []));
return keys.map((k) => gp[k] || []);
};
export const dummy = 5;
configProxy.js(解析器):
如果在我的解析器中,我用{models}替换{proxyLoader},这一行
proxyLoader.load(parent.ip_key);
与
一切正常,但没有批处理程序。我想在我的配料器里我做错了什么
log显示,即使在batcher中,一切都应该正常,这就是为什么我不明白问题出在哪里。以下是来自batcher函数的console.log:
console.log(proxies);
console.log(gp);
console.log(keys.map((k) => gp[k] || []));
Executing (default): SELECT `config_key`, `config_type`, `config_name`, `filename`, `member_key`, `proxy_port` FROM `config` AS `config` WHERE `config`.`config_key` = 2314;
Executing (default): SELECT `proxy_key`, `config_key`, `ip_key` FROM `config_proxy` AS `config_proxy` WHERE `config_proxy`.`config_key` = 2314;
Executing (default): SELECT `ip_key`, `ip_address`, `cblock_key` FROM `cblock_ip` AS `cblock_ip` WHERE `cblock_ip`.`ip_key` IN (116312, 185667, 185969, 99424);
[ { ip_key: 99424, ip_address: '172.246.69.152', cblock_key: 576 },
{ ip_key: 116312, ip_address: '45.59.24.113', cblock_key: 645 },
{ ip_key: 185667,
ip_address: '184.174.74.121',
cblock_key: 1051 },
{ ip_key: 185969,
ip_address: '184.174.75.170',
cblock_key: 1052 } ]
{ '99424':
[ { ip_key: 99424, ip_address: '172.246.69.152', cblock_key: 576 } ],
'116312':
[ { ip_key: 116312, ip_address: '45.59.24.113', cblock_key: 645 } ],
'185667':
[ { ip_key: 185667,
ip_address: '184.174.74.121',
cblock_key: 1051 } ],
'185969':
[ { ip_key: 185969,
ip_address: '184.174.75.170',
cblock_key: 1052 } ] }
[ [ { ip_key: 116312, ip_address: '45.59.24.113', cblock_key: 645 } ],
[ { ip_key: 185667,
ip_address: '184.174.74.121',
cblock_key: 1051 } ],
[ { ip_key: 185969,
ip_address: '184.174.75.170',
cblock_key: 1052 } ],
[ { ip_key: 99424, ip_address: '172.246.69.152', cblock_key: 576 } ] ]
graphql查询如下所示
query Proxies($config_key: Int!) {
config(config_key: $config_key) {
config_name
configProxy {
proxy_key
ip_key {
ip_address
}
}
}
}
你不能在你的解析器里返回任何东西
return proxyLoader.load(parent.ip_key);
谢谢,就这样。。。但是现在它没有返回ip地址,知道为什么吗?同样,它现在是空的。我怀疑这是您的类型和返回的内容不匹配(即,您的类型是[Foo]
但您返回的是一个对象,或者您的类型是Foo
但您返回的是一个数组)。很难说没有看到实际的模式。请随意使用相关的类型定义更新问题,我可以看一下。在此期间,我将以重复的方式结束此问题。我已将ip_地址的类型添加到顶部。感谢您抽出时间。对不起,我是说ConfigProxy.ip_key
-[CblockIp]
或CblockIp
?似乎我通过添加来实现。然后(posts=>posts[0])。再次感谢你的帮助。
return models.CblockIp.findByPk(parent.ip_key)
console.log(proxies);
console.log(gp);
console.log(keys.map((k) => gp[k] || []));
Executing (default): SELECT `config_key`, `config_type`, `config_name`, `filename`, `member_key`, `proxy_port` FROM `config` AS `config` WHERE `config`.`config_key` = 2314;
Executing (default): SELECT `proxy_key`, `config_key`, `ip_key` FROM `config_proxy` AS `config_proxy` WHERE `config_proxy`.`config_key` = 2314;
Executing (default): SELECT `ip_key`, `ip_address`, `cblock_key` FROM `cblock_ip` AS `cblock_ip` WHERE `cblock_ip`.`ip_key` IN (116312, 185667, 185969, 99424);
[ { ip_key: 99424, ip_address: '172.246.69.152', cblock_key: 576 },
{ ip_key: 116312, ip_address: '45.59.24.113', cblock_key: 645 },
{ ip_key: 185667,
ip_address: '184.174.74.121',
cblock_key: 1051 },
{ ip_key: 185969,
ip_address: '184.174.75.170',
cblock_key: 1052 } ]
{ '99424':
[ { ip_key: 99424, ip_address: '172.246.69.152', cblock_key: 576 } ],
'116312':
[ { ip_key: 116312, ip_address: '45.59.24.113', cblock_key: 645 } ],
'185667':
[ { ip_key: 185667,
ip_address: '184.174.74.121',
cblock_key: 1051 } ],
'185969':
[ { ip_key: 185969,
ip_address: '184.174.75.170',
cblock_key: 1052 } ] }
[ [ { ip_key: 116312, ip_address: '45.59.24.113', cblock_key: 645 } ],
[ { ip_key: 185667,
ip_address: '184.174.74.121',
cblock_key: 1051 } ],
[ { ip_key: 185969,
ip_address: '184.174.75.170',
cblock_key: 1052 } ],
[ { ip_key: 99424, ip_address: '172.246.69.152', cblock_key: 576 } ] ]
query Proxies($config_key: Int!) {
config(config_key: $config_key) {
config_name
configProxy {
proxy_key
ip_key {
ip_address
}
}
}
}
return proxyLoader.load(parent.ip_key);