Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created September 10, 2020 20:34
Show Gist options
  • Select an option

  • Save jasonLaster/fb8a65563f1a48858cc8a5c0b535df8a to your computer and use it in GitHub Desktop.

Select an option

Save jasonLaster/fb8a65563f1a48858cc8a5c0b535df8a to your computer and use it in GitHub Desktop.
diff --git a/src/dispatch/apollo.js b/src/dispatch/apollo.js
new file mode 100644
index 0000000..9237d70
--- /dev/null
+++ b/src/dispatch/apollo.js
@@ -0,0 +1,48 @@
+const { gql, ApolloClient, InMemoryCache } = require("@apollo/client");
+const { HttpLink } = require("apollo-link-http");
+
+const createHttpLink = (headers) => {
+ const httpLink = new HttpLink({
+ uri: "http://graphql.replay.io/v1/graphql",
+ headers: {
+ ...headers,
+ // "x-hasura-admin-secret": "supersecret",
+ }, // auth token is fetched on the server side
+ fetch,
+ });
+ return httpLink;
+};
+
+export const createApolloClient = () => {
+ const link = createHttpLink({});
+
+ const client = new ApolloClient({
+ link,
+ cache: new InMemoryCache(),
+ });
+
+ client
+ .query({
+ query: gql`
+ query MyRecordingsQuery($user_id: String) {
+ recordings(
+ where: { user: { auth_id: { _eq: $user_id } } }
+ order_by: { date: asc }
+ ) {
+ title
+ id
+ recording_id
+ user {
+ auth_id
+ }
+ url
+ }
+ }
+ `,
+ options: {
+ user_id: "google-oauth2|104244849821918151681",
+ },
+ })
+ .then((result) => console.log(result));
+ return client;
+};
diff --git a/src/dispatch/main.js b/src/dispatch/main.js
index f881888..1054212 100644
--- a/src/dispatch/main.js
+++ b/src/dispatch/main.js
@@ -19,9 +19,7 @@ const {
} = require("../shared/config");
const { ProtocolSocket } = require("../protocol/socket");
const { loadTypes } = require("../protocol/typeCheck");
-const {
- log,
-} = require("../shared/utils");
+const { log } = require("../shared/utils");
const {
AnalysisMessageHandlers,
analysisOnProtocolSocketClose,
@@ -40,7 +38,7 @@ const {
metadataOnProtocolSocketClose,
} = require("./metadata");
-process.on("unhandledRejection", error => {
+process.on("unhandledRejection", (error) => {
console.error("ErrorUnhandledRejection", error);
});
@@ -54,7 +52,41 @@ log("Dispatcher initializing.");
const server = http.createServer();
const wss = new WebSocket.Server({ server });
-wss.on("connection", socket => {
+
+server.post("/create-recording", () => {
+ const { query, variables } = data;
+ // https://github.com/RecordReplay/recordings/blob/master/pages/api/create-recording.ts
+ const ADD_RECORDING = gql`
+ mutation CreateRecording($data: recordings_insert_input!) {
+ insert_recordings_one(object: $data) {
+ id
+ recording_id
+ last_screen_data
+ last_screen_mime_type
+ duration
+ title
+ url
+ user_id
+ }
+ }
+ `;
+ try {
+ const data = await client.mutate({
+ variables: {
+ data: req.body,
+ },
+ mutation: ADD_RECORDING,
+ });
+ res.statusCode = 200;
+ res.setHeader('Content-Type', 'application/json');
+ res.end(JSON.stringify(data.data.insert_recordings_one));
+ } catch (e) {
+
+ }
+
+});
+
+wss.on("connection", (socket) => {
const id = uuid();
log(`NewConnection ${id}`);
@@ -81,10 +113,19 @@ const MessageHandlers = {
...AnalysisMessageHandlers,
...MetadataMessageHandlers,
- "Internal.echo": async function({ str, count }) {
+ "Internal.echo": async function ({ str, count }) {
const newStr = str.repeat(count);
return { str: newStr };
},
+
+ // "Recording.authenticate": async function({}) {
+ // const {recordingId, authId} = data;
+ // apolloClient.query(``)
+ // }
+ "Recording.query": async function ({}) {
+ const { query, variables } = data;
+ return apolloClient.query(query, variables);
+ },
};
function monitorMemoryUsage() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment