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

feat: apollo graphql link and server #225

Merged
merged 25 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
380237c
feat: apollo graphl link and server
viglucci Mar 25, 2022
9a81261
fix/custom metadata header alloc (#231)
viglucci Jun 18, 2022
ea8a790
chore(release): release
viglucci Jun 18, 2022
b6d2392
test: add encodeWellKnownMetadataHeader test
viglucci Jun 19, 2022
07e3dc3
test: add encodeAndAddWellKnownMetadata tests
viglucci Jun 19, 2022
f32c338
test: add encodeCompositeMetadata tests
viglucci Jun 19, 2022
e05b933
Optional metadata for rxjs requesters (#241)
viglucci Aug 9, 2022
9b4bd5e
chore(release): release
viglucci Aug 9, 2022
81ff1fc
chore: expand rxjs example to leverage fire and forget
viglucci Aug 10, 2022
04d3c40
refactor: rename rxjs messaging composite metadata routing example
viglucci Aug 10, 2022
9715523
chore: add rxjs example without rsocket-messaging
viglucci Aug 10, 2022
995a664
fixes request channel to drop second element (#244)
OlegDokuka Aug 11, 2022
1326c62
chore(release): release
viglucci Aug 13, 2022
d7bb535
docs: update release docs with example lerna command
viglucci Aug 13, 2022
2bba130
feat: add RSocketApolloGraphlQLPlugin (apollographql/apollo-server#6264)
viglucci Sep 4, 2022
871946d
fix: remove lerna-debug.log
viglucci Sep 4, 2022
d28ffd4
chore(release): release
viglucci Jun 18, 2022
bd1a27a
Optional metadata for rxjs requesters (#241)
viglucci Aug 9, 2022
7feed29
refactor: rename QueryLink to RSocketQueryLink
viglucci Sep 12, 2022
c377d6a
refactor: rename SubscriptionLink to RSocketSubscriptionLink
viglucci Sep 12, 2022
a0fb7ba
chore: add simple webpack example (#246)
viglucci Sep 10, 2022
03439d8
fix: merge conflict missing comma in package.json
viglucci Apr 27, 2023
b7b9149
Merge branch '1.0.x-alpha' into feature/apollo-graphql-link-server
viglucci Apr 27, 2023
5ae8115
chore: expand gql client example to leverage subscription
viglucci Apr 27, 2023
1982a3d
chore: run linter
viglucci Apr 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 6 additions & 1 deletion packages/rsocket-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
"start-client-server-request-response-websocket": "ts-node -r tsconfig-paths/register src/ClientServerRequestResponseExampleWebSocket.ts",
"start-client-server-composite-metadata-route": "ts-node -r tsconfig-paths/register src/ClientServerCompositeMetadataRouteExample.ts",
"start-client-server-rxjs-messaging-composite-metadata-route": "ts-node -r tsconfig-paths/register src/rxjs/RxjsMessagingCompositeMetadataRouteExample.ts",
"start-client-server-rxjs-requester-responder": "ts-node -r tsconfig-paths/register src/rxjs/RxjsRequesterResponderExample.ts"
"start-client-server-rxjs-requester-responder": "ts-node -r tsconfig-paths/register src/rxjs/RxjsRequesterResponderExample.ts",
"start-client-apollo-graphql": "ts-node -r tsconfig-paths/register src/graphql/apollo/client/example.ts",
"start-client-server-apollo-graphql": "ts-node -r tsconfig-paths/register src/graphql/apollo/client-server/example.ts"
},
"dependencies": {
"@apollo/client": "^3.5.10",
"graphql-tag": "^2.12.6",
"graphql-subscriptions": "^2.0.0",
"rsocket-adapter-rxjs": "^1.0.0-alpha.4",
"rsocket-composite-metadata": "^1.0.0-alpha.3",
"rsocket-core": "^1.0.0-alpha.3",
Expand Down
175 changes: 175 additions & 0 deletions packages/rsocket-examples/src/graphql/apollo/client-server/example.ts
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();
});
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"]),
},
},
};
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
}
Loading