Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library does not support ! in GraphQL schema during the java classes generation #3

Closed
PavelSusloparov opened this issue Sep 13, 2019 · 5 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@PavelSusloparov
Copy link

Describe the bug
During the java classes generation, all of the fields are nullable fields

To Reproduce
Steps to reproduce the behavior:
Run ./gradlew graphqlCodegen
Check the generated classes. Fields have no difference between nullable and not nullable.

Expected behavior
Nullable fields should be assigned null
Not nullable fields should be the same as of now.

@kobylynskyi
Copy link
Owner

@PavelSusloparov,
Each class variable of reference type by default is initialized with null.
Please also refer to the following article: http://www.javapractices.com/topic/TopicAction.do?Id=14
Please let me know if it is still an issue for you.

@kobylynskyi
Copy link
Owner

@PavelSusloparov, I have one idea.
We can annotate non-nullable fields with javax.validation.constraints.NotNull.
So that bean validation can be executed on the input.
Example:

public class SomeInputType {
    ...
    @javax.validation.constraints.NotNull
    private String mandatoryField;
    ...
}

Please let me know what you think.

@PavelSusloparov
Copy link
Author

PavelSusloparov commented Sep 17, 2019

Tried suggested approach, for some reason it the validation is not picked up by the service.

import java.util.*;

public class TestObject {

    @javax.validation.constraints.NotNull
    private String field;

    public TestObject() {
    }

    public String getField() {
        return field;
    }
    public void setField(String field) {
        this.field = field;
    }

}

If the query is defined in schema.graphqls as

type Query {
    testObject: TestObject!
}

type TestObject {
    field: String
}

and the resolver looks like

@Component
class Query : GraphQLQueryResolver {

    fun testObject() = TestObject()
}

The app starts without errors and when I run

query {
  testObject {
    field
  }
}

it returns success without any errors

{
  "data": {
    "testObject": {
      "field": null
    }
  }
}

However, I expect the error back.

@PavelSusloparov
Copy link
Author

Actually, I did it wrong. With validator here, I have the thing working.

fun testObject(): TestObject {
        val testObject = TestObject()
        val factory = Validation.buildDefaultValidatorFactory()
        val validator = factory.getValidator()
        val violations = validator.validate<TestObject>(testObject)
        val violationsString = violations.joinToString(", ")
        if (violationsString.isNotBlank()) {
            throw Exception(violationsString)
        }
        return testObject
    }

Thanks to https://www.baeldung.com/javax-validation

@kobylynskyi , yes I believe that is the good approach to go with.

@kobylynskyi kobylynskyi self-assigned this Sep 17, 2019
@kobylynskyi kobylynskyi added the enhancement New feature or request label Sep 17, 2019
@kobylynskyi kobylynskyi added this to the 1.2 milestone Sep 17, 2019
@kobylynskyi
Copy link
Owner

Done in 1.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants