Last active
November 3, 2022 02:19
-
-
Save josephktcheung/cd1b65b321736a520ae9d822ae5a951b to your computer and use it in GitHub Desktop.
Stitching schema with subscription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my case, the secure subscription connection got hanging, nothing in return.
With insecure subscription, it worked fine.