Skip to content

Commit

Permalink
🐛 🩹 send operation ids for introspection requests 🚨 wait for nebula-2…
Browse files Browse the repository at this point in the history
…002 before merge🚨 (#202)

* assume studio will always send us operationId & pass it through to the response. THIS COMMIT SHOULD BE MERGED AFTER STUDIO PR HAS BEEN OUT FOR ~1 week ish

* now that we have no service workers, we can assume studio is sending up only endpointUrl, not sandboxEndpointUrl. Also, updateSchemaInEmbed is not called in EmbeddedSandbox, so delete it
  • Loading branch information
mayakoneval authored Jan 18, 2023
1 parent 8308db5 commit 1f60f54
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
27 changes: 2 additions & 25 deletions packages/sandbox/src/EmbeddedSandbox.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import type { IntrospectionQuery } from 'graphql';
import {
EMBEDDABLE_SANDBOX_URL,
IFRAME_DOM_ID,
SCHEMA_RESPONSE,
} from './helpers/constants';
import { EMBEDDABLE_SANDBOX_URL, IFRAME_DOM_ID } from './helpers/constants';
import { defaultHandleRequest } from './helpers/defaultHandleRequest';
import {
HandleRequest,
sendPostMessageToEmbed,
} from './helpers/postMessageRelayHelpers';
import type { HandleRequest } from './helpers/postMessageRelayHelpers';
import { setupSandboxEmbedRelay } from './setupSandboxEmbedRelay';
import packageJSON from '../package.json';
import type { JSONObject } from './helpers/types';
Expand Down Expand Up @@ -150,19 +142,4 @@ export class EmbeddedSandbox {
throw new Error('"target" is required');
}
}

updateSchemaInEmbed({
schema,
}: {
schema?: string | IntrospectionQuery | undefined;
}) {
sendPostMessageToEmbed({
message: {
name: SCHEMA_RESPONSE,
schema,
},
embeddedIFrameElement: this.embeddedSandboxIFrameElement,
embedUrl: EMBEDDABLE_SANDBOX_URL(this.__testLocal__),
});
}
}
13 changes: 9 additions & 4 deletions packages/sandbox/src/helpers/postMessageRelayHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ export type OutgoingEmbedMessage =
name: typeof SCHEMA_ERROR;
error?: string;
errors?: Array<GraphQLError>;
operationId: string;
}
| {
name: typeof SCHEMA_RESPONSE;
schema: IntrospectionQuery | string | undefined;
operationId: string;
}
| {
name: typeof HANDSHAKE_RESPONSE;
Expand Down Expand Up @@ -150,10 +152,7 @@ export type IncomingEmbedMessage =
operationId: string;
variables?: Record<string, string>;
headers?: Record<string, string>;
// This should be deleted fall 2022. Studio has been updated to only send
// endpointUrl, but we kept this around for service workers
sandboxEndpointUrl?: string;
endpointUrl?: string;
endpointUrl: string;
}>
| MessageEvent<{
name: typeof EXPLORER_SUBSCRIPTION_REQUEST;
Expand Down Expand Up @@ -190,6 +189,7 @@ export type IncomingEmbedMessage =
introspectionRequestBody: string;
introspectionRequestHeaders: Record<string, string>;
sandboxEndpointUrl?: string;
operationId: string;
}>;

export function executeOperation({
Expand Down Expand Up @@ -355,13 +355,15 @@ export function executeIntrospectionRequest({
embeddedIFrameElement,
embedUrl,
handleRequest,
operationId,
}: {
endpointUrl: string;
embeddedIFrameElement: HTMLIFrameElement;
headers?: Record<string, string>;
introspectionRequestBody: string;
embedUrl: string;
handleRequest: HandleRequest;
operationId: string;
}) {
const { query, operationName } = JSON.parse(introspectionRequestBody) as {
query: string;
Expand All @@ -382,6 +384,7 @@ export function executeIntrospectionRequest({
message: {
name: SCHEMA_ERROR,
errors: response.errors,
operationId,
},
embeddedIFrameElement,
embedUrl,
Expand All @@ -391,6 +394,7 @@ export function executeIntrospectionRequest({
message: {
name: SCHEMA_RESPONSE,
schema: response.data,
operationId,
},
embeddedIFrameElement,
embedUrl,
Expand All @@ -401,6 +405,7 @@ export function executeIntrospectionRequest({
message: {
name: SCHEMA_ERROR,
error: error,
operationId,
},
embeddedIFrameElement,
embedUrl,
Expand Down
15 changes: 5 additions & 10 deletions packages/sandbox/src/setupSandboxEmbedRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function setupSandboxEmbedRelay({
introspectionRequestBody,
introspectionRequestHeaders,
sandboxEndpointUrl,
operationId,
} = data;
if (sandboxEndpointUrl) {
executeIntrospectionRequest({
Expand All @@ -65,6 +66,7 @@ export function setupSandboxEmbedRelay({
embeddedIFrameElement: embeddedSandboxIFrameElement,
embedUrl,
handleRequest,
operationId,
});
}
}
Expand All @@ -84,21 +86,14 @@ export function setupSandboxEmbedRelay({
const { operation, operationId, operationName, variables, headers } =
data;
if (isQueryOrMutation) {
const {
endpointUrl,
// this can be deleted in Fall 2022
// it is just here to be backwards compatible with old
// studio versions (service workers)
sandboxEndpointUrl,
} = data;
const endpointUrlToUseInExecution = endpointUrl ?? sandboxEndpointUrl;
if (!endpointUrlToUseInExecution) {
const { endpointUrl } = data;
if (!endpointUrl) {
throw new Error(
'Something went wrong, we should not have gotten here. The sandbox endpoint url was not sent.'
);
}
executeOperation({
endpointUrl: endpointUrlToUseInExecution,
endpointUrl,
handleRequest,
operation,
operationName,
Expand Down

0 comments on commit 1f60f54

Please sign in to comment.