-
-
Notifications
You must be signed in to change notification settings - Fork 114
Ability to combine multiple response projections #985 #1031
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
kobylynskyi
merged 10 commits into
develop
from
985-combine-multiple-response-projections
Feb 22, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
02cd272
Fix generation of Interface when return type of the method has a list…
kobylynskyi 5a5a30d
Ability to combine multiple response projections #985
kobylynskyi b140a8e
Merge branch 'develop' into 985-combine-multiple-response-projections
kobylynskyi 7799ed2
Handle conflicting aliases / arguments when combining response projec…
kobylynskyi 1a5bbd9
Kotlin/Scala: combining multiple projections #985
kobylynskyi ea29936
Fix javadoc
kobylynskyi 208e08b
Fix Scala response_projection.ftl
kobylynskyi 2ee3b7b
Fix Scala and Kotlin templates for parametrized_input and response_pr…
kobylynskyi 108933b
Merge branch 'develop' into 985-combine-multiple-response-projections
kobylynskyi 12cde31
Fix imports
kobylynskyi 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
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 |
|---|---|---|
|
|
@@ -5,4 +5,6 @@ | |
| */ | ||
| public interface GraphQLParametrizedInput { | ||
|
|
||
| GraphQLParametrizedInput deepCopy(); | ||
|
|
||
| } | ||
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
76 changes: 71 additions & 5 deletions
76
src/main/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLResponseProjection.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 |
|---|---|---|
| @@ -1,24 +1,90 @@ | ||
| package com.kobylynskyi.graphql.codegen.model.graphql; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.StringJoiner; | ||
|
|
||
| /** | ||
| * The implementation class should basically contain the fields of the particular type which | ||
| * should be returned back to the client. | ||
| * The implementation class should contain the fields of the particular type that should be returned to the client. | ||
| */ | ||
| public abstract class GraphQLResponseProjection { | ||
|
|
||
| protected final List<GraphQLResponseField> fields = new ArrayList<>(); | ||
| /** | ||
| * Contains all response projection fields, where: | ||
| * key - is the name+alias pair (where alias is nullable) | ||
| * value - is GraphQLResponseField which represents the response projection field | ||
| */ | ||
| protected final Map<Pair<String, String>, GraphQLResponseField> fields = new LinkedHashMap<>(); | ||
|
|
||
| protected GraphQLResponseProjection() { | ||
| } | ||
|
|
||
| protected GraphQLResponseProjection(GraphQLResponseProjection projection) { | ||
| if (projection == null) { | ||
| return; | ||
| } | ||
| projection.fields.values().forEach(this::add$); | ||
| } | ||
|
|
||
| protected GraphQLResponseProjection(List<? extends GraphQLResponseProjection> projections) { | ||
| if (projections == null) { | ||
| return; | ||
| } | ||
| for (GraphQLResponseProjection projection : projections) { | ||
| if (projection == null) { | ||
| continue; | ||
| } | ||
| projection.fields.values().forEach(this::add$); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings({"checkstyle:MethodName", "java:S100"}) | ||
| public abstract GraphQLResponseProjection deepCopy$(); | ||
|
|
||
| @SuppressWarnings({"checkstyle:MethodName", "java:S100", "java:S3824"}) | ||
| protected void add$(GraphQLResponseField responseField) { | ||
| Pair<String, String> nameAndAlias = new Pair<>(responseField.getName(), responseField.getAlias()); | ||
| GraphQLResponseField existingResponseField = fields.get(nameAndAlias); | ||
| if (existingResponseField == null) { | ||
| fields.put(nameAndAlias, responseField.deepCopy()); | ||
| return; | ||
| } | ||
|
|
||
| if (!Objects.equals(responseField.getParameters(), existingResponseField.getParameters())) { | ||
| throw new IllegalArgumentException( | ||
| String.format("Field '%s' has an argument conflict", existingResponseField.getName())); | ||
| } | ||
|
|
||
| if (responseField.getAlias() != null) { | ||
| existingResponseField.alias(responseField.getAlias()); | ||
| } | ||
| if (responseField.getParameters() != null) { | ||
| existingResponseField.parameters(responseField.getParameters().deepCopy()); | ||
| } | ||
| if (responseField.getProjection() != null) { | ||
| GraphQLResponseProjection projectionCopy = responseField.getProjection().deepCopy$(); | ||
| if (existingResponseField.getProjection() != null) { | ||
| for (GraphQLResponseField field : projectionCopy.fields.values()) { | ||
| existingResponseField.getProjection().add$(field); | ||
| } | ||
| } else { | ||
| existingResponseField.projection(projectionCopy); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (fields.isEmpty()) { | ||
| return ""; | ||
| } | ||
| StringJoiner joiner = new StringJoiner(" ", "{ ", " }"); | ||
| fields.forEach(field -> joiner.add(field.toString())); | ||
| for (GraphQLResponseField value : fields.values()) { | ||
| joiner.add(value.toString()); | ||
| } | ||
| return joiner.toString(); | ||
| } | ||
|
|
||
| } |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/kobylynskyi/graphql/codegen/model/graphql/Pair.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,50 @@ | ||
| package com.kobylynskyi.graphql.codegen.model.graphql; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Class that represents a key-value pair. | ||
| * | ||
| * @param <K> key | ||
| * @param <V> value | ||
| */ | ||
| public class Pair<K, V> { | ||
|
|
||
| private final K key; | ||
| private final V value; | ||
|
|
||
| public K getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| public V getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public Pair(K key, V value) { | ||
| this.key = key; | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return key + "=" + value; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return key.hashCode() * 13 + (value == null ? 0 : value.hashCode()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o instanceof Pair) { | ||
| Pair<?, ?> pair = (Pair<?, ?>) o; | ||
| return Objects.equals(key, pair.key) && Objects.equals(value, pair.value); | ||
| } | ||
| return false; | ||
| } | ||
| } |
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
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
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
Empty file.
Oops, something went wrong.
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.