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

Add defaultQuery prop #6

Merged
merged 1 commit into from
Sep 22, 2022
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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@edgeandnode/graphiql-playground",
"version": "0.1.1",
"version": "0.1.3",
"type": "module",
"main": "./dist/index.cjs.js",
"module": "./dist/index.es.js",
Expand Down
77 changes: 32 additions & 45 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import { useExplorerPlugin } from "@graphiql/plugin-explorer";
import { GraphiQLProvider } from "@graphiql/react";
import {
type Storage as GraphiQLStorage,
CreateFetcherOptions,
createGraphiQLFetcher,
} from "@graphiql/toolkit";
import { ReactNode, useEffect, useState } from "react";
import { useExplorerPlugin } from '@graphiql/plugin-explorer'
import { GraphiQLProvider } from '@graphiql/react'
import { type Storage as GraphiQLStorage, CreateFetcherOptions, createGraphiQLFetcher } from '@graphiql/toolkit'
import { ReactNode, useEffect, useState } from 'react'

import { SavedQuery } from "./SavedQueriesToolbar/types";
import { GraphiQLInterface, GraphiQLToolbar } from "./GraphiQLInterface";
import {
SavedQueriesContext,
SavedQueriesContextProvider,
SavedQueriesToolbar,
} from "./SavedQueriesToolbar";
import { SavedQuery } from './SavedQueriesToolbar/types'
import { GraphiQLInterface, GraphiQLToolbar } from './GraphiQLInterface'
import { SavedQueriesContext, SavedQueriesContextProvider, SavedQueriesToolbar } from './SavedQueriesToolbar'

import "@graphiql/react/font/fira-code.css";
import "@graphiql/plugin-explorer/dist/style.css";
import '@graphiql/react/font/fira-code.css'
import '@graphiql/plugin-explorer/dist/style.css'
//
// TODO: Should those two be merged?
import "./graphiql-styles.css";
import "./style-overrides.css";
import "./graphiql-react-properties.css";
import "./syntax-highlighting.css";
import './graphiql-styles.css'
import './style-overrides.css'
import './graphiql-react-properties.css'
import './syntax-highlighting.css'

// Temporary?
const TOOLBAR_HIDDEN = (
Expand All @@ -31,17 +23,19 @@ const TOOLBAR_HIDDEN = (
{/* Toolbar button tooltips are currently broken because of our global styles. */}
{/* Toolbar actions keyboard shortcuts don't work. */}
</GraphiQLToolbar>
);
)

/**
* @see https://graphiql-test.netlify.app/typedoc/modules/graphiql.html#graphiqlprops
*/
export interface GraphProtocolGraphiQLProps<TQuery extends SavedQuery>
extends Pick<SavedQueriesContext<TQuery>, "queries" | "currentQueryId"> {
fetcher: GraphProtocolGraphiQL.FetcherOptions;
storage?: GraphiQLStorage;
extends Pick<SavedQueriesContext<TQuery>, 'queries' | 'currentQueryId'> {
fetcher: GraphProtocolGraphiQL.FetcherOptions
storage?: GraphiQLStorage
/** used for initial state if ther eare no savedQueries */
defaultQuery?: string
/** slot for GraphProtocolGraphiQL.SavedQueriesToolbar */
header: ReactNode;
header: ReactNode
}

export function GraphProtocolGraphiQL<TQuery extends SavedQuery>({
Expand All @@ -50,35 +44,28 @@ export function GraphProtocolGraphiQL<TQuery extends SavedQuery>({
header,
currentQueryId,
queries,
defaultQuery = '',
}: GraphProtocolGraphiQLProps<TQuery>) {
const [fetcher] = useState(() => createGraphiQLFetcher(fetcherOptions));
const currentSavedQuery = queries.find(
(query) => query.id === currentQueryId
);
const [fetcher] = useState(() => createGraphiQLFetcher(fetcherOptions))
const currentSavedQuery = queries.find((query) => query.id === currentQueryId)

const [querySource, setQuerySource] = useState(
currentSavedQuery?.query || ""
);
const [querySource, setQuerySource] = useState(currentSavedQuery?.query || defaultQuery)

// Whenever currentQueryId changes, we update the text in CodeMirror.
useEffect(() => {
setQuerySource(currentSavedQuery?.query || "");
if (currentQueryId) setQuerySource(currentSavedQuery?.query || '')
// We don't refresh the editor on changes to currentSavedQuery.query,
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentQueryId]);
}, [currentQueryId])

const explorerPlugin = useExplorerPlugin({
query: querySource,
onEdit: setQuerySource,
storage,
});
})

return (
<GraphiQLProvider
fetcher={fetcher}
query={querySource}
storage={storage}
plugins={[explorerPlugin]}
>
<GraphiQLProvider fetcher={fetcher} query={querySource} storage={storage} plugins={[explorerPlugin]}>
<SavedQueriesContextProvider<TQuery>
value={{
currentQueryId,
Expand All @@ -98,14 +85,14 @@ export function GraphProtocolGraphiQL<TQuery extends SavedQuery>({
</GraphiQLInterface>
</SavedQueriesContextProvider>
</GraphiQLProvider>
);
)
}

GraphProtocolGraphiQL.SavedQueriesToolbar = SavedQueriesToolbar;
GraphProtocolGraphiQL.SavedQueriesToolbar = SavedQueriesToolbar

export declare namespace GraphProtocolGraphiQL {
export interface FetcherOptions extends CreateFetcherOptions {}
export interface Storage extends GraphiQLStorage {}
}

export * from "./SavedQueriesToolbar/types";
export * from './SavedQueriesToolbar/types'