Skip to content

feat: add equals criteria support for custom attributes types #133

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

Merged
merged 4 commits into from
May 5, 2019

Conversation

igdianov
Copy link
Collaborator

@igdianov igdianov commented May 5, 2019

This PR adds support for EQ predicate with custom attribute types annotated with @Convert, i.e.

Given entity class with custom attribute:

@Entity("TaskVariable")
public TaskVariableEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String type;

    private String name;

    @Convert(converter = VariableValueJsonConverter.class)
    @Column(columnDefinition="text")
    private VariableValue<?> value;
}

And custom attribute value implementation with provided constructor having single Object argument :

public class VariableValue<T> {

    private T value;

    public VariableValue() {
    }
    // Required constructor
    public VariableValue(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }
}    

And AttributeConverter class:

public class VariableValueJsonConverter implements AttributeConverter<VariableValue<?>, String> {

    private static ObjectMapper objectMapper = new ObjectMapper()
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    public VariableValueJsonConverter() {
    }

    @Override
    public String convertToDatabaseColumn(VariableValue<?> variableValue) {
        try {
            return objectMapper.writeValueAsString(variableValue);
        } catch (JsonProcessingException e) {
            throw new QueryException("Unable to serialize variable.", e);
        }
    }

    @Override
    public VariableValue<?> convertToEntityAttribute(String dbData) {
        try {
            return objectMapper.readValue(dbData, VariableValue.class);
        } catch (IOException e) {
            throw new QueryException("Unable to deserialize variable.", e);
        }
    }
    
}

With the following values stored as Json:

insert into TASK_VARIABLE (name, type, value) values
  ('variable1', 'string', '{"value":"data"}'),
  ('variable2', 'boolean', '{"value":true}'),
  ('variable3', 'object', '{"value":null}'),
  ('variable4', 'json', '{"value":{"key":"data"}}'),
  ('variable5', 'double', '{"value":1.2345}'),
  ('variable6', 'int', '{"value":12345}'),
  ('variable7', 'json', '{"value":[1,2,3,4,5]}');	

When the following query with EQ predicate for value attribute is excuted:

query {
  TaskVariables(where: {
    name: {EQ: "variable2"}
    value: {EQ: true}
  }) {
    select {
       name
       value
    }
  }
}

Then the result is as follows:

{
  "data": {
    "TaskVariables": {
      "select": [
        {
          "name": "variable2",
          "value": true
        }
      ]
    }
  }
}

See tests for other examples: https://github.com/introproventures/graphql-jpa-query/pull/133/files#diff-6d177f64d7dab4deb5f58343b51bd18aR332

@igdianov igdianov self-assigned this May 5, 2019
@codecov
Copy link

codecov bot commented May 5, 2019

Codecov Report

Merging #133 into master will increase coverage by 0.31%.
The diff coverage is 61.53%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #133      +/-   ##
============================================
+ Coverage     68.08%   68.39%   +0.31%     
- Complexity      400      405       +5     
============================================
  Files            33       33              
  Lines          2068     2079      +11     
  Branches        307      311       +4     
============================================
+ Hits           1408     1422      +14     
+ Misses          535      531       -4     
- Partials        125      126       +1
Impacted Files Coverage Δ Complexity Δ
...hql/jpa/query/schema/impl/JpaPredicateBuilder.java 47.48% <61.53%> (+0.85%) 44 <0> (+2) ⬆️
...a/query/schema/impl/QraphQLJpaBaseDataFetcher.java 74.8% <0%> (+1.04%) 130% <0%> (+2%) ⬆️
...ventures/graphql/jpa/query/schema/JavaScalars.java 46.66% <0%> (+1.25%) 5% <0%> (+1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1a27eee...9200fea. Read the comment docs.

@igdianov igdianov force-pushed the igdianov-equals-converter-values branch from c923501 to 9200fea Compare May 5, 2019 05:41
@igdianov igdianov changed the title feat: add equals criteria support for custom attributes converters feat: add equals criteria support for custom attributes types May 5, 2019
@igdianov igdianov merged commit a18a6ce into master May 5, 2019
@igdianov igdianov deleted the igdianov-equals-converter-values branch May 5, 2019 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant