|
| 1 | +--- |
| 2 | +title: "Concerns" |
| 3 | +date: 2018-09-09T12:52:46+10:00 |
| 4 | +draft: false |
| 5 | +tags: [documentation] |
| 6 | +weight: 102 |
| 7 | +description: Application concerns |
| 8 | +--- |
| 9 | +# Application concerns |
| 10 | + |
| 11 | +The graphql-java library concentrates on providing an engine for the execution of queries according to the specification. |
| 12 | + |
| 13 | +It does not concern itself about other high level application concerns such as the following : |
| 14 | + |
| 15 | +- Database access |
| 16 | +- Caching data |
| 17 | +- Data authorisation |
| 18 | +- Data pagination |
| 19 | +- HTTP transfer |
| 20 | +- JSON encoding |
| 21 | +- Code wiring via dependency injection |
| 22 | + |
| 23 | +You need to push these concerns into your business logic layers. |
| 24 | + |
| 25 | +The following are great links to read more about this |
| 26 | + |
| 27 | +- http://graphql.org/learn/serving-over-http/ |
| 28 | +- http://graphql.org/learn/authorization/ |
| 29 | +- http://graphql.org/learn/pagination/ |
| 30 | +- http://graphql.org/learn/caching/ |
| 31 | + |
| 32 | +## Context Objects |
| 33 | + |
| 34 | +You can pass in a context object during query execution that will allow you to better invoke that business logic. |
| 35 | + |
| 36 | +For example the edge of your application could be performing user detection and you need that information inside the |
| 37 | +graphql execution to perform authorisation. |
| 38 | + |
| 39 | +This made up example shows how you can pass yourself information to help execute your queries. |
| 40 | + |
| 41 | +{{< highlight java "linenos=table" >}} |
| 42 | + |
| 43 | + // |
| 44 | + // this could be code that authorises the user in some way and sets up enough context |
| 45 | + // that can be used later inside data fetchers allowing them |
| 46 | + // to do their job |
| 47 | + // |
| 48 | + UserContext contextForUser = YourGraphqlContextBuilder.getContextForUser(getCurrentUser()); |
| 49 | + |
| 50 | + ExecutionInput executionInput = ExecutionInput.newExecutionInput() |
| 51 | + .context(contextForUser) |
| 52 | + .build(); |
| 53 | + |
| 54 | + ExecutionResult executionResult = graphQL.execute(executionInput); |
| 55 | + |
| 56 | + // ... |
| 57 | + // |
| 58 | + // later you are able to use this context object when a data fetcher is invoked |
| 59 | + // |
| 60 | + |
| 61 | + DataFetcher dataFetcher = new DataFetcher() { |
| 62 | + @Override |
| 63 | + public Object get(DataFetchingEnvironment environment) { |
| 64 | + UserContext userCtx = environment.getContext(); |
| 65 | + Long businessObjId = environment.getArgument("businessObjId"); |
| 66 | + |
| 67 | + return invokeBusinessLayerMethod(userCtx, businessObjId); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | +{{< / highlight >}} |
0 commit comments