`
openxtiger
  • 浏览: 147738 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

【GraphQL Java】implements,unions 的实现

阅读更多

编写 以下 GraphQL schema

1.定义AppUsageRecord的类型

 

 # Store usage for app subscriptions with usage pricing.
type AppUsageRecord implements Node {
    # The date and time when the usage record was created.
    # createdAt: DateTime!

    # The description of the app usage record.
    description: String!

    # Globally unique identifier.
    id: ID!

    # The price of the usage record. The only permitted currency code is USD.
    #price: MoneyV2!

    # Defines the usage pricing plan the merchant is subscribed to.
    subscriptionLineItem: AppSubscriptionLineItem!

}

 2. 定义Node接口

 

 

# An object with an ID to support global identification.
interface Node {
    # Globally unique identifier.
    id: ID!
}

 3.相应的Java AppUsageRecord实现

@Data
//Store usage for app subscriptions with usage pricing.
public class AppUsageRecord implements Node {
    //The date and time when the usage record was created.
    private OffsetDateTime createdAt;

    //The description of the app usage record.
    private String description;

    //Globally unique identifier.
    private String id;

    //The price of the usage record. The only permitted currency code is USD.
    //private MoneyV2 price;

    //Defines the usage pricing plan the merchant is subscribed to.
    private AppSubscriptionLineItem subscriptionLineItem;

}

  4.相应的Java Node实现

public interface Node {
    //Globally unique identifier.
    String getId();

    void setId(String id);
}

 5. 如果直接运行,系统将会报错

     Caused by: graphql.kickstart.tools.SchemaClassScannerError: Object type 'AppUsageRecord' implements a known interface, but no class could be found for that type name.  Please pass a class for type 'AppUsageRecord' in the parser's dictionary.

 

6. 解决方案:

自定义配置AppUsageRecord在parser's dictionary.中

@Configuration
public class GraphQLConfig {
    @Bean
    public SchemaParserDictionary dictionary() {
        SchemaParserDictionary dictionary = new SchemaParserDictionary();
        dictionary.add(AppUsageRecord.class);
        return dictionary;
    }
}

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics