简体   繁体   中英

Graphql in java with complex objects

I searched a lot on google as well as on stackoverflow. Most of the examples are from spring boot. I am newbie for springboot. and I am very confused on how to fetch the data using graphql. In below example, I have one report group that can have multiple reports in it.

How do I fetch details of group along with custom report details?

What should I user? Resolver/DataFetcher?

If resolver please help me with proper explanation

Result should be:

{
    group{
       name : xyz
       reports [
       {
          id:7
          name : report1
       },
       {
         id:8
         name : report2
       },
       {
         id:9
         name : report3
       }
      ]
  }    

}

ReportGroup class

@Entity
@Table(name = "reportgroup")
public class ReportGroup{
  private String name;
  private List<CustomReport> customReports;
}

public String getName() {
    return this.name;
}

public void setName(final String name) {
    this.name = name;
}

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "report_group_custom_report", joinColumns = {
        @JoinColumn(name = "REPORT_GROUP_ID") }, inverseJoinColumns = {
                @JoinColumn(name = "CUSTOM_REPORT_ID") })
public List<CustomReport> getCustomReports() {
    return this.customReports;
}

Rest api call that returns group along with reports

@RequestMapping(value = "/secure/getReportGroupDetail/{name}", method = RequestMethod.POST)
public ResponseBody<GroupDTO> getReportGroupDetail(
    @PathVariable("id") final Integer id,
    final HttpServletRequest request) throws RecordNotFoundException {
final ReportGroup group = this.reportGroupService.getReportGroupDetail(id);     
return new ResponseBody(dto, Constants.SUCCESS);
}

GroupService.java

@Transactional(readOnly = true)
    public ReportGroup getReportGroupDetail(final Integer id){
return this.reportGroupDAO
                .findById(id);

}

Schema:

schema {
    query: Query
}
type Query {
    reportgroup:ReportGroup
}

type ReportGroup{
    name:String!,
     customReport : [CustomReport]
}

DataFetcher

public class ReportGroupDataFetcher implements DataFetcher<ReportGroup> {

@Autowired
ReportGroupService reportGroupService;

@Override
public Group get(final DataFetchingEnvironment env) {   
    Group   group = this.reportGroupService.getReportGroupDetail(1);        
    return group;
}

}

The Controller

@RequestMapping(value = "/secure/getGroupDetail", method = RequestMethod.POST)
    public ResponseBody<Object> getGroupDetail(     
            @RequestBody final JsonData jsonData,
            final HttpServletRequest request)  {
        final JSONObject jsonRequest = new JSONObject(jsonData.getData());  
        final GraphQL graphQL = this.schemaBuilder.getGraphQL();        
        final ExecutionInput executionInput = ExecutionInput.newExecutionInput()
                .query(jsonRequest.getString("query")).build();     
        final ExecutionResult executionResult = graphQL.execute(executionInput);        
        return new ResponseBody(executionResult, Constants.SUCCESS);
    }

To Load Schema

public class SchemaBuilder {
@Autowired
ReportGroupDataFetcher reportgroupDataFetcher;


public GraphQL getGraphQL() {
    final SchemaParser schemaParser = new SchemaParser();
    final SchemaGenerator schemaGenerator = new SchemaGenerator();
    final File schemaFile = new File(myFileName);
    final TypeDefinitionRegistry typeRegistry = schemaParser
            .parse(schemaFile);
    final RuntimeWiring wiring = buildRuntimeWiring();
    final GraphQLSchema graphQLSchema = schemaGenerator
            .makeExecutableSchema(typeRegistry, wiring);
    return GraphQL.newGraphQL(graphQLSchema).build();
}

private RuntimeWiring buildRuntimeWiring() {
    return RuntimeWiring.newRuntimeWiring().type("Query",
            typeWiring -> typeWiring                        
                    .dataFetcher("reportgroup", this.reportgroupDataFetcher))               
            .build();
    }


 }

You need to define your customer group data as well. Try something like the below.

  schema {
        query: Query
    }
    type CustomReport{
    id:Int!,
    name:String!

    }
    type Query {
        reportgroup:ReportGroup
    }

    type ReportGroup{
        name:String!,
         customReport : [CustomReport]
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM