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.

How to validate fields of @GraphQLArgument ?

See original GitHub issue


This is fantastic library but is not well documented…

I’d like ot know if it possible to validate fields af a type used as GrapQLArgument…

I have the service AuthService with the method

@GraphQLMutation(name = "create")
    public User createUser(@GraphQLArgument(name = "userRequest") UserRequest userRequest) throws ServiceException {
        User user = new User();
        user = mapUser(user, userRequest);

Then the Class UserRequest

public class UserRequest {

    @GraphQLQuery(name = "name")
    @NotEmpty(groups = CreateChecks.class)
    private String name;

    @GraphQLQuery(name = "surname")
    @NotEmpty(groups = CreateChecks.class)
    private String surname;

I sent the JSON

  "query": "mutation ($userRequest : UserRequestInput!) { create(userRequest: $userRequest) { name } } ",
  "params": {
  	"userRequest" : 
    	{ "name" : "Name" } 

I’d like that if change

    @GraphQLQuery(name = "surname")
    @NotEmpty(groups = CreateChecks.class)
    private String surname;

Then the method createUser was not called as the suname is missing and returns an error… Is it possible do something like that ?? How ?? I miss documentation on this things

Is it possibkle to create validationGroups as in REST with GraphQL ??

Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

JohannesFluegelcommented, Dec 3, 2018

If you use Spring Framework, you can write:

public class MyEntity {
  @GraphQLQuery(description = "The description")
  private String myField
public class MyService {
  @GraphQLMutation(name = "createEntity")
  public MyEntity createEntity(@GraphQLArgument(name = "entity") 
    @Valid MyEntity entity) {

If you try to create the entity with

mutation {
  createEntity(entity: {
    myField: ""
  }) {

you will get an error message like

  "data": {
    "createEntity": null
  "errors": [
      "message": "Exception while fetching data (/createEntity) : 
         createEntity.entity.myField: may not be empty",
ferguardiolacommented, Jun 22, 2018

Yes it works!

Install a ValidationModule in GUICE, then annotated the services with @ValidateOnExecution and you can use any validation on methods (including validation groups, custom validation, etc…)

public class AuthServiceImpl implements AuthService {
   public User createUser(@GraphQLArgument(name = "userRequest") @Valid UserRequest userRequest) 
   throws ServiceException {

public class UserRequest {

    @NotBlank(groups = CreateChecks.class, message = "Name must be not empty")
    private String name;

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL validation using directives
A simple example: User sign up form. To see how schema validation could be used, it's easiest to start with an example.
Read more >
graphql.validation.ValidationContext java code examples
graphql.validation. Best Java code snippets using graphql.validation. ... (fieldDef == null) return; Map<String, Argument> argumentMap = argumentMap(field.
Read more >
Argument and Input validation - TypeGraphQL
Scalars. The standard way to ensure that inputs and arguments are correct, such as an email field that really contains a proper e-mail...
Read more >
validator - Go Packages
type FieldArgumentRule interface { CheckFieldArgument( ctx *ValidationContext, field *FieldInfo, argDef *graphql.Argument, arg *ast.
Read more >
GraphQL : Query failed to validate - Stack Overflow
So you don't need to further define what of its fields to be returned as the scalar does not have any fields for...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found