Skip to content

Instantly share code, notes, and snippets.

@josephktcheung
Last active November 3, 2022 02:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save josephktcheung/cd1b65b321736a520ae9d822ae5a951b to your computer and use it in GitHub Desktop.
Save josephktcheung/cd1b65b321736a520ae9d822ae5a951b to your computer and use it in GitHub Desktop.
Stitching schema with subscription
import { GraphQLServer, Options } from 'graphql-yoga'
import { mergeSchemas } from 'graphql-tools';
import { getRemoteSchema } from "./remoteSchema";
import { SubscriptionClient } from 'subscriptions-transport-ws';
import * as ws from 'ws';
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const start = async () => {
const messageSchema = await getRemoteSchema(process.env.MESSAGE_SERVICE_GRAPHQL_URL, process.env.MESSAGE_SERVICE_SUBSCRIPTION_URL, 'messageService');
const userSchema = await getRemoteSchema(process.env.USER_SERVICE_GRAPHQL_URL, process.env.USER_SERVICE_SUBSCRIPTION_URL);
const schema = mergeSchemas({
schemas: [userSchema, messageSchema] as any,
});
const server = new GraphQLServer({
schema,
context: ({ connection }) => {
if (connection && connection.context) {
return connection.context;
}
},
});
server.start({
port: 4000,
subscriptions: {
onConnect: (connectionParams, websocket, context) => {
return {
subscriptionClients: {
messageService: new SubscriptionClient(process.env.MESSAGE_SERVICE_SUBSCRIPTION_URL, {
connectionParams,
reconnect: true,
}, ws),
},
};
},
onDisconnect: async (websocket, context) => {
const params = await context.initPromise;
const { subscriptionClients } = params;
for (const key in subscriptionClients) {
subscriptionClients[key].close();
}
}
}
}, (options) => console.log('Server is running on http://localhost:4000'))
}
start();
import { HttpLink } from 'apollo-link-http';
import { split } from 'apollo-client-preset';
import { getMainDefinition } from 'apollo-utilities';
import fetch from 'node-fetch';
import { introspectSchema, makeRemoteExecutableSchema } from 'graphql-tools';
import { GraphQLSchema } from "graphql";
export const getRemoteSchema = async (uri: string, subUri: string, clientName?: string): Promise<GraphQLSchema> => {
const httpLink = new HttpLink({ uri, fetch });
const wsLink = (operation, forward) => {
const context = operation.getContext();
const { graphqlContext: { subscriptionClients } } = context;
return subscriptionClients && subscriptionClients[clientName] && subscriptionClients[clientName].request(operation);
};
const link = split(
({ query }) => {
const definition = getMainDefinition(query as any);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'
},
wsLink as any,
httpLink,
);
const schema = await introspectSchema(httpLink);
const executableSchema = makeRemoteExecutableSchema({
schema,
link,
});
return executableSchema as any;
}
@revskill10
Copy link

In my case, the secure subscription connection got hanging, nothing in return.
With insecure subscription, it worked fine.

@maxpain
Copy link

maxpain commented Nov 6, 2018

@revskill10, did you set up it for secure subscriptions?

@kanben-a
Copy link

kanben-a commented Jun 4, 2019

@revskill10 same here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment