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 {
1515}
1616
1717dependencies {
18- implementation ' org.springframework.boot :spring-boot-starter- graphql'
18+ implementation project( " :spring-graphql" )
1919 implementation ' org.springframework.boot:spring-boot-starter-webflux'
2020 implementation ' org.springframework.boot:spring-boot-starter-security'
2121 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 11type Query {
22 employees : [Employee ]
3+ username : String
34}
45type Mutation {
56 # 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 2222import org .springframework .boot .test .context .SpringBootTest ;
2323import org .springframework .graphql .execution .ErrorType ;
2424import org .springframework .graphql .test .tester .WebGraphQlTester ;
25+ import org .springframework .security .test .context .support .WithMockUser ;
2526
2627import static org .assertj .core .api .Assertions .assertThat ;
2728import static org .assertj .core .api .Assertions .assertThatThrownBy ;
@@ -71,6 +72,14 @@ void canQueryName() {
7172 .path ("employees[0].name" ).entity (String .class ).isEqualTo ("Andi" );
7273 }
7374
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+
7483 @ Test
7584 void canNotQuerySalary () {
7685 this .graphQlTester .queryName ("employeesNamesAndSalaries" )
You can’t perform that action at this time.
0 commit comments