package sonarqube;

import graphql.GraphQL;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLTypeReference;
import org.sonarqube.ws.Issues;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.issue.SearchWsRequest;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;
import static java.util.Arrays.asList;

public class Schema {

  public static GraphQL create() {

    GraphQLObjectType issueType = newObject()
      .name("Issue")
      .description("A SonarQube issue")
      .field(newFieldDefinition()
        .name("key")
        .description("The key.")
        .type(GraphQLString)
        .build())
      .field(newFieldDefinition()
        .name("message")
        .description("The msg")
        .type(GraphQLString)
        .build())
      .field(newFieldDefinition()
        .name("severity")
        .description("The severity")
        .type(GraphQLString)
        .build())
      .build();

    GraphQLObjectType queryType = newObject()
      .name("sonarQubeQuery")
      .field(newFieldDefinition()
        .type(new GraphQLList(new GraphQLTypeReference("Issue")))
        .name("issues")
        .argument(GraphQLArgument.newArgument()
          .name("severity")
          .type(GraphQLString)
          .build())
        .dataFetcher(env -> searchIssues(new SearchWsRequest().setSeverities(asList((String)env.getArgument("severity")))).getIssuesList())
        .build())
      .field(newFieldDefinition()
        .type(issueType)
        .name("issue")
        .dataFetcher(env -> searchIssues(new SearchWsRequest()).getIssues(0))
        .build())
      .build();

    GraphQLSchema schema = GraphQLSchema.newSchema()
      .query(queryType)
      .build();


    return new GraphQL(schema);

    //Object data = .execute("{issues(severity: \"BLOCKER\") { key, message } }").getData();

  }

  public static Issues.SearchWsResponse searchIssues(SearchWsRequest request) {
    HttpConnector httpConnector = HttpConnector.newBuilder()
      .url("https://sonarqube.com")
      .build();
    WsClient wsClient = WsClientFactories.getDefault().newClient(httpConnector);
    return wsClient.issues().search(request);
  }
}

@@ -111,16 +111,54 @@
 QLList;%0A
+import graphql.schema.GraphQLNonNull;%0A
 import g
@@ -299,24 +299,55 @@
 .ws.Issues;%0A
+import org.sonarqube.ws.Rules;%0A
 import org.s
@@ -800,16 +800,303 @@
 te() %7B%0A%0A
+    GraphQLObjectType ruleType = newObject()%0A      .name(%22Rule%22)%0A      .field(newFieldDefinition()%0A        .name(%22key%22)%0A        .type(GraphQLString)%0A        .build())%0A      .field(newFieldDefinition()%0A        .name(%22name%22)%0A        .type(GraphQLString)%0A        .build())%0A      .build();%0A%0A
     Grap
@@ -1622,93 +1622,647 @@
    .
-build();%0A%0A    GraphQLObjectType queryType = newObject()%0A      .name(%22sonarQubeQuery%22)
+field(newFieldDefinition()%0A        .name(%22rule%22)%0A        .type(ruleType)%0A        .dataFetcher(env -%3E %7B%0A          Issues.Issue issue = (Issues.Issue) env.getSource();%0A          return searchRule(issue.getRule());%0A        %7D)%0A        .build())%0A      .build();%0A%0A    GraphQLObjectType queryType = newObject()%0A      .name(%22sonarQubeQuery%22)%0A%0A      .field(newFieldDefinition()%0A        .type(ruleType)%0A        .name(%22rule%22)%0A        .argument(GraphQLArgument.newArgument()%0A          .name(%22key%22)%0A          .type(new GraphQLNonNull(GraphQLString))%0A          .build())%0A        .dataFetcher(env -%3E searchRule(env.getArgument(%22key%22)))%0A        .build())%0A
 %0A   
@@ -2659,32 +2659,33 @@
       .build())%0A
+%0A
       .field(new
@@ -2994,103 +2994,8 @@
 a);%0A
-%0A    //Object data = .execute(%22%7Bissues(severity: %5C%22BLOCKER%5C%22) %7B key, message %7D %7D%22).getData();%0A%0A
   %7D%0A
@@ -3075,16 +3075,62 @@
 uest) %7B%0A
+    System.out.println(%22HTTP ----- issues %22);%0A
     Http
@@ -3315,16 +3315,44 @@
 ector);%0A
+    request.setPageSize(5);%0A
     retu
@@ -3392,11 +3392,428 @@
 st);%0A  %7D
+%0A%0A  public static Rules.Rule searchRule(String key) %7B%0A    System.out.println(%22HTTP ----- rule %22 + key);%0A    HttpConnector httpConnector = HttpConnector.newBuilder()%0A      .url(%22https://sonarqube.com%22)%0A      .build();%0A    WsClient wsClient = WsClientFactories.getDefault().newClient(httpConnector);%0A    return wsClient.rules().search(new org.sonarqube.ws.client.rule.SearchWsRequest().setRuleKey(key)).getRules(0);%0A  %7D
 %0A%7D%0A

Add rule type