繁体   English   中英

Java中带有复杂对象的Graphql

[英]Graphql in java with complex objects

我在Google以及stackoverflow上进行了大量搜索。 大多数示例来自spring boot。 我是springboot的新手。 我对如何使用graphql获取数据感到非常困惑。 在下面的示例中,我有一个报告组,其中可以包含多个报告。

如何获取组的详细信息以及自定义报告的详细信息?

我应该使用什么? 解析器/ DataFetcher?

如果解析器请帮助我进行适当的解释

结果应为:

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

}

ReportGroup类别

@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调用,可返回组以及报告

@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 {
    query: Query
}
type Query {
    reportgroup:ReportGroup
}

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

数据读取器

public class ReportGroupDataFetcher implements DataFetcher<ReportGroup> {

@Autowired
ReportGroupService reportGroupService;

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

}

控制器

@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);
    }

加载架构

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();
    }


 }

您还需要定义客户组数据。 尝试以下类似的方法。

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

    }
    type Query {
        reportgroup:ReportGroup
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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