-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rsocket-graphql-apollo-link & rsocket-graphql-apollo-server (…
…#225) Signed-off-by: Kevin Viglucci <[email protected]> Co-authored-by: Oleh Dokuka <[email protected]>
- Loading branch information
1 parent
e9d5267
commit e7f4f5f
Showing
27 changed files
with
8,124 additions
and
6,009 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ How to publish new releases for this project. | |
|
||
You can either set versions in the `package.json` files manually, or use the `lerna version` command to set them via the Lerna CLI. When setting versions manually, you will also need to set the git tags for each package and version. For this reason, it is recommended you use the `lerna version` command, which will create these tags automatically. | ||
|
||
ex: `@rsocket/[email protected]` | ||
ex: `rsocket-[email protected]` | ||
|
||
Lerna will not push the git tags after creation. You should push the git tags once you are confident in your changes. | ||
|
||
|
This file contains 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
175 changes: 175 additions & 0 deletions
175
packages/rsocket-examples/src/graphql/apollo/client-server/example.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright 2021-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { RSocket, RSocketConnector, RSocketServer } from "rsocket-core"; | ||
import { TcpClientTransport } from "rsocket-tcp-client"; | ||
import { TcpServerTransport } from "rsocket-tcp-server"; | ||
import { exit } from "process"; | ||
import { makeRSocketLink } from "rsocket-graphql-apollo-link"; | ||
import { RSocketApolloServer } from "rsocket-graphql-apollo-server"; | ||
import { | ||
ApolloClient, | ||
InMemoryCache, | ||
NormalizedCacheObject, | ||
} from "@apollo/client/core"; | ||
import gql from "graphql-tag"; | ||
import { resolvers } from "./resolvers"; | ||
import { DocumentNode } from "@apollo/client"; | ||
import * as fs from "fs"; | ||
import path from "path"; | ||
import { RSocketApolloGraphlQLPlugin } from "rsocket-graphql-apollo-server/src/RSocketApolloGraphlQLPlugin"; | ||
|
||
let apolloServer: RSocketApolloServer; | ||
let rsocketClient: RSocket; | ||
|
||
function readSchema() { | ||
return fs.readFileSync(path.join(__dirname, "schema.graphql"), { | ||
encoding: "utf8", | ||
}); | ||
} | ||
|
||
function makeRSocketServer({ handler }) { | ||
return new RSocketServer({ | ||
transport: new TcpServerTransport({ | ||
listenOptions: { | ||
port: 9090, | ||
host: "127.0.0.1", | ||
}, | ||
}), | ||
acceptor: { | ||
accept: async () => handler, | ||
}, | ||
}); | ||
} | ||
|
||
function makeRSocketConnector() { | ||
return new RSocketConnector({ | ||
transport: new TcpClientTransport({ | ||
connectionOptions: { | ||
host: "127.0.0.1", | ||
port: 9090, | ||
}, | ||
}), | ||
}); | ||
} | ||
|
||
function makeApolloServer({ typeDefs, resolvers }) { | ||
const plugin = new RSocketApolloGraphlQLPlugin({ makeRSocketServer }); | ||
const server = new RSocketApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
plugins: [() => plugin], | ||
}); | ||
plugin.setApolloServer(server); | ||
return server; | ||
} | ||
|
||
function makeApolloClient({ rsocketClient }) { | ||
return new ApolloClient({ | ||
cache: new InMemoryCache(), | ||
link: makeRSocketLink({ | ||
rsocket: rsocketClient, | ||
}), | ||
}); | ||
} | ||
|
||
async function sendMessage( | ||
client: ApolloClient<NormalizedCacheObject>, | ||
{ message }: { message: String } | ||
) { | ||
console.log("Sending message", { message }); | ||
await client.mutate({ | ||
variables: { | ||
message, | ||
}, | ||
mutation: gql` | ||
mutation CreateMessage($message: String) { | ||
createMessage(message: $message) { | ||
message | ||
} | ||
} | ||
`, | ||
}); | ||
} | ||
|
||
function subcribe( | ||
client: ApolloClient<NormalizedCacheObject>, | ||
variables: Record<any, any>, | ||
query: DocumentNode | ||
) { | ||
return client.subscribe({ | ||
variables, | ||
query, | ||
}); | ||
} | ||
|
||
async function main() { | ||
// server setup | ||
const typeDefs = readSchema(); | ||
apolloServer = makeApolloServer({ typeDefs, resolvers }); | ||
await apolloServer.start(); | ||
|
||
// client setup | ||
const connector = makeRSocketConnector(); | ||
rsocketClient = await connector.connect(); | ||
|
||
const apolloClient = makeApolloClient({ rsocketClient }); | ||
|
||
console.log("\nSubscribing to messages."); | ||
let subscription = subcribe( | ||
apolloClient, | ||
{}, | ||
gql` | ||
subscription ChannelMessages { | ||
messageCreated { | ||
message | ||
} | ||
} | ||
` | ||
).subscribe({ | ||
next(data) { | ||
console.log("subscription event:", data); | ||
}, | ||
error(err) { | ||
console.log(`subscription error: ${err}`); | ||
}, | ||
complete() {}, | ||
}); | ||
|
||
await sendMessage(apolloClient, { | ||
message: "my first message", | ||
}); | ||
|
||
await sendMessage(apolloClient, { | ||
message: "my second message", | ||
}); | ||
|
||
await sendMessage(apolloClient, { | ||
message: "my third message", | ||
}); | ||
|
||
subscription.unsubscribe(); | ||
} | ||
|
||
main() | ||
.catch((error: Error) => { | ||
console.error(error); | ||
exit(1); | ||
}) | ||
.finally(async () => { | ||
await apolloServer.stop(); | ||
rsocketClient.close(); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/rsocket-examples/src/graphql/apollo/client-server/resolvers.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2021-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { PubSub } from "graphql-subscriptions"; | ||
|
||
const pubsub = new PubSub(); | ||
|
||
export const resolvers = { | ||
Query: { | ||
echo: (parent, args, context, info) => { | ||
const { message } = args; | ||
return { | ||
message, | ||
}; | ||
}, | ||
}, | ||
Mutation: { | ||
createMessage: async (_, { message }, context, info) => { | ||
await pubsub.publish("POST_CREATED", { | ||
messageCreated: { | ||
message, | ||
}, | ||
}); | ||
}, | ||
}, | ||
Subscription: { | ||
messageCreated: { | ||
// subscribe must return an AsyncIterator | ||
// https://www.apollographql.com/docs/apollo-server/data/subscriptions/#resolving-a-subscription | ||
subscribe: () => pubsub.asyncIterator(["POST_CREATED"]), | ||
}, | ||
}, | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/rsocket-examples/src/graphql/apollo/client-server/schema.graphql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Copyright 2021-2022 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
|
||
type ChatMessage { | ||
message: String | ||
} | ||
|
||
type Query { | ||
echo(message: String): ChatMessage | ||
} | ||
|
||
type Mutation { | ||
createMessage(message: String): ChatMessage | ||
} | ||
|
||
type Subscription { | ||
messageCreated: ChatMessage | ||
} |
Oops, something went wrong.