forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcernsExamples.java
More file actions
82 lines (59 loc) · 2.11 KB
/
Copy pathConcernsExamples.java
File metadata and controls
82 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package readme;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLSchema;
import static graphql.StarWarsSchema.queryType;
@SuppressWarnings({"unused", "Convert2Lambda"})
public class ConcernsExamples {
private GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryType)
.build();
private GraphQL buildSchema() {
return GraphQL.newGraphQL(schema)
.build();
}
private static class User {
}
static class YourGraphqlContextBuilder {
static UserContext getContextForUser(User user) {
return null;
}
}
private static class UserContext {
}
private User getCurrentUser() {
return null;
}
private Object invokeBusinessLayerMethod(UserContext userCtx, Long businessObjId) {
return null;
}
private GraphQL graphQL = GraphQL.newGraphQL(schema)
.build();
private void contextHelper() {
//
// this could be code that authorises the user in some way and sets up enough context
// that can be used later inside data fetchers allowing them
// to do their job
//
UserContext contextForUser = YourGraphqlContextBuilder.getContextForUser(getCurrentUser());
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.context(contextForUser)
.build();
ExecutionResult executionResult = graphQL.execute(executionInput);
// ...
//
// later you are able to use this context object when a data fetcher is invoked
//
DataFetcher dataFetcher = new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
UserContext userCtx = environment.getContext();
Long businessObjId = environment.getArgument("businessObjId");
return invokeBusinessLayerMethod(userCtx, businessObjId);
}
};
}
}