graphql序列化数据加载器问题,对于不可为null的字段[重复项],无法返回null
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了graphql序列化数据加载器问题,对于不可为null的字段[重复项],无法返回null相关的知识,希望对你有一定的参考价值。
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
cblock_key: Int!
`;
我创建了一个数据加载器,但出现“错误”:
[
"message": "Cannot return null for non-nullable field ConfigProxy.ip_key.",
如果我需要字段ip_key: CblockIp!
如果我删除了“!”从ip_key: CblockIp
到ip_key为null :)
"data":
"config":
"config_name": "FF0RFQH0",
"configProxy": [
"proxy_key": 4351701,
"ip_key": null
,
"proxy_key": 4351702,
"ip_key": null
,
"proxy_key": 4351703,
"ip_key": null
,
"proxy_key": 4351700,
"ip_key": null
]
我的文件如下:
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(解析器):
import requiresAuth from '../permissions';
const resolvers =
Query:
// configs: (parent, args, models ) =>
// return models.Config.findAll();
// ,
configProxy: requiresAuth.createResolver(
(parent, proxy_key , models ) =>
return models.ConfigProxy.findOne(
where:
proxy_key
);
)
,
ConfigProxy:
config_key: (parent, args, models ) =>
return models.Config.findByPk(parent.config_key);
,
ip_key: (parent, args, proxyLoader ) =>
proxyLoader.load(parent.ip_key);
//return models.CblockIp.findByPk(parent.ip_key)
;
如果在我的解析器中,我将proxyLoader替换为models和此行
proxyLoader.load(parent.ip_key);
与
return models.CblockIp.findByPk(parent.ip_key)
一切正常,但没有批处理程序。我想我的配料器做错了什么。
Console.log显示,即使在批处理程序中也应该没问题,这就是为什么我不明白问题出在哪里的原因。这是来自:
的批处理功能的console.logconsole.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);
以上是关于graphql序列化数据加载器问题,对于不可为null的字段[重复项],无法返回null的主要内容,如果未能解决你的问题,请参考以下文章
Nestjs / GraphQL - Playground 为查询返回 Null 错误。我的解析器?
Apollo GraphQL“不能为不可为空的字段 Mutation.createUser 返回 null”
如何处理数据加载器/GraphQL 嵌套查询中的并发 DbContext 访问?