Skip to content

Commit

Permalink
feat(CodeBlock): Pass variables into graphqlPlayground url (#362)
Browse files Browse the repository at this point in the history
* WIP

* Cleanup code

* Convert to json from jsonc

* Rename var

* Cleanup code
  • Loading branch information
tadhglewis authored Dec 8, 2021
1 parent 27c1039 commit 00ed511
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"clsx": "^1.1.1",
"find-up": "^5.0.0",
"fs-extra": "^10.0.0",
"jsonc-parser": "^3.0.0",
"polished": "^4.1.3",
"prism-react-renderer": "1.2.1",
"react-keyed-flatten-children": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/CodeBlock.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CodeBlock as Component } from './CodeBlock';
export default {
args: {
braidThemeName: defaultArgs.braidThemeName,
graphqlPlayground: 'https://graphql.seek.com/graphql',
graphqlPlayground: 'https://manage.developer.seek.com/graphql-explorer',
language: 'graphql',
size: defaultArgs.size,
trim: true,
Expand Down
17 changes: 13 additions & 4 deletions src/components/CodeBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Box, Stack, Text, TextLinkButton } from 'braid-design-system';
import { parse } from 'jsonc-parser';
import Highlight from 'prism-react-renderer';
import React, { useState } from 'react';

Expand Down Expand Up @@ -56,15 +57,23 @@ export const CodeBlock = ({

const child = children[index];

const jsoncVariables =
children[0].language === 'graphql' && children[1]?.label === 'Variables'
? children[1].code
: undefined;

const variables =
jsoncVariables && JSON.stringify(parse(jsoncVariables), null, 2);

const graphqlPlaygroundButton =
child.language === 'graphql' && graphqlPlayground ? (
children[0].language === 'graphql' && graphqlPlayground ? (
<Box component="span" paddingLeft={tablePadding}>
<GraphQLPlaygroundAction
graphqlPlayground={graphqlPlayground}
size={size}
>
{child.code}
</GraphQLPlaygroundAction>
query={children[0].code}
variables={variables}
/>
</Box>
) : undefined;

Expand Down
11 changes: 7 additions & 4 deletions src/components/CodeBlock/GraphQLPlaygroundAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ import { SIZE_TO_SMALLER, Size } from '../../private/size';
const URL = url.URL ?? window.URL;

interface Props {
children: string;
query: string;
variables: string | undefined;
graphqlPlayground: string;
size: Size;
}

export const GraphQLPlaygroundAction = ({
children,
query,
variables,
graphqlPlayground,
size,
}: Props) => {
const playgroundUrl = new URL(graphqlPlayground);
playgroundUrl.searchParams.set('query', children);
playgroundUrl.searchParams.set('query', query);
playgroundUrl.searchParams.set('variables', variables ?? '{}');
const href = playgroundUrl.toString();

const smallerSize = SIZE_TO_SMALLER[size];

return (
<Text size={smallerSize} weight="medium">
<TextLink href={href} rel="noreferrer" target="_blank">
<IconVideo alignY="lowercase" /> Playground
<IconVideo alignY="lowercase" /> GraphQL Explorer
</TextLink>
</Text>
);
Expand Down

0 comments on commit 00ed511

Please sign in to comment.