File tree Expand file tree Collapse file tree 5 files changed +46
-1
lines changed
java/io/spring/sample/graphql
test/java/io/spring/sample/graphql Expand file tree Collapse file tree 5 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ repositories {
15
15
}
16
16
17
17
dependencies {
18
- implementation ' org.springframework.boot :spring-boot-starter- graphql'
18
+ implementation project( " :spring-graphql" )
19
19
implementation ' org.springframework.boot:spring-boot-starter-webflux'
20
20
implementation ' org.springframework.boot:spring-boot-starter-security'
21
21
implementation ' org.springframework.boot:spring-boot-starter-actuator'
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
+ */
16
+ package io .spring .sample .graphql ;
17
+
18
+ import org .springframework .graphql .data .method .annotation .QueryMapping ;
19
+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
20
+ import org .springframework .security .core .userdetails .UserDetails ;
21
+ import org .springframework .stereotype .Controller ;
22
+ import reactor .core .publisher .Mono ;
23
+
24
+ @ Controller
25
+ public class CurrentUserController {
26
+
27
+ @ QueryMapping
28
+ Mono <String > username (@ AuthenticationPrincipal Mono <UserDetails > userDetails ) {
29
+ return userDetails .map (UserDetails ::getUsername );
30
+ }
31
+
32
+ }
Original file line number Diff line number Diff line change 1
1
type Query {
2
2
employees : [Employee ]
3
+ username : String
3
4
}
4
5
type Mutation {
5
6
# restricted
Original file line number Diff line number Diff line change
1
+ query {
2
+ username
3
+ }
Original file line number Diff line number Diff line change 22
22
import org .springframework .boot .test .context .SpringBootTest ;
23
23
import org .springframework .graphql .execution .ErrorType ;
24
24
import org .springframework .graphql .test .tester .WebGraphQlTester ;
25
+ import org .springframework .security .test .context .support .WithMockUser ;
25
26
26
27
import static org .assertj .core .api .Assertions .assertThat ;
27
28
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
@@ -71,6 +72,14 @@ void canQueryName() {
71
72
.path ("employees[0].name" ).entity (String .class ).isEqualTo ("Andi" );
72
73
}
73
74
75
+ @ Test
76
+ @ WithMockUser ("rob" )
77
+ void canQueryUsername () {
78
+ this .graphQlTester .queryName ("username" )
79
+ .execute ()
80
+ .path ("username" ).entity (String .class ).isEqualTo ("rob" );
81
+ }
82
+
74
83
@ Test
75
84
void canNotQuerySalary () {
76
85
this .graphQlTester .queryName ("employeesNamesAndSalaries" )
You can’t perform that action at this time.
0 commit comments