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

Migrate @storybook/addon-graphql to typescript #6935

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"license": "MIT",
"main": "dist/index.js",
"jsnext:main": "src/index.js",
"types": "dist/index.d.ts",
"scripts": {
"prepare": "node ../../scripts/prepare.js"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import React, { FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import style from './style';
import { style } from './style';

export default function FullScreen({ children }) {
export const FullScreen: FunctionComponent = ({ children }) => {
return <div style={style.wrapper}>{children}</div>;
}
};

FullScreen.defaultProps = { children: null };
FullScreen.propTypes = { children: PropTypes.node };
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
export const style = {
wrapper: {
position: 'absolute',
position: 'absolute' as const,
top: 0,
right: 0,
bottom: 0,
Expand Down
File renamed without changes.
29 changes: 7 additions & 22 deletions addons/graphql/src/manager.js → addons/graphql/src/manager.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
import { fetch } from 'global';
import React from 'react';
import React, { FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import GraphiQL from 'graphiql';
import 'graphiql/graphiql.css';

import { Consumer } from '@storybook/api';
import { Consumer, Combo } from '@storybook/api';

import { PARAM_KEY } from '.';
import { reIndentQuery, getDefaultFetcher } from './shared';

const FETCH_OPTIONS = {
method: 'post',
headers: { 'Content-Type': 'application/json' },
};

function getDefaultFetcher(url) {
return params => {
const body = JSON.stringify(params);
const options = Object.assign({ body }, FETCH_OPTIONS);
return fetch(url, options).then(res => res.json());
};
}

function reIndentQuery(query) {
const lines = query.split('\n');
const spaces = lines[lines.length - 1].length - 1;
return lines.map((l, i) => (i === 0 ? l : l.slice(spaces))).join('\n');
interface GQLProps {
active: boolean;
}

const GQL = ({ active }) => {
const GQL: FunctionComponent<GQLProps> = ({ active }) => {
return active ? (
<Consumer>
{({ api, state }) => {
{({ api, state }: Combo) => {
const story = state.storiesHash[state.storyId];
const parameters = story ? api.getParameters(story.id, PARAM_KEY) : null;

Expand Down
37 changes: 0 additions & 37 deletions addons/graphql/src/preview.js

This file was deleted.

31 changes: 31 additions & 0 deletions addons/graphql/src/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import GraphiQL from 'graphiql';

import 'graphiql/graphiql.css';

import { FullScreen } from './components/FullScreen';
import { reIndentQuery, getDefaultFetcher } from './shared';

export interface FetcherParams {
query: string;
variables?: string;
operationName?: string;
}

export interface SetupGraphiQLConfig {
url: string;
fetcher?: (params: FetcherParams) => Promise<any>;
}

export const setupGraphiQL = (config: SetupGraphiQLConfig) => (
_query: string,
variables = '{}'
) => {
const query = reIndentQuery(_query);
const fetcher = config.fetcher || getDefaultFetcher(config.url);
return () => (
<FullScreen>
<GraphiQL query={query} variables={variables} fetcher={fetcher} />
</FullScreen>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addons, types } from '@storybook/addons';
import GQL from './manager';
import { ADDON_ID } from '.';

export function register() {
export const register = () => {
addons.register(ADDON_ID, () => {
addons.add(ADDON_ID, {
title: 'GraphiQL',
Expand All @@ -13,4 +13,4 @@ export function register() {
render: GQL,
});
});
}
};
22 changes: 22 additions & 0 deletions addons/graphql/src/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { fetch } from 'global';

import { FetcherParams } from './preview';

const FETCH_OPTIONS = {
method: 'post',
headers: { 'Content-Type': 'application/json' },
};

export const getDefaultFetcher = (url: string) => {
return (params: FetcherParams) => {
const body = JSON.stringify(params);
const options = Object.assign({ body }, FETCH_OPTIONS);
return fetch(url, options).then((res: any) => res.json());
};
};

export const reIndentQuery = (query: string) => {
const lines = query.split('\n');
const spaces = lines[lines.length - 1].length - 1;
return lines.map((l, i) => (i === 0 ? l : l.slice(spaces))).join('\n');
};
7 changes: 7 additions & 0 deletions addons/graphql/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module 'global';

// There are no types for graphiql
declare module 'graphiql' {
const GraphiQL: any;
export default GraphiQL;
}
13 changes: 13 additions & 0 deletions addons/graphql/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"types": ["webpack-env"]
},
"include": [
"src/**/*"
],
"exclude": [
"src/__tests__/**/*"
]
}