我正在为3种类型(例如,学生,同学和宠物)设置端点,其中学生有很多同学和很多宠物。 我找出了2个选项:
首先是创建一个FriendsInterface并让Classmate和Pet实现该接口,然后将它们全部作为ListGraphType返回
第二个是创建与以下类型的连接:ClassmateConnection和PetConnection
我描述的第一种和第二种方法有什么区别?
我尝试创建一个ListGraphType,因为我可以在示例中看到这一点。
但是,没有关于如何创建连接的示例,我一直遇到以下错误:
Error message:
Error: Request failed with status code 400
at e.exports (http://localhost:3000/bundle.js:12:13653)
at e.exports (http://localhost:3000/bundle.js:44:1280)
at XMLHttpRequest.p.onreadystatechange (http://localhost:3000/bundle.js:12:12667)
第一种方法
public StudentType (MyData data) {
Name = "Student";
Description = "A curios creature in a giant universe.";
Field (d => d.Id).Description ("The id of the student.");
Field<ListGraphType<FriendInterface>> (
"friends",
resolve : context => data.GetFriends (context.Source)
);
}
第二种方法(创建连接)
public StudentType (MyData data) {
Name = "Student";
Description = "A curios creature in a giant universe.";
Field (d => d.Id).Description ("The id of the student.");
Connection<ListGraphType<ClassmateType>> ()
.Name ("classmates")
.Description ("classmate test")
.Resolve (context => data.GetClassmates (context.Source));
Connection<ListGraphType<PetType>> ()
.Name ("classmates")
.Description ("pet test")
.Resolve (context => data.GetPets (context.Source));
}
public ClassmateType (MyData data) {
Name = "Classmate";
Description = "A misunderstood creature in a giant universe.";
Field (d => d.Id).Description ("The id of the classmate.");
}
public PetType (MyData data) {
Name = "Pet";
Description = "An animal creature in a giant universe.";
Field (d => d.Id).Description ("The id of the pet.");
}
在第一种方法中,这是我要发送的查询:
student(studentID: "1") {
id
name
friends {
id
}
}
在第二种方法中,查询如下:
student(studentID: "1") {
id
name
classmateConnection {
classmates {
id
}
}
petConnection {
pets {
id
}
}
}