Skip to content

Commit

Permalink
Correct object handling in altair-static render function
Browse files Browse the repository at this point in the history
  • Loading branch information
cody committed Sep 3, 2020
1 parent 6a07424 commit 3d87caf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/altair-static/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ exports[`renderAltair should return expected string 1`] = `
};
AltairGraphQL.init(altairOpts);
</script></body>
Expand All @@ -68,9 +69,10 @@ exports[`renderInitialOptions should return expected string 1`] = `
}\`,
initialHeaders: {\\"X-GraphQL-Token\\":\\"asd7-237s-2bdk-nsdk4\\"},
initialSettings: {\\"theme\\":\\"dark\\"},
};
AltairGraphQL.init(altairOpts);
"
Expand Down
8 changes: 7 additions & 1 deletion packages/altair-static/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ describe('renderInitialOptions', () => {
initialQuery: `query {
Hello
}`,
endpointURL: 'https://example.com/graphql'
endpointURL: 'https://example.com/graphql',
initialHeaders: {
'X-GraphQL-Token': 'asd7-237s-2bdk-nsdk4'
},
initialSettings: {
theme: 'dark'
}
});
expect(result).toMatchSnapshot();
});
Expand Down
17 changes: 11 additions & 6 deletions packages/altair-static/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface RenderOptions {
* 'X-GraphQL-Token': 'asd7-237s-2bdk-nsdk4'
* }
*/
initialHeaders?: Object;
initialHeaders?: {[key: string]: string};

/**
* Whether to render the initial options in a seperate javascript file or not.
Expand Down Expand Up @@ -86,8 +86,12 @@ export interface RenderOptions {

/**
* Initial app settings to use
* @example
* {
* theme: 'dark'
* }
*/
initialSettings?: any;
initialSettings?: {[key: string]: any};
}

/**
Expand All @@ -102,7 +106,8 @@ export const renderInitialOptions = ({
initialHeaders,
initialPreRequestScript,
initialEnvironments,
instanceStorageNamespace
instanceStorageNamespace,
initialSettings
}: RenderOptions = {}) => {
return `
const altairOpts = {
Expand All @@ -114,6 +119,7 @@ export const renderInitialOptions = ({
${getObjectPropertyForOption(initialHeaders, 'initialHeaders')}
${getObjectPropertyForOption(initialEnvironments, 'initialEnvironments')}
${getObjectPropertyForOption(instanceStorageNamespace, 'instanceStorageNamespace')}
${getObjectPropertyForOption(initialSettings, 'initialSettings')}
};
AltairGraphQL.init(altairOpts);
`;
Expand All @@ -140,12 +146,11 @@ export const renderAltair = (options: RenderOptions = {}) => {

function getObjectPropertyForOption(option: any, propertyName: string) {
if (option) {
let optionString = option;
switch (typeof option) {
case 'object':
optionString = JSON.stringify(option);
return `${propertyName}: ${JSON.stringify(option)},`;
}
return `${propertyName}: \`${optionString}\`,`;
return `${propertyName}: \`${option}\`,`;
}
return '';
}
Expand Down

0 comments on commit 3d87caf

Please sign in to comment.