能够扩展现有类型非常棒,因为它允许代码模块化和权限分离。我找到了关于如何在查询中扩展对象输出的好例子(见下文),但没有很好的方法来扩展给定对象的输入。
为了这个例子,假设我们有一个 class User
。
class User {
String firstName;
String lastName;
}
如果我们声明一个 bean,我们可以有这样的查询:
/**
* This is valid and can be invoked using
* query {
* user(id=1) {
* firstName
* lastName
* }
* }
*/
@GraphQLQuery(name = "user")
public User getUser(@GraphQLArgument(name = "id") long id) {
}
然后在另一个 bean bean 中我们可以扩展 User
/**
* <<this currently works>>
* So now this query becomes valid
* query {
* user(id=1) {
* firstName
* lastName
* address { <-- this is not a top level, but extends User
* streetNam
* }
* }
* }
*/
@GraphQLQuery(name = "address")
public Address getUserAddress(@GraphQLContext User) {
}
同样对于突变,我们可以定义:
/**
* <<this currently works>>
* This can be invoked using:
* mutation {
* addUser(user :{
* firstName: "John"
* lastName: "Smith"
* })
* fistName
* }
*/
@GraphQLMutation(name = "addUser")
public User addUser(@GraphQLArgument(name = "user") User user) {
}
现在我正在尝试添加address
,以与我们添加它以进行查询相同的方式添加,但添加为 的输入参数User
。
以下内容仍然在某些 bean 中声明。
/**
* << this is what I am trying to achieve>>
* I want to be able to invoke the following query and not having to declare 'Address' inside of 'User' class.
* mutation {
* addUser(user :{
* firstName: "John"
* lastName: "Smith"
* address: { <-- being able to pass address as argument now, and be part of user.
* streetName: "1 str"
* }
* })
* fistName
* }
*/
// e.g. something like ...
@GraphQLInputField(name = "address")
public void addAddressToUser(@GraphQLContext User user, @GraphQLArgument Address address) {
}