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

Any Example for Subscriptions using graphql-java-servlet #144

Closed
BharathaAravind opened this issue Feb 7, 2019 · 2 comments
Closed

Any Example for Subscriptions using graphql-java-servlet #144

BharathaAravind opened this issue Feb 7, 2019 · 2 comments

Comments

@BharathaAravind
Copy link

I haven't found any examples for subscriptions using graphql-java-servlet. Can I get a link or example on how to use subscriptions

@wleroux
Copy link
Contributor

wleroux commented Feb 10, 2019

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.xml

graphql:
  servlet:
    mapping: /graphql
    enabled: true
    websocket:
      enabled: true

GraphQL Schema Definition

type 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)

@oliemansm
Copy link
Member

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

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

3 participants