-
Notifications
You must be signed in to change notification settings - Fork 55
Implementation calculations fields #83
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
.../java/com/introproventures/graphql/jpa/query/schema/impl/CashGraphQLCalculatedFields.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.introproventures.graphql.jpa.query.schema.impl; | ||
|
||
|
||
import javax.persistence.Transient; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class CashGraphQLCalculatedFields { | ||
protected static Map<Class, Map<String, Optional<Transient>>> cashCalcFields = new ConcurrentHashMap<>(); | ||
|
||
public static void clearCashCalcFields() { | ||
cashCalcFields.values().stream().forEach(v -> v.clear()); | ||
cashCalcFields.clear(); | ||
} | ||
|
||
public static boolean isCalcField(Class cls, String field) { | ||
if (cashCalcFields.containsKey(cls)) { | ||
if (cashCalcFields.get(cls).containsKey(field)) { | ||
return cashCalcFields.get(cls).get(field).isPresent(); | ||
} | ||
} | ||
|
||
Optional<Transient> cf = getTransient(cls, field); | ||
addCashCalcFields(cls, field, cf); | ||
|
||
return cf.isPresent(); | ||
} | ||
|
||
public static void addCashCalcFields(Class cls, String field, Optional<Transient> an) { | ||
if (!cashCalcFields.containsKey(cls)) { | ||
Map<String, Optional<Transient>> tpMap = new ConcurrentHashMap<>(); | ||
cashCalcFields.put(cls, tpMap); | ||
} | ||
|
||
cashCalcFields.get(cls).put(field, an); | ||
} | ||
|
||
public static Optional<Transient> getTransient(Class cls, String field) { | ||
Optional<Transient> calcField = Arrays.stream(cls.getDeclaredFields()) | ||
.filter(f -> f.getName().equals(field) && f.isAnnotationPresent(Transient.class)) | ||
.map(f -> f.getAnnotation(Transient.class)) | ||
.findFirst(); | ||
|
||
if (!calcField.isPresent()) { | ||
calcField = getGraphQLCalcMethod(cls, field, "get"); | ||
} | ||
|
||
if (!calcField.isPresent()) { | ||
calcField = getGraphQLCalcMethod(cls, field, "is"); | ||
} | ||
|
||
return calcField; | ||
} | ||
|
||
public static Optional<Transient> getGraphQLCalcMethod(Class cls, String field, String prefix) { | ||
String methodName = prefix + field.substring(0,1).toUpperCase() + field.substring(1); | ||
|
||
return Arrays.stream(cls.getDeclaredMethods()) | ||
.filter(m -> m.getName().equals(methodName) && m.isAnnotationPresent(Transient.class)) | ||
.map(m -> m.getAnnotation(Transient.class)) | ||
.findFirst() | ||
; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...y-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/CalcEntityTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.introproventures.graphql.jpa.query.schema; | ||
|
||
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor; | ||
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.util.Assert; | ||
|
||
import javax.persistence.EntityManager; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.NONE) | ||
@TestPropertySource({"classpath:hibernate.properties"}) | ||
public class CalcEntityTests { | ||
@SpringBootApplication | ||
static class Application { | ||
@Bean | ||
public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaBuilder) { | ||
return new GraphQLJpaExecutor(graphQLSchemaBuilder.build()); | ||
} | ||
|
||
@Bean | ||
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) { | ||
|
||
return new GraphQLJpaSchemaBuilder(entityManager) | ||
.name("GraphQLCalcFields") | ||
.description("CalcFields JPA test schema"); | ||
} | ||
|
||
} | ||
|
||
@Autowired | ||
private GraphQLExecutor executor; | ||
|
||
@Test | ||
public void contextLoads() { | ||
Assert.isAssignable(GraphQLExecutor.class, executor.getClass()); | ||
} | ||
|
||
@Test | ||
public void getAllRecords() { | ||
//given | ||
String query = "query GraphQLCalcFields { CalcEntities { select {id title fieldMem fieldFun logic customLogic } } }"; | ||
|
||
String expected = "{CalcEntities={select=[{id=1, title=title 1, fieldMem=member, fieldFun=title 1 function, logic=true, customLogic=false}, {id=2, title=title 2, fieldMem=member, fieldFun=title 2 function, logic=true, customLogic=false}]}}"; | ||
|
||
//when | ||
Object result = executor.execute(query).getData(); | ||
|
||
//then | ||
assertThat(result.toString()).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
public void testIgnoreFields() { | ||
String query = "query GraphQLCalcFields { CalcEntities { select {id title fieldMem fieldFun logic customLogic hideField hideFieldFunction } } }"; | ||
|
||
String expected = "[ValidationError{validationErrorType=FieldUndefined, queryPath=[CalcEntities, select, hideField], message=Validation error of type FieldUndefined: Field 'hideField' in type 'CalcEntity' is undefined @ 'CalcEntities/select/hideField', locations=[SourceLocation{line=1, column=95}], description='Field 'hideField' in type 'CalcEntity' is undefined'}, ValidationError{validationErrorType=FieldUndefined, queryPath=[CalcEntities, select, hideFieldFunction], message=Validation error of type FieldUndefined: Field 'hideFieldFunction' in type 'CalcEntity' is undefined @ 'CalcEntities/select/hideFieldFunction', locations=[SourceLocation{line=1, column=105}], description='Field 'hideFieldFunction' in type 'CalcEntity' is undefined'}]"; | ||
|
||
//when | ||
Object result = executor.execute(query).getErrors(); | ||
|
||
//then | ||
assertThat(result.toString()).isEqualTo(expected); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...ma/src/test/java/com/introproventures/graphql/jpa/query/schema/model/calc/CalcEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.introproventures.graphql.jpa.query.schema.model.calc; | ||
|
||
import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription; | ||
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore; | ||
import lombok.Data; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Transient; | ||
|
||
@Data | ||
@Entity | ||
public class CalcEntity { | ||
@Id | ||
Long id; | ||
|
||
String title; | ||
|
||
String info; | ||
|
||
@Transient | ||
boolean logic = true; | ||
|
||
@Transient | ||
@GraphQLDescription("i desc member") | ||
String fieldMem = "member"; | ||
|
||
@Transient | ||
@GraphQLIgnore | ||
String hideField = "hideField"; | ||
|
||
@Transient | ||
@GraphQLDescription("i desc function") | ||
public String getFieldFun() { | ||
return title + " function"; | ||
} | ||
|
||
@Transient | ||
public boolean isCustomLogic() { | ||
return false; | ||
} | ||
|
||
public String getHideFieldFunction() { | ||
return "getHideFieldFunction"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.