Skip to content

Commit 0adc5f8

Browse files
authored
immutable arguments (graphql-java#1165)
* Added @OverRide as part of errorprone code health check * Revert "Added @OverRide as part of errorprone code health check" This reverts commit 38dfab1 * Immutable arguments * PR feedback on Maps * PR feedback on Maps
1 parent f02ee24 commit 0adc5f8

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/main/java/graphql/schema/DataFetchingEnvironmentImpl.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import graphql.language.Field;
99
import graphql.language.FragmentDefinition;
1010

11+
import java.util.Collections;
12+
import java.util.HashMap;
1113
import java.util.List;
1214
import java.util.Map;
1315

1416
import static graphql.Assert.assertNotNull;
17+
import static java.util.Collections.unmodifiableMap;
1518

16-
@SuppressWarnings({"unchecked","TypeParameterUnusedInFormals"})
19+
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
1720
@Internal
1821
public class DataFetchingEnvironmentImpl implements DataFetchingEnvironment {
1922
private final Object source;
@@ -46,15 +49,15 @@ public DataFetchingEnvironmentImpl(Object source,
4649
ExecutionTypeInfo fieldTypeInfo,
4750
ExecutionContext executionContext) {
4851
this.source = source;
49-
this.arguments = arguments;
52+
this.arguments = arguments == null ? Collections.emptyMap() : arguments;
53+
this.fragmentsByName = fragmentsByName == null ? Collections.emptyMap() : fragmentsByName;
5054
this.context = context;
5155
this.root = root;
5256
this.fieldDefinition = fieldDefinition;
5357
this.fields = fields;
5458
this.fieldType = fieldType;
5559
this.parentType = parentType;
5660
this.graphQLSchema = graphQLSchema;
57-
this.fragmentsByName = fragmentsByName;
5861
this.executionId = executionId;
5962
this.selectionSet = selectionSet;
6063
this.fieldTypeInfo = fieldTypeInfo;
@@ -68,7 +71,7 @@ public <T> T getSource() {
6871

6972
@Override
7073
public Map<String, Object> getArguments() {
71-
return arguments;
74+
return new HashMap<>(arguments);
7275
}
7376

7477
@Override
@@ -123,7 +126,7 @@ public GraphQLSchema getGraphQLSchema() {
123126

124127
@Override
125128
public Map<String, FragmentDefinition> getFragmentsByName() {
126-
return fragmentsByName;
129+
return new HashMap<>(fragmentsByName);
127130
}
128131

129132
@Override
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package graphql.schema
2+
3+
import graphql.execution.ExecutionId
4+
import spock.lang.Specification
5+
6+
import static graphql.execution.ExecutionContextBuilder.*
7+
import static graphql.schema.DataFetchingEnvironmentBuilder.*
8+
9+
class DataFetchingEnvironmentImplTest extends Specification {
10+
11+
def executionContext = newExecutionContextBuilder().executionId(ExecutionId.from("123")).build()
12+
13+
def "immutable arguments"() {
14+
def dataFetchingEnvironment = newDataFetchingEnvironment(executionContext).arguments([arg: "argVal"]).build()
15+
16+
when:
17+
def value = dataFetchingEnvironment.getArguments().get("arg")
18+
then:
19+
value == "argVal"
20+
when:
21+
dataFetchingEnvironment.getArguments().put("arg", "some other value")
22+
value = dataFetchingEnvironment.getArguments().get("arg")
23+
then:
24+
value == "argVal"
25+
}
26+
}

0 commit comments

Comments
 (0)