【问题标题】:Declaring multiple relayjs connections声明多个relayjs连接
【发布时间】:2017-05-31 13:43:16
【问题描述】:

我要两个 GraphQLObjectType

const AType = new GraphQLObjectType({
name: 'A'
fields: () => ({
    id: globalIdField('A', obj => obj._id),
    Id: {
        type: GraphQLID,
        resolve: obj => obj._id
    },
    email: { type: GraphQLString },
    name: { type: GraphQLString },
    status: { type: GraphQLString },
    description: { type: GraphQLString }
}),
interfaces: [nodeInterface]
});

和另一个Type BType,我在so t的类型中使用AType

const BType = new GraphQLObjectType({
name: 'BType',
fields: {
    id: globalIdField('BType'),
     ApplicantsDetails: {
        type: AType,
        resolve: obj => {
            return obj.applicantsId;
        }
    },
    respo_2: {
        type: GraphQLString,
        resolve: obj => "I have declared a ObjectType inside another"
    }
},
 interfaces: [nodeInterface]
});

以及我返回promise的主要类型,所以当我返回promise resolve(d)时,它应该转到BType和

const { connectionType: EmployerDashBoardConnectionType} =
connectionDefinitions({
    name: 'EmployerDash',
    nodeType: BType
});

上面是连接

        EmployerDashBoardConnection: {
        type: EmployerDashBoardConnectionType,
        description: 'Employer DashBoard Details',
        args: connectionArgsWithJobId,
        resolve: (_, args, auth) => {
                return new Promise((resolve, reject) => {
                    Dash(_, args, auth, function (err, d) {
                        if (err) {
                            reject(err);
                        } else {
                            resolve(d);
                        }
                    });
                });
        }
    }
    /* Introduce your new fields here */
}

Dash() 函数调用的响应是

 {
   applicantsId: 
   { _id: 5878548b51179817f48eb1f1,
   email: '123@gmail.com',
   password: '$2a$10$lDpfl7kL4i/8VPij8aypmeeiD1794g1afACUxca397LdlErMgWa.S',
 __v: 0,
 name: 'Alpaina',
 status: 'Unemployed',
 isJobSeeker: true,
 to: 2017-01-13T04:16:11.755Z }
 }

它只打印 null

【问题讨论】:

    标签: javascript node.js module relayjs graphql-js


    【解决方案1】:

    要使用边,您需要传递一个数组来解析函数

    resolve: (_, args, auth) => {
    return new Promise((resolve, reject) => {
        Dash(_, args, auth, function (err, d) {
            if (err) {
                reject(err);
            } else {
                  // if d is object then 
                  const a = [];
                  a.push(d);
                resolve(connectionFromArray(a, args));
            }
        });
      });
    }
    

    此代码仅用于解决您当前的问题

    注意:在使用中继连接时,您应该始终解析列表[ ]

    【讨论】:

      【解决方案2】:

      你的代码有两个问题:

      第一个问题:

      EmployerDashBoardConnectionType 的节点类型为BType。因此,EmployerDashBoardConnection 字段的 resolve() 函数中返回的项目应该具有相同的属性 - 在您的代码中并非如此。

      BType的结构是:

      {
          id
          ApplicantsDetails {
              id,
              Id,
              email,
              name,
              status,
              description,
          }
          respo_2,
      }
      

      而您正在传递以下对象,它完全不匹配。

      {
          applicantsId: {
              _id: 5878548b51179817f48eb1f1,
              email: '123@gmail.com',
              password: '$2a$10$lDpfl7kL4i/8VPij8aypmeeiD1794g1afACUxca397LdlErMgWa.S',
              __v: 0,
              name: 'Alpaina',
              status: 'Unemployed',
              isJobSeeker: true,
              to: 2017-01-13T04:16:11.755Z
          }
      }
      

      第二个问题:

      这就是为什么您的边数为空的原因。你的 resolve() 函数:

      resolve: (_, args, auth) => {
          return new Promise((resolve, reject) => {
              Dash(_, args, auth, function (err, d) {
                  if (err) {
                      reject(err);
                  } else {
                      resolve(d);
                  }
              });
          });
      }
      

      你什么都不返回。您可以使用来自graphql-relay npm 模块的connectionFromArray

      resolve: (_, args, auth) => {
          return new Promise((resolve, reject) => {
              Dash(_, args, auth, function (err, d) {
                  if (err) {
                      reject(err);
                  } else {
                      resolve(connectionFromArray(d, args));
                  }
              });
          });
      }
      

      d 的值必须是一个列表,其中每个项目都应具有顶级属性 idApplicantsDetailsrespo_2

      【讨论】:

      • 在 AType 中,它就像某种打字错误,现在我已经纠正了。,Dash() 正在返回一个带有数据的承诺对象,但当我使用 connectionFromArray 时,graphql 也给出了 null给出arraySlice.slice 不是函数
      • 我认为如果你在做连接,那么你的数据类型应该是一个数组
      • @terik_poe,我已经更新了我的答案。 Dash 函数的返回值d 必须是一个列表,其中每个项必须具有类似于BType 的结构。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多