|
| 1 | +/* |
| 2 | + * Copyright 2021-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { RSocket, RSocketConnector, RSocketServer } from "rsocket-core"; |
| 18 | +import { TcpClientTransport } from "rsocket-tcp-client"; |
| 19 | +import { TcpServerTransport } from "rsocket-tcp-server"; |
| 20 | +import { exit } from "process"; |
| 21 | +import { makeRSocketLink } from "rsocket-graphql-apollo-link"; |
| 22 | +import { RSocketApolloServer } from "rsocket-graphql-apollo-server"; |
| 23 | +import { |
| 24 | + ApolloClient, |
| 25 | + InMemoryCache, |
| 26 | + NormalizedCacheObject, |
| 27 | +} from "@apollo/client/core"; |
| 28 | +import gql from "graphql-tag"; |
| 29 | +import { resolvers } from "./resolvers"; |
| 30 | +import { DocumentNode } from "@apollo/client"; |
| 31 | +import * as fs from "fs"; |
| 32 | +import path from "path"; |
| 33 | +import { RSocketApolloGraphlQLPlugin } from "rsocket-graphql-apollo-server/src/RSocketApolloGraphlQLPlugin"; |
| 34 | + |
| 35 | +let apolloServer: RSocketApolloServer; |
| 36 | +let rsocketClient: RSocket; |
| 37 | + |
| 38 | +function readSchema() { |
| 39 | + return fs.readFileSync(path.join(__dirname, "schema.graphql"), { |
| 40 | + encoding: "utf8", |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function makeRSocketServer({ handler }) { |
| 45 | + return new RSocketServer({ |
| 46 | + transport: new TcpServerTransport({ |
| 47 | + listenOptions: { |
| 48 | + port: 9090, |
| 49 | + host: "127.0.0.1", |
| 50 | + }, |
| 51 | + }), |
| 52 | + acceptor: { |
| 53 | + accept: async () => handler, |
| 54 | + }, |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +function makeRSocketConnector() { |
| 59 | + return new RSocketConnector({ |
| 60 | + transport: new TcpClientTransport({ |
| 61 | + connectionOptions: { |
| 62 | + host: "127.0.0.1", |
| 63 | + port: 9090, |
| 64 | + }, |
| 65 | + }), |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +function makeApolloServer({ typeDefs, resolvers }) { |
| 70 | + const plugin = new RSocketApolloGraphlQLPlugin({ makeRSocketServer }); |
| 71 | + const server = new RSocketApolloServer({ |
| 72 | + typeDefs, |
| 73 | + resolvers, |
| 74 | + plugins: [() => plugin], |
| 75 | + }); |
| 76 | + plugin.setApolloServer(server); |
| 77 | + return server; |
| 78 | +} |
| 79 | + |
| 80 | +function makeApolloClient({ rsocketClient }) { |
| 81 | + return new ApolloClient({ |
| 82 | + cache: new InMemoryCache(), |
| 83 | + link: makeRSocketLink({ |
| 84 | + rsocket: rsocketClient, |
| 85 | + }), |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +async function sendMessage( |
| 90 | + client: ApolloClient<NormalizedCacheObject>, |
| 91 | + { message }: { message: String } |
| 92 | +) { |
| 93 | + console.log("Sending message", { message }); |
| 94 | + await client.mutate({ |
| 95 | + variables: { |
| 96 | + message, |
| 97 | + }, |
| 98 | + mutation: gql` |
| 99 | + mutation CreateMessage($message: String) { |
| 100 | + createMessage(message: $message) { |
| 101 | + message |
| 102 | + } |
| 103 | + } |
| 104 | + `, |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +function subcribe( |
| 109 | + client: ApolloClient<NormalizedCacheObject>, |
| 110 | + variables: Record<any, any>, |
| 111 | + query: DocumentNode |
| 112 | +) { |
| 113 | + return client.subscribe({ |
| 114 | + variables, |
| 115 | + query, |
| 116 | + }); |
| 117 | +} |
| 118 | + |
| 119 | +async function main() { |
| 120 | + // server setup |
| 121 | + const typeDefs = readSchema(); |
| 122 | + apolloServer = makeApolloServer({ typeDefs, resolvers }); |
| 123 | + await apolloServer.start(); |
| 124 | + |
| 125 | + // client setup |
| 126 | + const connector = makeRSocketConnector(); |
| 127 | + rsocketClient = await connector.connect(); |
| 128 | + |
| 129 | + const apolloClient = makeApolloClient({ rsocketClient }); |
| 130 | + |
| 131 | + console.log("\nSubscribing to messages."); |
| 132 | + let subscription = subcribe( |
| 133 | + apolloClient, |
| 134 | + {}, |
| 135 | + gql` |
| 136 | + subscription ChannelMessages { |
| 137 | + messageCreated { |
| 138 | + message |
| 139 | + } |
| 140 | + } |
| 141 | + ` |
| 142 | + ).subscribe({ |
| 143 | + next(data) { |
| 144 | + console.log("subscription event:", data); |
| 145 | + }, |
| 146 | + error(err) { |
| 147 | + console.log(`subscription error: ${err}`); |
| 148 | + }, |
| 149 | + complete() {}, |
| 150 | + }); |
| 151 | + |
| 152 | + await sendMessage(apolloClient, { |
| 153 | + message: "my first message", |
| 154 | + }); |
| 155 | + |
| 156 | + await sendMessage(apolloClient, { |
| 157 | + message: "my second message", |
| 158 | + }); |
| 159 | + |
| 160 | + await sendMessage(apolloClient, { |
| 161 | + message: "my third message", |
| 162 | + }); |
| 163 | + |
| 164 | + subscription.unsubscribe(); |
| 165 | +} |
| 166 | + |
| 167 | +main() |
| 168 | + .catch((error: Error) => { |
| 169 | + console.error(error); |
| 170 | + exit(1); |
| 171 | + }) |
| 172 | + .finally(async () => { |
| 173 | + await apolloServer.stop(); |
| 174 | + rsocketClient.close(); |
| 175 | + }); |
0 commit comments