title: How to make GraphQL making asynchronus/parralel/concurrently query in Java? (code examples inside)
body: <p>I have project in <code>java 8</code> currently.</p>
  
  <p>If i am using only:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.graphql-java&lt;/groupId&gt;
          &lt;artifactId&gt;graphql-spring-boot-starter&lt;/artifactId&gt;
          &lt;version&gt;5.0.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.graphql-java&lt;/groupId&gt;
          &lt;artifactId&gt;graphiql-spring-boot-starter&lt;/artifactId&gt;
          &lt;version&gt;4.0.0&lt;/version&gt; &lt;!--5.0.2 http://localhost:8999/graphiql fails to load--&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>And have schema like:</p>
  
  <pre><code>type Query {
      getAllItems: [TestEntity]
      getItem(dictionaryType: String): TestEntity
      }
  
      type TestEntity {
      code: String!
      name: String
      description: String
      dictionaryType: String
  }
  </code></pre>
  
  <p>1)
  And have <code>resolver</code> like:</p>
  
  <pre><code>@Component
  @RequiredArgsConstructor(onConstructor = @__(@Autowired))
  public class QueryResolver implements GraphQLQueryResolver {
      @Autowired
      private final DictionaryService dictionaryService;
  
      public List&lt;TestEntity&gt; getAllItems() {
          return dictionaryService.getAllDictionaryItemsAsStrings();
      }
  
      public TestEntity getItem(String dictionaryType) {
          return dictionaryService.getDictionaryItemsFirstAsString(dictionaryType);
      }
  }
  </code></pre>
  
  <p>If i ask for <code>two</code> resources at once:</p>
  
  <pre><code>query {
      getAllItems {
          code
          description
      }
          getItem(dictionaryType: "test1") {
          code
          name
          description
      }
  }
  </code></pre>
  
  <p>Does <code>getItem</code> and <code>getAllItems</code> execute <code>asynchronusly</code>/<code>parralel</code>/<code>concurrently</code> or the task is executed one by one?</p>
  
  <p>2)
  If we change our code with the same schema, but withous implementing `resolvers, so like that:</p>
  
  <pre><code>@RestController
  @PreAuthorize("isAuthenticated()")
  @RequiredArgsConstructor(onConstructor = @__(@Autowired))
  public class GraphqlController {
      private final DictionaryService dictionaryService;
  
      private DataFetcher&lt;List&lt;TestEntity&gt;&gt; allDictionaryItemsFetcher;
      private DataFetcher&lt;TestEntity&gt; dictionaryItemFetcher;
  
      @Value("classpath:test-resolvers.graphqls")
      private Resource schemaResource;
      private GraphQL graphQL;
  
      @PostConstruct
      private void loadSchema() throws IOException {
          File schemaFile = schemaResource.getFile();
          TypeDefinitionRegistry registry = new SchemaParser().parse(schemaFile);
          RuntimeWiring wiring = buildWiring();
          GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(registry, wiring);
          graphQL = GraphQL.newGraphQL(schema).build();
      }
  
      private void initializeFetchers() {
          allDictionaryItemsFetcher = dataFetchingEnvironment -&gt; dictionaryService.getAllDictionaryItemsAsStrings();
          dictionaryItemFetcher = dataFetchingEnvironment -&gt;
                  dictionaryService.getDictionaryItemsFirstAsString(dataFetchingEnvironment.getArgument("dictionaryType"));
      }
  
      private RuntimeWiring buildWiring() {
          initializeFetchers();
          return RuntimeWiring.newRuntimeWiring()
                  .type("Query", typeWriting -&gt; typeWriting
                          .dataFetcher("getAllItems", allDictionaryItemsFetcher)
                          .dataFetcher("getItem", dictionaryItemFetcher)
                  )
                  .build();
      }
  
      @PostMapping("graphql")
      public ResponseEntity&lt;Object&gt; getResource(@RequestBody Map&lt;String, Object&gt; request) {
          ExecutionResult result;
              String query = request.get("query").toString();
              Map&lt;String, Object&gt; variables = (Map&lt;String, Object&gt;) (request).get("variables");
  
              ExecutionInput input = ExecutionInput.newExecutionInput()
                      .query(query)
                      .variables(variables)
                      .context(SecurityContextHolder.getContext().getAuthentication())
                      .build();
  
              result = graphQL.execute(input);
          return new ResponseEntity&lt;&gt;(result, HttpStatus.OK);
      }
  
  }
  </code></pre>
  
  <p>It probably does not run asychnously, cause we need to use for example <code>CompletableFuture</code>. I am not sure how to change that code to <code>asynchronous</code>/<code>concurrently</code>/<code>parralel</code> if someone too would should what change i have to made to make it that way i would be glad. 
  Thanks in advance!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In example <code>2)</code> i could write something like:</p>
  
  <pre><code>@PostMapping("graphql")
  public DeferredResult&lt;ResponseEntity&lt;?&gt;&gt; handleReqDefResult(@RequestBody Map&lt;String, Object&gt; request) throws ExecutionException, InterruptedException {
      DeferredResult&lt;ResponseEntity&lt;?&gt;&gt; output = new DeferredResult&lt;&gt;();
  
      CompletableFuture&lt;ExecutionResult&gt;  result;
      String query = request.get("query").toString();
      Map&lt;String, Object&gt; variables = (Map&lt;String, Object&gt;) (request).get("variables");
  
      ExecutionInput input = ExecutionInput.newExecutionInput()
              .query(query)
              .variables(variables)
              .context(SecurityContextHolder.getContext().getAuthentication())
              .build();
  
      result = graphQL.executeAsync(input);
      output.setResult(ResponseEntity.ok(result.get()));
      return output;
  }
  </code></pre>