1

我正在关注DGS 订阅的文档,我没有收到任何错误,但也没有取回任何数据。

设置非常简单。在 schema.graphqls 文件中,我定义了订阅:

type Subscription {
    ratings: Rating
}

type Rating {
    stars: Int
}

Java代码如下:

@DgsComponent
public class SubscriptionDataFetcher {
    @DgsData(parentType = "Subscription", field = "ratings")
    public Publisher<Rating> ratings() {
        return Flux.interval(Duration.ofSeconds(1)).map(t -> new Rating(4));
    }
}

如果我现在将 websocket 连接到我的后端,它连接得很好,但没有按预期取回任何数据(不管我怎么做,也尝试使用 JavaScript,也连接得很好,但没有得到任何数据)。

例如使用 curl 连接(但使用 JavaScript 结果是一样的,连接但没有数据):

curl -o - --http1.1 \
    --include \
    --no-buffer \
    --header "Connection: Upgrade" \
    --header "Upgrade: websocket" \
    --header "Host: localhost:8443" \
    --header "Origin: https://localhost:8443" \
    --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
    --header "Sec-WebSocket-Version: 13" \
    https://localhost:8443/subscriptions\?query\=ewoicXVlcnkiOiAic3Vic2NyaXB0aW9uIHsgIHN0b2NrcyB7bmFtZX0gfSIKfQ==

我也尝试过通过 Graphiql 界面进行连接,但出现错误:

subscription {
  ratings {
    stars
  }
}

错误信息:

{
  "message": "response is not defined",
  "stack": "ReferenceError: response is not defined\n    at https://localhost:8443/graphiql:46:35\n    at async Object.onRun (https://unpkg.com/graphiql/graphiql.min.js:1:540500)"
}

从链接中的示例中我不清楚的另一件事是如何实际管理订阅。因此,例如,假设我想在发生突变时发布通知。任何使用 Netflix DGS 框架如何完成的指针也将不胜感激。

4

1 回答 1

3

不幸的是,DGS 附带的 graphiql 界面似乎无法正确处理订阅 - 如果您添加playground-spring-boot-starter到您的项目中,将提供更完善的工具/playground,它完全支持订阅。如果您在那里尝试订阅,它应该可以工作(假设您已经graphql-dgs-subscriptions-websockets-autoconfigure按照文档添加)。

关于您的第二个问题,即如果发生突变,如何发布通知 - 不幸的是,文档中缺少这一点,但示例 repo中有一个示例。

我在这里简化了这个例子。如果你想支持这样的订阅和突变:

@DgsSubscription
public Publisher<Review> reviewAdded() {
    return reviewsService.getReviewsPublisher();
}

@DgsMutation
public Review addReview(@InputArgument SubmittedReview review) {
    return reviewsService.saveReview(review);
}

在您的服务中,您将创建一个 Flux(返回给订阅者)并保留对其发射器的引用,以便您可以在发生突变时调用 next。

@Service
public class ReviewsService {

    private FluxSink<Review> reviewsStream;
    private ConnectableFlux<Review> reviewsPublisher;

    @PostConstruct
    public void init() {
        Flux<Review> publisher = Flux.create(emitter -> {
            reviewsStream = emitter;
        });

        reviewsPublisher = publisher.publish();
        reviewsPublisher.connect();
    }

    public Review saveReview(SubmittedReview reviewInput) {
        Review review = Review.newBuilder()
                .username(reviewInput.getUsername())
                .starScore(reviewInput.getStarScore())
                .submittedDate(OffsetDateTime.now()).build();

        // Save to the database, etc.

        reviewsStream.next(review); // publishes the review to subscribers
        return review;
    }

    public Publisher<Review> getReviewsPublisher() {
        return reviewsPublisher;
    }
}
于 2021-05-08T21:45:37.780 回答