我是新手graphql-spqr所以我希望这是一个简单的问题,但是我找不到一个解决方案,即使经过长时间的搜索。
提示:在我的应用程序中,我使用代码优先/模式最后的方法,我喜欢graphql-spqr,所以没有模式。从文件中加载图形
My User.java以这个
开头@Table(name = "users")
@Entity
@Setter
@Getter
@EntityListeners(AuditingEntityListener.class)
public class User {
@Id
@GraphQLQuery(name = "id", description = "A user's id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
public Long id;
@GraphQLQuery(name = "firstName", description = "User's first name")
@Column(name = "first_name")
public String firstName;
@GraphQLQuery(name = "lastName", description = "User's last name")
@Column(name = "last_name")
public String lastName;
@GraphQLQuery(name = "email", description = "User's email")
public String email;
@GraphQLQuery(name = "uuid", description = "User's uuid")
//@Type(type = "char")
public String uuid;
//@Type(type = "char")
@Transient
public Company company;
@Column(name = "company")
public Long companyId;
@Transient
public Role role;
@Column(name = "role")
public Long roleId;
@Column(name = "pw")
public String password;
@GraphQLQuery(name = "terms", description = "User accepted terms")
public Boolean terms;
@Transient
public String token;
@CreatedDate
public Instant created;
public String getUuid() {
return this.uuid;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public String getEmail() {
return this.email;
}
public String getPassword() {
return this.password;
}
}
通过一个突变创建一个用户:
@GraphQLMutation(name = "createUser")
public User createUser (
@GraphQLArgument(name = "firstName") String firstName,
@GraphQLArgument(name = "lastName") String lastName,
@GraphQLArgument(name = "email") String email,
@GraphQLArgument(name = "password") String password,
@GraphQLArgument(name = "company") String company,
@GraphQLArgument(name = "terms") Boolean terms) throws UserExistsException {
... some business logic
... and finally I use the JpaRepository<User, String> to save the user
return userRepository.save(user);
}
This is the query I am sending to the server
{"operationName":"CreateUser","variables":{"firstName":"Chris","lastName":"Rowing","email":"foo54@bar.com","password":"dada","company":"Test 5","terms":true,"source":"start","invitationId":null},"query":"mutation CreateUser($firstName: String!, $lastName: String!, $email: String!, $password: String!, $terms: Boolean!, $company: String) {n createUser(n firstName: $firstNamen lastName: $lastNamen email: $emailn password: $passwordn terms: $termsn company: $companyn ) {n idn __typenamen }n}n"}
新用户被保存在DB中,一切正常,在我的Angular客户端中,我监听成功事件,在检查器中有以下输出
{"data":{"createUser":{"id":4,"__typename":"User"}}}
我的问题如何定制响应?例如,我还需要响应一个JWT令牌,并且可能隐藏id。到目前为止,我还没有找到这样做的方法,任何帮助都将不胜感激!谢谢!
对于任何遇到相同新手问题的人:以下是我的解决方法:
我在GraphQL查询中添加了token
属性,删除了id
属性,并将其添加到UserService
// Attach a new field called token to the User GraphQL type
@GraphQLQuery
public String token(@GraphQLContext User user) {
return authService.createToken(user.email, user.uuid);
}
通过使用@GraphQLContext