如何使用spring-boot将GraphQl接口转换为Java世界

发布于 2025-01-22 20:15:11 字数 3490 浏览 2 评论 0 原文

我是GraphQl的新手,我正在努力将GraphQl接口的概念转换为适用于Spring Boot中graphql-java-kickstart库的正确Java代码。我花了很多时间在线搜索答案,但找不到适当的工作示例。在这里,我的简单情况我尝试弄清楚:

graphql schema

  interface Notification {
    id: ID!
    date: String
    isOpened: Boolean!
    title: String
    description: String
  }
  
  type InvitationNotification implements  Notification {
    id: ID!
    date: String
    isOpened: Boolean!
    title: String
    description: String
    sendBy: String
  }
  
  type Query {
    notification(id: ID!): Notification!
  }

my(bad)my(bad)translation


// Query resolver
@Component
public class Query implements GraphQLQueryResolver {

    Notification notification(UUID id) {
        return new InvitationNotification(id,
                OffsetDateTime.now(),
                false,
                "Connection request from notary xyz",
                "Blabla",
                "Notary xyz"
                );
    }
}

// POJO abstract class, omitted getters
public abstract class Notification {

    private final UUID id;

    private final OffsetDateTime date;

    private  final boolean isOpened;

    private final String title;

    private final String description;

    public Notification(UUID id, OffsetDateTime date, boolean isOpened, String title, String description) {
        this.id = id;
        this.date = date;
        this.isOpened = isOpened;
        this.title = title;
        this.description = description;
    }

// POJO that extends
public class InvitationNotification extends Notification {

    private final String sendBy;

    public InvitationNotification(UUID id, OffsetDateTime date, boolean isOpened, String title, String description, String sendBy) {
        super(id, date, isOpened, title, description);
        this.sendBy = sendBy;
    }

    public String getSendBy() {
        return sendBy;
    }
}
 
// Spring config bean
@Configuration
public class GraphQLConfig {

    @Bean
    public SchemaParserDictionary schemaParserDictionary() {
        return new SchemaParserDictionary().add(InvitationNotification.class);
    }

}

使用上述代码,我可以成功执行以下查询,仅查询

# Query
query aQuery {
  notification(id: "c782bbb2-7929-4694-9236-8108b49d5ba7") {
    id,
    title,
    description,
    isOpened,
  }
}
# Result
{
  "data": {
    "notification": {
      "id": "c782bbb2-7929-4694-9236-8108b49d5ba7",
      "title": "Connection request from notary xyz",
      "description": "Blabla",
      "isOpened": false
    }
  }
}

使用 常见属性上面的代码,当我请求其他属性时

# Query
query aQuery {
  notification(id: "c782bbb2-7929-4694-9236-8108b49d5ba7") {
    id,
    title,
    description,
    isOpened,
    sendBy
  }
}
# Result
{
  "errors": [
    {
      "message": "Validation error of type FieldUndefined: Field 'sendBy' in type 'Notification' is undefined @ 'notification/sendBy'",
      "locations": [
        {
          "line": 38,
          "column": 5
        }
      ],
      "extensions": {
        "classification": "ValidationError"
      }
    }
  ],
  "data": null
}

,我猜我必须在某个地方提示库实现该界面,因此它可以执行某种类型的实例,但是我不知道在哪里。

如果将一个示例添加到

请注意,我还在 graphql-java-kickstart/samples

I'm quite new to GraphQL and I'm struggling with how to translate the concept of the GraphQL interface to proper java code that works with the graphql-java-kickstart library in Spring Boot. I've spent quite some time searching online for answers but could not find a proper working example. Here my simple case I try to figure out:

GraphQL schema

  interface Notification {
    id: ID!
    date: String
    isOpened: Boolean!
    title: String
    description: String
  }
  
  type InvitationNotification implements  Notification {
    id: ID!
    date: String
    isOpened: Boolean!
    title: String
    description: String
    sendBy: String
  }
  
  type Query {
    notification(id: ID!): Notification!
  }

My (bad) translation


// Query resolver
@Component
public class Query implements GraphQLQueryResolver {

    Notification notification(UUID id) {
        return new InvitationNotification(id,
                OffsetDateTime.now(),
                false,
                "Connection request from notary xyz",
                "Blabla",
                "Notary xyz"
                );
    }
}

// POJO abstract class, omitted getters
public abstract class Notification {

    private final UUID id;

    private final OffsetDateTime date;

    private  final boolean isOpened;

    private final String title;

    private final String description;

    public Notification(UUID id, OffsetDateTime date, boolean isOpened, String title, String description) {
        this.id = id;
        this.date = date;
        this.isOpened = isOpened;
        this.title = title;
        this.description = description;
    }

// POJO that extends
public class InvitationNotification extends Notification {

    private final String sendBy;

    public InvitationNotification(UUID id, OffsetDateTime date, boolean isOpened, String title, String description, String sendBy) {
        super(id, date, isOpened, title, description);
        this.sendBy = sendBy;
    }

    public String getSendBy() {
        return sendBy;
    }
}
 
// Spring config bean
@Configuration
public class GraphQLConfig {

    @Bean
    public SchemaParserDictionary schemaParserDictionary() {
        return new SchemaParserDictionary().add(InvitationNotification.class);
    }

}

With the above code, I can successfully execute the following query that only queries the common attributes

# Query
query aQuery {
  notification(id: "c782bbb2-7929-4694-9236-8108b49d5ba7") {
    id,
    title,
    description,
    isOpened,
  }
}
# Result
{
  "data": {
    "notification": {
      "id": "c782bbb2-7929-4694-9236-8108b49d5ba7",
      "title": "Connection request from notary xyz",
      "description": "Blabla",
      "isOpened": false
    }
  }
}

With the above code, I get an error when I request the additional attribute

# Query
query aQuery {
  notification(id: "c782bbb2-7929-4694-9236-8108b49d5ba7") {
    id,
    title,
    description,
    isOpened,
    sendBy
  }
}
# Result
{
  "errors": [
    {
      "message": "Validation error of type FieldUndefined: Field 'sendBy' in type 'Notification' is undefined @ 'notification/sendBy'",
      "locations": [
        {
          "line": 38,
          "column": 5
        }
      ],
      "extensions": {
        "classification": "ValidationError"
      }
    }
  ],
  "data": null
}

I'm guessing I have to somewhere hint the library what classes implement the interface, so it can do an Instance Of type of thing, but I have no clue where.

It would be really helpful if an example was added to https://github.com/graphql-java-kickstart/samples

Note that I also reached out on the discussion section of graphql-java-kickstart/samples

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文