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

NEBULA-1385: Add initialState to embedable sandbox based on config #6628

Merged
Merged
48 changes: 47 additions & 1 deletion docs/source/api/plugin/landing-pages.md
Expand Up @@ -106,6 +106,52 @@ By default, the landing page displays a footer that links to the documentation t
<tr>
<td>

###### `document`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these get passed through to sandbox in the regular landing page? We were just missing them before but they are supported right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup! they were always in ApolloServerPluginLandingPageDefaultBaseOptions, but just not mentioned in the docs


`string`
</td>
<td>

A GraphQL document (eg, query or mutation) to populate in the Studio Explorer's editor on load.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A GraphQL document (eg, query or mutation) to populate in the Studio Explorer's editor on load.
A GraphQL document (eg, query or mutation) to populate in the Studio Sandbox Explorer's editor on load.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want to add this? This API applies to both sandbox and explorer, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the local dev docs, so they will only ever lead to sandbox afaict

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no strong opinion tho!


If you omit this, the Explorer initially loads an example query based on your schema.

</td>
</tr>

<tr>
<td>

###### `variables`

`Record<string, string | Record>`
</td>
<td>

An object containing initial variable values to populate in the Explorer on load.

If provided, these variables should apply to the initial query you provide in `document`.

</td>
</tr>

<tr>
<td>

###### `headers`

`Record<string, string>`
</td>
<td>

An object containing initial HTTP header values to populate in the Explorer on load.

</td>
</tr>

<tr>
<td>

###### `includeCookies`

`boolean`
Expand Down Expand Up @@ -228,7 +274,7 @@ If you omit this, the Explorer initially loads an example query based on your sc

###### `variables`

`Record<string, string>`
`Record<string, string | Record>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the right hand side here should just be unknown or any? i mean you can put numbers and bools and lists in variables...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ticket was for a new config (initialState) recently added to the embedded-explorer repo. These parameters (document, variables and headers) already existed, but yeah they weren't used or passed through for embedded Sandbox.

The parameters were added and the docs were updated accordingly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this change in #6656

</td>
<td>

Expand Down
@@ -0,0 +1,158 @@
import {
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
getEmbeddedExplorerHTML,
getEmbeddedSandboxHTML,
} from '../plugin/landingPage/default/index';

const version = '_latest';

describe('Landing Page Config HTML', () => {
it('for embedded explorer with document, variables, headers and displayOptions provided', () => {
const config = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you define config as the actual options type (ApolloServerPluginEmbeddedLandingPageProductionDefaultOptions) you won't need the type casts below. Right now TS infers the type of the object (with docsPanelState and theme as string, which is not narrow enough). But if you tell it the object's type, it'll correctly give you an error if you used an incorrect string for those properties.

includeCookies: true,
document: 'query Test { id }',
variables: {
option: {
a: 'val',
b: 1,
c: true,
},
},
headers: { authorization: 'true' },
embed: {
displayOptions: {
showHeadersAndEnvVars: true,
docsPanelState: 'open' as 'open',
theme: 'light' as 'light',
},
persistExplorerState: true,
},
graphRef: 'graph@current',
};
const expected = `
<style>
iframe {
background-color: white;
}
</style>
<div
style="width: 100vw; height: 100vh; position: absolute; top: 0;"
id="embeddableExplorer"
></div>
<script src="https://embeddable-explorer.cdn.apollographql.com/_latest/embeddable-explorer.umd.production.min.js"></script>
<script>
var endpointUrl = window.location.href;
var embeddedExplorerConfig = {"includeCookies":true,"document":"query Test { id }","variables":{"option":{"a":"val","b":1,"c":true}},"headers":{"authorization":"true"},"embed":{"displayOptions":{"showHeadersAndEnvVars":true,"docsPanelState":"open","theme":"light"},"persistExplorerState":true},"graphRef":"graph@current","target":"#embeddableExplorer","initialState":{"includeCookies":true,"document":"query Test { id }","variables":{"option":{"a":"val","b":1,"c":true}},"headers":{"authorization":"true"},"embed":{"displayOptions":{"showHeadersAndEnvVars":true,"docsPanelState":"open","theme":"light"},"persistExplorerState":true},"graphRef":"graph@current","displayOptions":{"showHeadersAndEnvVars":true,"docsPanelState":"open","theme":"light"}},"persistExplorerState":true};
new window.EmbeddedExplorer({
...embeddedExplorerConfig,
endpointUrl,
});
</script>
`;
const htmlString = getEmbeddedExplorerHTML(version, config);
expect(htmlString).toEqual(expected);
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved
});

it('for embedded explorer with document, variables, headers and displayOptions excluded', () => {
const config = {
includeCookies: false,
embed: true as true,
graphRef: 'graph@current',
};
const expected = `
<style>
iframe {
background-color: white;
}
</style>
<div
style="width: 100vw; height: 100vh; position: absolute; top: 0;"
id="embeddableExplorer"
></div>
<script src="https://embeddable-explorer.cdn.apollographql.com/_latest/embeddable-explorer.umd.production.min.js"></script>
<script>
var endpointUrl = window.location.href;
var embeddedExplorerConfig = {"includeCookies":false,"embed":true,"graphRef":"graph@current","target":"#embeddableExplorer","initialState":{"includeCookies":false,"embed":true,"graphRef":"graph@current","displayOptions":{}},"persistExplorerState":false};
new window.EmbeddedExplorer({
...embeddedExplorerConfig,
endpointUrl,
});
</script>
`;
const htmlString = getEmbeddedExplorerHTML(version, config);
expect(htmlString).toEqual(expected);
});

it('for embedded sandbox with document, variables and headers provided', () => {
const config = {
includeCookies: true,
document: 'query Test { id }',
variables: {
option: {
a: 'val',
b: 1,
c: true,
},
},
headers: { authorization: 'true' },
displayOptions: {
showHeadersAndEnvVariables: true,
docsPanelState: 'open',
theme: 'light',
},
embed: true as true,
};
const expected = `
<style>
iframe {
background-color: white;
}
</style>
<div
style="width: 100vw; height: 100vh; position: absolute; top: 0;"
id="embeddableSandbox"
></div>
<script src="https://embeddable-sandbox.cdn.apollographql.com/_latest/embeddable-sandbox.umd.production.min.js"></script>
<script>
var initialEndpoint = window.location.href;
new window.EmbeddedSandbox({
target: '#embeddableSandbox',
initialEndpoint,
includeCookies: true,
initialState: {"document":"query Test { id }","variables":{"option":{"a":"val","b":1,"c":true}},"headers":{"authorization":"true"}},
});
</script>
`;
const htmlString = getEmbeddedSandboxHTML(version, config);
expect(htmlString).toEqual(expected);
});

it('for embedded sandbox with document, variables and headers excluded', () => {
const config = {
includeCookies: false,
embed: true as true,
};
const expected = `
<style>
iframe {
background-color: white;
}
</style>
<div
style="width: 100vw; height: 100vh; position: absolute; top: 0;"
id="embeddableSandbox"
></div>
<script src="https://embeddable-sandbox.cdn.apollographql.com/_latest/embeddable-sandbox.umd.production.min.js"></script>
<script>
var initialEndpoint = window.location.href;
new window.EmbeddedSandbox({
target: '#embeddableSandbox',
initialEndpoint,
includeCookies: false,
initialState: {},
});
</script>
`;
const htmlString = getEmbeddedSandboxHTML(version, config);
expect(htmlString).toEqual(expected);
});
});
Expand Up @@ -57,7 +57,7 @@ function getConfigStringForHtml(config: LandingPageConfig) {
.replace("'", '\\u0027');
}

const getEmbeddedExplorerHTML = (
export const getEmbeddedExplorerHTML = (
version: string,
config: ApolloServerPluginEmbeddedLandingPageProductionDefaultOptions,
) => {
Expand Down Expand Up @@ -124,7 +124,10 @@ id="embeddableExplorer"
`;
};

const getEmbeddedSandboxHTML = (version: string, config: LandingPageConfig) => {
export const getEmbeddedSandboxHTML = (
version: string,
config: LandingPageConfig,
) => {
return `
<style>
iframe {
Expand All @@ -142,6 +145,11 @@ id="embeddableSandbox"
target: '#embeddableSandbox',
initialEndpoint,
includeCookies: ${config.includeCookies ?? 'false'},
initialState: ${getConfigStringForHtml({
document: config.document ?? undefined,
variables: config.variables ?? undefined,
headers: config.headers ?? undefined,
})},
});
</script>
`;
Expand Down
Expand Up @@ -21,7 +21,7 @@ export interface ApolloServerPluginLandingPageDefaultBaseOptions {
* Users can configure their landing page to link to Studio Explorer with
* variables loaded in the UI.
*/
variables?: Record<string, string>;
variables?: Record<string, any>;
/**
* Users can configure their landing page to link to Studio Explorer with
* headers loaded in the UI.
Expand Down