【问题标题】:graphql sequlize dataloader issue, Cannot return null for non-nullable field [duplicate]graphql sequlize 数据加载器问题,无法为不可为空的字段返回 null [重复]
【发布时间】:2023-03-22 18:50:02
【问题描述】:

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

【问题讨论】:

    标签: graphql dataloader


    【解决方案1】:

    您没有在解析器中返回任何内容。

    return proxyLoader.load(parent.ip_key);
    

    【讨论】:

    • 谢谢,就是这样……但现在它没有返回 ip_address,知道为什么吗?同样的,它现在是空的。
    • 我怀疑这是您的类型与您返回的内容不匹配(即您的类型是 [Foo] 但您返回的是一个对象,或者您的类型是 Foo 但您返回的是大批)。很难说没有看到实际的架构。随意用相关的类型定义更新问题,我可以看看。在此期间,我将把这个问题作为一个骗子来结束。
    • 我已将 ip_address 的类型添加到顶部。感谢您抽出宝贵时间。
    • 抱歉,我的意思是ConfigProxy.ip_key 的类型是什么——[CblockIp]CblockIp
    • 似乎我已经通过添加 .then(posts => posts[0]) 使其工作。再次感谢您的帮助。
    猜你喜欢
    • 2021-11-27
    • 2021-10-24
    • 2021-10-29
    • 2018-04-10
    • 2021-03-18
    • 2022-01-02
    • 2020-11-30
    • 2019-10-04
    • 2019-10-25
    相关资源
    最近更新 更多