Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for subscriptions #309

Open
markhamnr opened this issue Dec 27, 2024 · 2 comments
Open

support for subscriptions #309

markhamnr opened this issue Dec 27, 2024 · 2 comments

Comments

@markhamnr
Copy link

Am I wrong in thinking that subscriptions aren't supported currently?

The README implies that they are, or at least it mentions registering a Subscription class as well as a Query class and a Mutation class.

But I can't see any examples, nor can I get it to work myself. The GraphQL Java site says that a subscription should return a Publisher, but when I try that with AnnotationsSchemaCreator I get an InvalidSchemaException: "Publisher" must define one or more fields.

Is there a way to handle subscriptions I haven't figure out yet? Or if not, is this something that could be added?

@jkevan
Copy link
Collaborator

jkevan commented Feb 6, 2025

Hello @markhamnr,

Subscriptions are supported—we are using them in our implementation. This support is not directly related to graphql-java-annotations but rather to graphql-java. However, I agree that this specific case lacks documentation and examples.

I'll try to help:

1. WebSocket Endpoint

First, you need a WebSocket endpoint to receive subscription requests. You can find an example in our implementation:
🔗 [OsgiGraphQLWsEndpoint.java](https://github.com/Jahia/graphql-core/blob/master/graphql-dxm-provider/src/main/java/org/jahia/modules/graphql/provider/dxm/OsgiGraphQLWsEndpoint.java)

2. Base Schema Endpoint

You also need a base schema endpoint to register your subscriptions:

@GraphQLName("Subscription")
public static class Subscription {
}

3. Subscription Implementation Example

Here’s an example of a Subscription implementation:
🔗 [GqlWorkflowSubscriptionExtension.java](https://github.com/Jahia/graphql-core/blob/master/graphql-dxm-provider/src/main/java/org/jahia/modules/graphql/provider/dxm/workflow/GqlWorkflowSubscriptionExtension.java)

A key part that might be missing on your side is that the Publisher should carry a GraphQL object, like this:

Publisher<GqlWorkflowEvent>

Where GqlWorkflowEvent is a GraphQL object annotated with GraphQL annotations:

@GraphQLField
@GraphQLDescription("Workflow that has just been started")
public GqlWorkflow getStartedWorkflow() {
    return startedWorkflow;
}

Hope this helps! Feel free to explore the repository—it’s public and serves as the GraphQL API for our project. 🚀

@markhamnr
Copy link
Author

OK, this is about the simplest example I can come up with:

https://gist.github.com/markhamnr/7a516ef640f353791165ef3f75478eae

When run, it gives me the InvalidSchemaException I mentioned above. Is there some way I can modify it to make it a valid schema?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants