I am new to graphql-spqr so I hope this is an easy question, however I couldn't find a solution for this, even after a long search.
Hint: In my app, I use the code-first/schema-last approach, which I like about the graphql-spqr, so there is no schema.graphqls loaded from a file.
My User.java starts with this
@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;
}
}
A user is created by a mutation:
@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":"[email protected]","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: $firstName\n lastName: $lastName\n email: $email\n password: $password\n terms: $terms\n company: $company\n ) {\n id\n __typename\n }\n}\n"}
The new user gets saved in the DB, everything works fine, and in my Angular client I listen to the success event, and in the inspector there is the following output
{"data":{"createUser":{"id":4,"__typename":"User"}}}
My question How can I customize the response? For example I need to respond also a JWT token, and maybe hide the id. I have not found a way to do this up to now and any help would be appreciated! Thanks!
For anyone who is experiencing the same newbie problem: This is how I solved it:
I added the
tokenproperty to the GraphQL query, removed theidproperty, and added this to the UserServiceIt is possible to add external fields to the response without changing the original User.class by using
@GraphQLContext