question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated input model is empty

See original GitHub issue

Hi, I’m having trouble understanding how to use SPQR. I’ve use a similar project for node https://typegraphql.ml/docs/introduction.html before with much success, which shares a lot of concepts with this.

Questions: ~1. How to correctly generate input types for object classes?~ ~2. How to control the required tribute of a schema object of field, eg (stockSize: Long!)?~ 3. How can I generate a schema minus the extra annotations for use with a client or for documentation? ~4. @GraphQLId does not produce a schema field that uses the type ID?~ ~5. How would I exclude a generated field like Id from an input type but not the output type, does this require multiple classes?~

Simple example

Dependency versions:

Java: 1.8
spring-boot-starter-parent: 2.1.1.RELEASE
io.leangen.graphql.spqr: 0.9.9

Here we have a simple POJO User.java

@Data // lombok
public class User {
    private final @GraphQLId Integer id;
    private String email;
    private final String username;
    private String phoneNumber;
    private final Boolean isActive;
}

And a resolver that references the user in a query and a mutation UserResolver.java

@Component
public class UserResolver {

    private UserService userService;

    @Autowired
    UserResolver(
        UserService userService
    ) {
        this.userService = userService;
    }

    @GraphQLQuery(name = "getUsers")
    public ArrayList<User> getUsers(){
        return this.userService.getUsers();
    }

    @GraphQLMutation(name = "createUser")
    public User createUser(@GraphQLArgument(name = "User") User user){
        return this.userService.creatUser(user);
    }

}

When the schema is rendered with new SchemaPrinter().print(schema); I get the following

#Mutation root
type Mutation {
  createUser(User: UserInput): User @_mappedOperation(operation : "__internal__")
}

#Query root
type Query {
  getUsers: [User] @_mappedOperation(operation : "__internal__")
}

type User @_mappedType(type : "__internal__") {
  email: String @_mappedOperation(operation : "__internal__")
  id: Int @_mappedOperation(operation : "__internal__")
  isActive: Boolean @_mappedOperation(operation : "__internal__")
  phoneNumber: String @_mappedOperation(operation : "__internal__")
  username: String @_mappedOperation(operation : "__internal__")
}

input UserInput @_mappedType(type : "__internal__") {
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
glennji-asxcommented, Oct 10, 2019

Worth trying:

@Getter(onMethod = @__({@GraphQLId }))

… to annotate the generated getter rather than the instance variable itself. Lombok doesn’t copy annotations from vars to generated methods.

0reactions
iamlothiancommented, Oct 30, 2019

Using @Setter(onMethod = @__({@GraphQLIgnore})) is experamental so ill stick with writing the getters and setter myself. For futre reference.

Model

@GraphQLType(name = "User") @GraphQLNonNull
public class User {

    protected String email;

    // applies to GraphQL output model
    @GraphQLNonNull
    public String getMobileNumber() {
        return this.email;
    }

    // applies to GraphQL input model
    public void setEmail(@GraphQLNonNull String value) {
        this.email = value;
    }

}

Resolver

@Component
public class UserResolver {

    private UserService userService;

    @Autowired
    UserResolver(
        UserService userService
    ) {
        this.userService = userService;
    }

    @GraphQLQuery(name = "getUsers")
    @GraphQLNonNull
    public ArrayList<User> getUsers(){
        ArrayList<UserEntity> entities = this.userService.getUsers();
        return User.toUserList(entities);
    }

    @GraphQLMutation(name = "createUser")
    @GraphQLNonNull
    public User createUser(@GraphQLArgument(name = "User") User user){
        return User.toUser(
            this.userService.creatUser(user)
        );
    }

}

will result in the types

type Mutation {
  createUser(User: UserInput!): User!)
}

type Query {
  getUsers: [User!]!
}

type User {
  email: String!
}

input UserInput {
  email: String!
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET MVC - How to generate empty input boxes in loop ...
I think you need to do two things, first is to check if you have an empty model. If you do display input...
Read more >
Model field reference - Django documentation
If True , Django will store empty values as NULL in the database. ... The default value is used when new model instances...
Read more >
empty - Manual - PHP
A variable is considered empty if it does not exist or if its value equals false . empty() does not generate a warning...
Read more >
torch.empty — PyTorch 1.13 documentation
Returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument size . Parameters: size (int...) ......
Read more >
ViewModel data is empty on form submit when using ...
IList<RoleUsersViewModel> model = new List<RoleUsersViewModel>();; IQueryable<ApplicationUser> users ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found