-
Notifications
You must be signed in to change notification settings - Fork 149
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
Any Example for Subscriptions using graphql-java-servlet #144
Comments
I was able to get Subscriptions using WebSockets to work by running the example below. Although this will work, it's not secure... WebSockets do not play nice with existing spring boot infrastructure (such as CORS/CSRF, Authentication, etc.) I opted out of this solution and focusing on making Server-Sent Events (SSE) work (#145) as these security concerns are handled by SSE. This will create a servlet at /subscriptions that I can subscribe to: application.xmlgraphql:
servlet:
mapping: /graphql
enabled: true
websocket:
enabled: true GraphQL Schema Definitiontype Subscription {
echo(arg: String!): String!
} Subscription Resolver:@Component
class EchoController: GraphQLSubscriptionResolver {
fun echo(arg: String): Publisher<String> {
return Mono.just(arg)
}
} Client:import ApolloClient from "apollo-client";
import { WebSocketLink } from 'apollo-link-ws'
import { HttpLink } from 'apollo-link-http'
import { split } from 'apollo-link'
import { getMainDefinition } from 'apollo-utilities';
import { InMemoryCache } from 'apollo-cache-inmemory';
import gql from "graphql-tag";
const wsLink = new WebSocketLink({
uri: `wss://${window.location.hostname}/subscriptions`
});
const httpLink = new HttpLink({
uri: `https://${window.location.hostname}/graphql`
});
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
);
const apolloClient = new ApolloClient({
link,
cache: new InMemoryCache()
});
const query = gql`subscription { echo(arg: "Hello") }`
apolloClient.subscribe({query: query, variables: {}}).forEach(payload => payload.data.echo) |
There's an example in the samples project which also shows how to use authentication icw subscriptions: https://github.com/graphql-java-kickstart/samples/tree/master/subscription-with-authentication |
I haven't found any examples for subscriptions using graphql-java-servlet. Can I get a link or example on how to use subscriptions
The text was updated successfully, but these errors were encountered: