-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ResponseMapFactory #3857 #3894
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
ResponseMapFactory #3857 #3894
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
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
26 changes: 26 additions & 0 deletions
26
src/main/java/graphql/execution/DefaultResponseMapFactory.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,26 @@ | ||
package graphql.execution; | ||
|
||
import com.google.common.collect.Maps; | ||
import graphql.Internal; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Implements the contract of {@link ResponseMapFactory} with {@link java.util.LinkedHashMap}. | ||
* This is the default of graphql-java since a long time and changing it could cause breaking changes. | ||
*/ | ||
@Internal | ||
public class DefaultResponseMapFactory implements ResponseMapFactory { | ||
|
||
@Override | ||
public Map<String, Object> createInsertionOrdered(List<String> keys, List<Object> values) { | ||
Map<String, Object> result = Maps.newLinkedHashMapWithExpectedSize(keys.size()); | ||
int ix = 0; | ||
for (Object fieldValue : values) { | ||
String fieldName = keys.get(ix++); | ||
result.put(fieldName, fieldValue); | ||
} | ||
return result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package graphql.execution; | ||
|
||
import graphql.ExperimentalApi; | ||
import graphql.PublicSpi; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Allows to customize the concrete class {@link Map} implementation. For example, it could be possible to use | ||
* memory-efficient implementations, like eclipse-collections. | ||
*/ | ||
@ExperimentalApi | ||
@PublicSpi | ||
public interface ResponseMapFactory { | ||
|
||
/** | ||
* The default implementation uses JDK's {@link java.util.LinkedHashMap}. | ||
*/ | ||
ResponseMapFactory DEFAULT = new DefaultResponseMapFactory(); | ||
|
||
/** | ||
* The general contract is that the resulting map keeps the insertion orders of keys. Values are nullable but keys are not. | ||
* Implementations are free to create or to reuse map instances. | ||
* | ||
* @param keys the keys like k1, k2, ..., kn | ||
* @param values the values like v1, v2, ..., vn | ||
* @return a new or reused map instance with (k1,v1), (k2, v2), ... (kn, vn) | ||
*/ | ||
Map<String, Object> createInsertionOrdered(List<String> keys, List<Object> values); | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/test/groovy/graphql/CustomMapImplementationTest.groovy
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,69 @@ | ||
package graphql | ||
|
||
import graphql.execution.ResponseMapFactory | ||
import graphql.schema.idl.RuntimeWiring | ||
import groovy.transform.Immutable | ||
import spock.lang.Specification | ||
|
||
class CustomMapImplementationTest extends Specification { | ||
|
||
@Immutable | ||
static class Person { | ||
String name | ||
@SuppressWarnings('unused') // used by graphql-java | ||
int age | ||
} | ||
|
||
class CustomResponseMapFactory implements ResponseMapFactory { | ||
|
||
@Override | ||
Map<String, Object> createInsertionOrdered(List<String> keys, List<Object> values) { | ||
return Collections.unmodifiableMap(DEFAULT.createInsertionOrdered(keys, values)) | ||
} | ||
} | ||
|
||
def graphql = TestUtil.graphQL(""" | ||
type Query { | ||
people: [Person!]! | ||
} | ||
|
||
type Person { | ||
name: String! | ||
age: Int! | ||
} | ||
|
||
""", | ||
RuntimeWiring.newRuntimeWiring() | ||
.type("Query", { | ||
it.dataFetcher("people", { List.of(new Person("Mario", 18), new Person("Luigi", 21))}) | ||
}) | ||
.build()) | ||
.responseMapFactory(new CustomResponseMapFactory()) | ||
.build() | ||
|
||
def "customMapImplementation"() { | ||
when: | ||
def input = ExecutionInput.newExecutionInput() | ||
.query(''' | ||
query { | ||
people { | ||
name | ||
age | ||
} | ||
} | ||
''') | ||
.build() | ||
|
||
def executionResult = graphql.execute(input) | ||
|
||
then: | ||
executionResult.errors.isEmpty() | ||
executionResult.data == [ people: [ | ||
[name: "Mario", age: 18], | ||
[name: "Luigi", age: 21], | ||
]] | ||
executionResult.data.getClass().getSimpleName() == 'UnmodifiableMap' | ||
executionResult.data['people'].each { it -> it.getClass().getSimpleName() == 'UnmodifiableMap' } | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/test/groovy/graphql/execution/DefaultResponseMapFactoryTest.groovy
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,42 @@ | ||
package graphql.execution | ||
|
||
import spock.lang.Specification | ||
|
||
class DefaultResponseMapFactoryTest extends Specification { | ||
|
||
def "no keys"() { | ||
given: | ||
var sut = new DefaultResponseMapFactory() | ||
|
||
when: | ||
var result = sut.createInsertionOrdered(List.of(), List.of()) | ||
|
||
then: | ||
result.isEmpty() | ||
result instanceof LinkedHashMap | ||
} | ||
|
||
def "1 key"() { | ||
given: | ||
var sut = new DefaultResponseMapFactory() | ||
|
||
when: | ||
var result = sut.createInsertionOrdered(List.of("name"), List.of("Mario")) | ||
|
||
then: | ||
result == ["name": "Mario"] | ||
result instanceof LinkedHashMap | ||
} | ||
|
||
def "2 keys"() { | ||
given: | ||
var sut = new DefaultResponseMapFactory() | ||
|
||
when: | ||
var result = sut.createInsertionOrdered(List.of("name", "age"), List.of("Mario", 18)) | ||
|
||
then: | ||
result == ["name": "Mario", "age": 18] | ||
result instanceof LinkedHashMap | ||
} | ||
} |
Oops, something went wrong.
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.