@@ -124,3 +124,40 @@ One thing to note is the above only works if you use `DataLoaderDispatcherInstru
124124is called. If this was not in place, then all the promises to data will never be dispatched ot the batch loader function
125125and hence nothing would ever resolve.
126126
127+ Per Request Data Loaders
128+ ^^^^^^^^^^^^^^^^^^^^^^^^
129+
130+ If you are serving web requests then the data can be specific to the user requesting it. If you have user specific data then you will not want to
131+ cache data meant for user A to then later give it user B in a subsequent request.
132+
133+ The scope of your DataLoader instances is important. You might want to create them per web request to
134+ ensure data is only cached within that web request and no more.
135+
136+ If your data can be shared across web requests then you might want to scope your data loaders so they survive
137+ longer than the web request say.
138+
139+ But if you are doing per request data loaders then creating a new set of ``GraphQL `` and ``DataLoader `` objects per
140+ request is super cheap. Its the ``GraphQLSchema `` creation that can be expensive, especially if you are using graphql IDL parsing.
141+
142+ Structure your code so that the schema is statically held, perhaps in a static variable or in a singleton IoC component but
143+ build out a new ``GraphQL `` set of objects on each request.
144+
145+
146+ .. code-block :: java
147+
148+ GraphQLSchema staticSchema = staticSchema_Or_MayBeFrom_IoC_Injection();
149+
150+ DataLoaderRegistry registry = new DataLoaderRegistry ();
151+ registry. register(" character" , getCharacterDataLoader());
152+
153+ DataLoaderDispatcherInstrumentation dispatcherInstrumentation
154+ = new DataLoaderDispatcherInstrumentation (registry);
155+
156+ GraphQL graphQL = GraphQL . newGraphQL(staticSchema)
157+ .instrumentation(dispatcherInstrumentation)
158+ .build();
159+
160+ graphQL. execute(" { helloworld }" );
161+
162+ // you can now throw away the GraphQL and hence DataLoaderDispatcherInstrumentation
163+ // and DataLoaderRegistry objects since they are really cheap to build per request
0 commit comments