声明多个 relayjs 连接

Declaring multiple relayjs connections

我要两个 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]
});

和我 return 承诺的主要类型,所以当我 return 承诺 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: 'a$lDpfl7kL4i/8VPij8aypmeeiD1794g1afACUxca397LdlErMgWa.S',
 __v: 0,
 name: 'Alpaina',
 status: 'Unemployed',
 isJobSeeker: true,
 to: 2017-01-13T04:16:11.755Z }
 }

它只打印 null

你的代码有两个问题:

第一个问题:

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

BType的结构是:

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

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

{
    applicantsId: {
        _id: 5878548b51179817f48eb1f1,
        email: '123@gmail.com',
        password: 'a$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);
            }
        });
    });
}

你return什么都没有。您可以使用 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

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

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));
        }
    });
  });
}

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

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