有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GraphQL SPQR扩展输入对象的参数

能够扩展现有的类型是非常棒的,因为它允许 代码模块化和特权分离。关于如何在查询中扩展对象输出,我已经找到了很好的例子(见下文),但没有很好的方法来扩展给定对象的输入

为了便于示例,让我们假设我们有一个类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中,我们可以扩展用户

    /**
     * <<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,与我们为查询添加它的方式相同,但add应该是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) {

    }

共 (0) 个答案