0

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":"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: $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!

chriszichrisz
  • 527
  • 5
  • 11
  • 1
    Can you share your GraphQL query? I am not sure if you problem is that you don't know how to select fields or that your `User` object only has the `id` field. But it seems like your mutation correctly returns the `User` type. – Herku Oct 12 '21 at 12:14
  • I edited my question, is it basically that I have to send the parameters I want to be responded along with the request? Sorry for my question I am very new to GraphQL.. – chriszichrisz Oct 12 '21 at 12:31
  • I solved it, thanks for your question pointing me in the right direction! – chriszichrisz Oct 12 '21 at 12:51

1 Answers1

0

For anyone who is experiencing the same newbie problem: This is how I solved it:

I added the token property to the GraphQL query, removed the id property, and added this to the 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);
}

It is possible to add external fields to the response without changing the original User.class by using @GraphQLContext

chriszichrisz
  • 527
  • 5
  • 11