如何在使用 spqr-spring-boot-starter 时获取 GraphQL 的模式文件?

huangapple 未分类评论56阅读模式
英文:

How to get a schema file in graphql using spqr-spring-boot-starter?

问题

以下是翻译好的部分:

有谁可以帮我一下,教我如何使用 spqr-spring-boot-starter 获取模式文件?

我在网上寻找了解决方案,找到了这个链接:https://stackoverflow.com/questions/52086525/how-to-get-the-generated-scheme-file-graphqls-using-spqr

new SchemaPrinter(
                  // 根据需要调整选项
                  SchemaPrinter.Options.defaultOptions()
                          .includeScalarTypes(true)
                          .includeExtendedScalarTypes(true)
                          .includeIntrospectionTypes(true)
                          .includeSchemaDefintion(true)
            ).print(schema);

但我不确定我应该传什么给 schema?因为我正在使用 spqr-spring-boot-starter,我没有编写任何与 GraphQLSchema 实例相关的内容。

我需要模式文件以在 Postman 中启用自动补全。如果您对此了解,请提供帮助,这将非常有帮助。谢谢!

英文:

Could anyone please help me how to get a schema file using spqr-spring-boot-starter?

I looked for the solutions online and found this: https://stackoverflow.com/questions/52086525/how-to-get-the-generated-scheme-file-graphqls-using-spqr

new SchemaPrinter(
                  // Tweak the options accordingly
                  SchemaPrinter.Options.defaultOptions()
                          .includeScalarTypes(true)
                          .includeExtendedScalarTypes(true)
                          .includeIntrospectionTypes(true)
                          .includeSchemaDefintion(true)
            ).print(schema);

But I'm not sure what should I pass for schema? As I'm using spqr-spring-boot-starter, I'm not writing anything related to GraphQLSchema instance.

I need the schema file to enable auto-completion in Postman. Please assist if you know anything about this, this will be really helpful. Thanks!

答案1

得分: 0

根据spqr教程,您会希望在RestController类中定义您的GraphQL模式,您可以在代码的其他地方使用该对象,只需将其设置为公共对象。

@Autowired
public GraphQLController(CheckConfigurationDemoResolver checkConfigurationDemoResolver) {
    GraphQLSchema schema = new GraphQLSchemaGenerator()
            .withBasePackages("com.acme.mygraphqlproj")
            .withOperationsFromSingleton(checkConfigurationDemoResolver)
            .generate();
    graphQL = GraphQL.newGraphQL(schema).build();
    String schemaString = new SchemaPrinter().print(schema);
    System.out.println("schemaString = " + schemaString);
}
英文:

Per spqr tutorial, you would want to define your graphQL schema in the RestController class, you can use that object for SchemaPrinter.
You can set it public and use it elsewhere in the code

@Autowired
public GraphQLController(CheckConfigurationDemoResolver checkConfigurationDemoResolver) {
    GraphQLSchema schema = new GraphQLSchemaGenerator()
    schema = new GraphQLSchemaGenerator()
            .withBasePackages("com.acme.mygraphqlproj")
            .withOperationsFromSingleton(checkConfigurationDemoResolver)
            .generate();
    graphQL = GraphQL.newGraphQL(schema).build();
    String schemaString = new SchemaPrinter().print(schema);
    System.out.println("schemaString = " + schemaString);
}

huangapple
  • 本文由 发表于 2020年7月27日 07:57:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/63107022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定