File tree 10 files changed +58
-15
lines changed
java/io/spring/sample/graphql
java/io/spring/sample/graphql
main/java/io/spring/sample/graphql
test/java/io/spring/sample/graphql
10 files changed +58
-15
lines changed Original file line number Diff line number Diff line change @@ -49,10 +49,10 @@ public Mono<BigDecimal> salary(Employee employee) {
49
49
}
50
50
51
51
@ MutationMapping
52
- public void updateSalary (@ Argument ("input" ) SalaryInput salaryInput ) {
52
+ public Mono < Void > updateSalary (@ Argument ("input" ) SalaryInput salaryInput ) {
53
53
String employeeId = salaryInput .getEmployeeId ();
54
54
BigDecimal salary = salaryInput .getNewSalary ();
55
- this .salaryService .updateSalary (employeeId , salary );
55
+ return this .salaryService .updateSalary (employeeId , salary );
56
56
}
57
57
58
58
}
Original file line number Diff line number Diff line change @@ -23,6 +23,11 @@ public class SalaryInput {
23
23
24
24
private BigDecimal newSalary ;
25
25
26
+ public SalaryInput (String employeeId , BigDecimal newSalary ) {
27
+ this .employeeId = employeeId ;
28
+ this .newSalary = newSalary ;
29
+ }
30
+
26
31
public String getEmployeeId () {
27
32
return employeeId ;
28
33
}
Original file line number Diff line number Diff line change @@ -32,9 +32,9 @@ public Mono<BigDecimal> getSalaryForEmployee(Employee employee) {
32
32
return Mono .just (new BigDecimal ("42" ));
33
33
}
34
34
35
- @ Secured ({ "ROLE_HR" } )
36
- public void updateSalary (String employeeId , BigDecimal newSalary ) {
37
- // empty
35
+ @ Secured ("ROLE_HR" )
36
+ public Mono < Void > updateSalary (String employeeId , BigDecimal newSalary ) {
37
+ return Mono . empty ();
38
38
}
39
39
40
40
}
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ public class SecurityConfig {
35
35
@ Bean
36
36
SecurityWebFilterChain springWebFilterChain (ServerHttpSecurity http ) throws Exception {
37
37
return http
38
- .csrf (spec -> spec .disable ())
38
+ .csrf (c -> c .disable ())
39
39
// Demonstrate that method security works
40
40
// Best practice to use both for defense in depth
41
41
.authorizeExchange (requests -> requests .anyExchange ().permitAll ())
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ type Employee {
14
14
15
15
input UpdateSalaryInput {
16
16
employeeId : ID !
17
- salary : String !
17
+ newSalary : String !
18
18
}
19
19
type UpdateSalaryPayload {
20
20
success : Boolean !
Original file line number Diff line number Diff line change 15
15
*/
16
16
package io .spring .sample .graphql ;
17
17
18
+ import java .math .BigDecimal ;
18
19
import java .net .URI ;
19
20
import java .time .Duration ;
20
21
21
22
import org .junit .jupiter .api .AfterEach ;
22
23
import org .junit .jupiter .api .BeforeEach ;
24
+ import org .junit .jupiter .api .Disabled ;
23
25
import org .junit .jupiter .api .Test ;
24
26
25
27
import org .springframework .boot .test .context .SpringBootTest ;
@@ -105,6 +107,21 @@ void canNotQuerySalary() {
105
107
});
106
108
}
107
109
110
+ @ Disabled // This does not work currently
111
+ @ Test
112
+ void canNotMutateUpdateSalary () {
113
+ SalaryInput salaryInput = new SalaryInput ("1" , BigDecimal .valueOf (44 ));
114
+
115
+ this .graphQlTester .documentName ("updateSalary" )
116
+ .variable ("salaryInput" , salaryInput )
117
+ .execute ()
118
+ .errors ()
119
+ .satisfy (errors -> {
120
+ assertThat (errors ).hasSize (1 );
121
+ assertThat (errors .get (0 ).getErrorType ()).isEqualTo (ErrorType .UNAUTHORIZED );
122
+ });
123
+ }
124
+
108
125
@ Test
109
126
void canQuerySalaryAsAdmin () {
110
127
Original file line number Diff line number Diff line change
1
+ mutation updateSalary ($salaryInput : UpdateSalaryInput ! ) {
2
+ updateSalary (input : $salaryInput ) {
3
+ success
4
+ employee {
5
+ id
6
+ name
7
+ }
8
+ }
9
+ }
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ public BigDecimal getSalaryForEmployee(Employee employee) {
15
15
return new BigDecimal ("42" );
16
16
}
17
17
18
- @ Secured ({ "ROLE_HR" } )
18
+ @ Secured ("ROLE_HR" )
19
19
public void updateSalary (String employeeId , BigDecimal newSalary ) {
20
20
21
21
}
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2002-2022 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
package io .spring .sample .graphql ;
2
17
3
18
import org .springframework .context .annotation .Bean ;
@@ -23,9 +38,7 @@ DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Except
23
38
.csrf (c -> c .disable ())
24
39
// Demonstrate that method security works
25
40
// Best practice to use both for defense in depth
26
- .authorizeRequests (requests -> requests
27
- .anyRequest ().permitAll ()
28
- )
41
+ .authorizeRequests (requests -> requests .anyRequest ().permitAll ())
29
42
.httpBasic (withDefaults ())
30
43
.build ();
31
44
}
Original file line number Diff line number Diff line change @@ -75,11 +75,10 @@ void canNotQuerySalary() {
75
75
}
76
76
77
77
@ Test
78
- void canNotMutationUpdateSalary () {
79
- WebGraphQlTester tester = this .graphQlTester .mutate ().build ();
78
+ void canNotMutateUpdateSalary () {
80
79
SalaryInput salaryInput = new SalaryInput ("1" , BigDecimal .valueOf (44 ));
81
80
82
- tester .documentName ("updateSalary" )
81
+ this . graphQlTester .documentName ("updateSalary" )
83
82
.variable ("salaryInput" , salaryInput )
84
83
.execute ()
85
84
.errors ()
You can’t perform that action at this time.
0 commit comments