|
| 1 | +# Upgrading `graphiql` from `1.x` to `2.0.0` |
| 2 | + |
| 3 | +Hello GraphiQL user and thanks for upgrading! |
| 4 | + |
| 5 | +This migration guide walks you through all changes that come with `[email protected]`, in particular the breaking ones, and will show you how to upgrade your `1.x` implementation. |
| 6 | + |
| 7 | +> If you encounter any issues while upgrading that are not covered in here, please open an issue or PR on this repo and we will extend this guide. |
| 8 | +
|
| 9 | +## Design refresh including dark theme |
| 10 | + |
| 11 | +Arguably the biggest change in `graphiql@2` is the new design of the UI. It has been reworked from scratch to look more modern while keeping its simplistic look and feel. We also finally added a built-in dark theme. Theme selection is based on system defaults and can be changed in the new settings dialog (available by clicking on the gear icon at the bottom of the sidebar on the left of the screen). |
| 12 | + |
| 13 | +Starting with `graphiql@2`, the only officially supported way of customizing the CSS that make up the looks of GraphiQL is by overriding the design tokens defined using CSS variables. In particular, changes to class names are no longer considered breaking changes. If you use class-name based selectors to change styles your overrides might break with minor or patch version bumps. |
| 14 | + |
| 15 | +A list of all CSS variables that can be customized can be found in the [`root.css`](../../packages/graphiql-react/src/style/root.css) file of the `@graphiql/react` package. The variables for colors use a list of values that can be passed into the [`hsl`](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl) function in CSS that defines colors by hue, saturation and lightness. |
| 16 | + |
| 17 | +## Changes to `GraphiQL` component props |
| 18 | + |
| 19 | +A couple of props of the `GraphiQL` have undergone breaking changes: |
| 20 | + |
| 21 | +- The props `defaultVariableEditorOpen` and `defaultSecondaryEditorOpen` have been merged into one prop `defaultEditorToolsVisibility`. The default behavior if this prop is not passed is that the editor tools are shown if at least one of the secondary editors has contents. You can pass the following values to the prop: |
| 22 | + - Passing `false` hides the editor tools. |
| 23 | + - Passing `true` shows the editor tools. |
| 24 | + - Passing `"variables"` explicitly shows the variables editor. |
| 25 | + - Passing `"headers"` explicitly shows the headers editor. |
| 26 | +- The props `docExplorerOpen`, `onToggleDocs` and `onToggleHistory` have been removed. They are replaced by the more generic props `visiblePlugin` (for controlling which plugin is visible) and `onTogglePluginVisibility` (which is called each time the visibility of any plugin changes). |
| 27 | +- The `headerEditorEnabled` prop has been renamed to `isHeadersEditorEnabled`. |
| 28 | +- The `ResultsTooltip` prop has been renamed to `responseTooltip`. |
| 29 | + |
| 30 | +### Tabs enabled by default |
| 31 | + |
| 32 | +Tabs were supported opt-in starting with `@[email protected]`. With `graphiql@2` tabs are now always enabled. The `tabs` prop (which previously toggled if tabs were enabled or not) has therefore been replaced with a prop `onTabChange`. If you used the `tabs` prop before to pass this function you can change your implementation like so: |
| 33 | + |
| 34 | +```diff |
| 35 | +<GraphiQL |
| 36 | +- tabs={{ onTabChange: (tabState) => {/* do something */} }} |
| 37 | ++ onTabChange={(tabState) => {/* do something */}} |
| 38 | +/> |
| 39 | +``` |
| 40 | + |
| 41 | +As long as only one session is open, the tab bar above the editors is hidden. A plus icon next to the logo on the top right allows the user to open more tabs. With at least two tabs opened, the tab bar appears above the editors. |
| 42 | + |
| 43 | +## Removed package exports |
| 44 | + |
| 45 | +All React components apart from the `GraphiQL` component have been moved to the `@graphiql/react` package. That's why we removed most of the exports with `graphiql@2`. Here is a list of all exported components and functions that have been removed and where you can find them now: |
| 46 | + |
| 47 | +- `QueryEditor`, `VariableEditor` and `DocExplorer`: Now exported from `@graphiql/react` under the same names |
| 48 | + - Note that the `schema` prop of the `DocExplorer` no longer exists, the component now uses the schema provided by the `ExplorerContext`. |
| 49 | +- `ToolbarMenu`: Now exported from `@graphiql/react` as `ToolbarMenu` |
| 50 | +- `ToolbarMenuItem`: Now exported from `@graphiql/react` as `ToolbarMenu.Item` |
| 51 | +- `ToolbarSelect`: Now exported from `@graphiql/react` as `ToolbarListbox` |
| 52 | +- `ToolbarSelectOption`: Now exported from `@graphiql/react` as `ToolbarListbox.Option` |
| 53 | +- `onHasCompletion`: This function is only meant to be used internally, it is no longer being exported |
| 54 | +- `fillLeafs`, `getSelectedOperationName` and `mergeAst`: Now exported from `@graphiql/toolkit` under the same names |
| 55 | +- types `Fetcher`, `FetcherOpts`, `FetcherParams`, `FetcherResult`, `FetcherReturnType`, `Observable`, `Storage` and `SyncFetcherResult`: Exported from `@graphiql/toolkit` under the same names (previously just re-exported by `graphiql`) |
| 56 | + |
| 57 | +## `GraphiQL` is now a function component |
| 58 | + |
| 59 | +The `GraphiQL` component in `[email protected]` was a class component. That allowed easy access to its props, state and methods by attaching a ref to it like so: |
| 60 | + |
| 61 | +```jsx |
| 62 | +import { createGraphiQLFetcher } from '@graphiql/toolkit'; |
| 63 | +import { GraphiQL } from 'graphiql'; |
| 64 | +import React from 'react'; |
| 65 | + |
| 66 | +const fetcher = createGraphiQLFetcher({ url: 'https://my.endpoint' }); |
| 67 | + |
| 68 | +class MyComponent extends React.Component { |
| 69 | + _graphiql: GraphiQL; |
| 70 | + |
| 71 | + componentDidMount() { |
| 72 | + const query = this._graphiql.getQueryEditor().getValue(); |
| 73 | + } |
| 74 | + |
| 75 | + render() { |
| 76 | + return <GraphiQL ref={r => (this._graphiql = r)} fetcher={fetcher} />; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +With `graphiql@2` we refactored the codebase to more "modern" React. This also meant replacing all class components with function components. The code above no longer works in `graphiql@2` as attaching refs to function components is not possible in React. |
| 82 | + |
| 83 | +All logic and state management now lives in multiple React contexts, provided by the `@graphiql/react` package. The `GraphiQL` component is now basically combining two other components, both of which are also exported by the package. |
| 84 | + |
| 85 | +- `GraphiQLProvider` (originally coming from `@graphiql/react`) will render all context providers and takes care of state management |
| 86 | +- `GraphiQLInterface` is defined in the `graphiql` package and renders the UI |
| 87 | + |
| 88 | +If you want to read or modify GraphiQL state from your custom implementation you need to render both the above components separately as the hooks for consuming the context values only work in components that are rendered inside the provider component. |
| 89 | + |
| 90 | +With all that, the example above can be refactored a such: |
| 91 | + |
| 92 | +```jsx |
| 93 | +import { useEditorContext } from '@graphiql/react'; |
| 94 | +import { createGraphiQLFetcher } from '@graphiql/toolkit'; |
| 95 | +import { GraphiQLInterface, GraphiQLProvider } from 'graphiql'; |
| 96 | +import { useEffect } from 'react'; |
| 97 | + |
| 98 | +const fetcher = createGraphiQLFetcher({ url: 'https://my.endpoint' }); |
| 99 | + |
| 100 | +function MyComponent() { |
| 101 | + return ( |
| 102 | + <GraphiQLProvider fetcher={fetcher}> |
| 103 | + <InsideContext /> |
| 104 | + </GraphiQLProvider> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +function InsideContext() { |
| 109 | + // Calling this hook would not work in `MyComponent` (it would return `null`) |
| 110 | + const { queryEditor } = useEditorContext(); |
| 111 | + |
| 112 | + useEffect(() => { |
| 113 | + const query = queryEditor.getValue(); |
| 114 | + }, []); |
| 115 | + |
| 116 | + return <GraphiQLInterface />; |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +Here is a list of all public class methods that existed in `graphiql@1` and its replacement in `graphiql@2`. All the contexts mentioned below can be accessed using a hook exported by `@graphiql/react`. |
| 121 | + |
| 122 | +- `getQueryEditor`: Use the `queryEditor` property from the `EditorContext`. |
| 123 | +- `getVariableEditor`: Use the `variableEditor` property from the `EditorContext`. |
| 124 | +- `getHeaderEditor`: Use the `headerEditor` property from the `EditorContext`. |
| 125 | +- `refresh`: Calling this method should no longer be necessary, all editors will refresh automatically after resizing. If you really need to refresh manually you have to call the `refresh` method on all editor instances individually. |
| 126 | +- `autoCompleteLeafs`: Use the `useAutoCompleteLeafs` hook provided by `@graphiql/react` that returns this function. |
| 127 | + |
| 128 | +There are a couple more class methods that were intended to be private and were already removed starting in `[email protected]`. Since they were not actually marked with `private`, here's an extension to the above list for these methods: |
| 129 | + |
| 130 | +- `handleClickReference`: This was a callback method triggered when clicking on a type or field. It would open the doc explorer for the clicked reference. If you want to manually mimic this behavior you can use the `push` method from the `ExplorerContext` to add an item to the navigation stack of the doc explorer, and you can use the `setVisiblePlugin` method of the `PluginContext` to show the doc explorer plugin (by passing the `DOC_EXPLORER_PLUGIN` object provided by `@graphiql/react`). |
| 131 | +- `handleRunQuery`: To execute a query, use the `run` method of the `ExecutionContext`. If you want to explicitly set an operation name, call the `setOperationName` method of the `EditorContext` provider before that (passing in the operation name string as argument). |
| 132 | +- `handleEditorRunQuery`: Use the `run` method of the `ExecutionContext`. |
| 133 | +- `handleStopQuery`: Use the `stop` method from the `ExecutionContext`. |
| 134 | +- `handlePrettifyQuery`: Use the `usePrettifyQuery` hook provided by `@graphiql/react` that returns this function. |
| 135 | +- `handleMergeQuery`: Use the `useMergeQuery` hook provided by `@graphiql/react` that returns this function. |
| 136 | +- `handleCopyQuery`: Use the `useCopyQuery` hook provided by `@graphiql/react` that returns this function. |
| 137 | +- `handleToggleDocs` and `handleToggleHistory`: Use the `setVisiblePlugin` method of the `PluginContext`. |
| 138 | + |
| 139 | +Some class methods were callbacks to modify state which are not intended to be called manually. All these methods don't have a successor: `handleEditQuery`, `handleEditVariables`, `handleEditHeaders`, `handleEditOperationName`, `handleSelectHistoryQuery`, `handleResetResize` and `handleHintInformationRender` |
| 140 | + |
| 141 | +### Static properties have been removed |
| 142 | + |
| 143 | +In `[email protected]` the `GraphiQL` component included a bunch of static properties that exposed utility functions and other components. Most of these have been removed in `graphiql@2` since the components and functions have been moved to the `@graphiql/react` and `@graphiql/toolkit` packages. |
| 144 | + |
| 145 | +The properties that remain on the `GraphiQL` function component are `GraphiQL.Logo`, `GraphiQL.Toolbar` and `GraphiQL.Footer`. All three are React components that can be passed as children to the `GraphiQL` components and override certain parts of the UI: |
| 146 | + |
| 147 | +- `GraphiQL.Logo`: Overrides the "logo" at the top right of the screen. By default it contains the text "Graph*i*QL". |
| 148 | +- `GraphiQL.Toolbar`: Overrides the toolbar next to the query editor. By default if contains buttons for prettifying the current editor contents, merging fragment definitions into the operation definition and copying the contents of the query editor to the clipboard. Note that the default buttons will not be shown when passing this component as child to `GraphiQL`, instead it will show the children you pass to `GraphiQL.Toolbar`. The execute button will always be shown. If you want to keep the default buttons and add additional buttons you can use the `toolbar` prop. |
| 149 | +- `GraphiQL.Footer`: Adds a section below the response editor. By default this won't show up in the UI. |
| 150 | + |
| 151 | +Here is a list of all the static properties that have been removed and their replacements: |
| 152 | + |
| 153 | +- `GraphiQL.formatResult` and `GraphiQL.formatError`: Replaced by equally named functions from `@graphiql/toolkit` |
| 154 | +- `GraphiQL.QueryEditor`, `GraphiQL.VariableEditor` and `GraphiQL.HeaderEditor`: Replaced by equally named components from `@graphiql/react` |
| 155 | +- `GraphiQL.ResultViewer`: Replaced by the `ResponseEditor` component from `@graphiql/react` |
| 156 | +- `GraphiQL.Button`: Replaced by the `ToolbarButton` component from `@graphiql/react` |
| 157 | +- `GraphiQL.ToolbarButton`: This exposed the same component as `GraphiQL.Button`. |
| 158 | +- `GraphiQL.Menu`: Replaced by the `ToolbarMenu` component from `@graphiql/react` |
| 159 | +- `GraphiQL.MenuItem`: Replaced by the `ToolbarMenu.Item` component from `@graphiql/react` |
| 160 | +- `GraphiQL.Group`: Grouping multiple buttons side-by-side is not provided out-of-the box anymore in the new GraphiQL UI. If you want to implement a similar feature in the new vertical toolbar you can do so by adding your own styles for your custom toolbar elements. Example: |
| 161 | + |
| 162 | + ```jsx |
| 163 | + import { createGraphiQLFetcher } from '@graphiql/toolkit'; |
| 164 | + import { GraphiQL } from 'graphiql'; |
| 165 | + |
| 166 | + const fetcher = createGraphiQLFetcher({ url: 'https://my.endpoint' }); |
| 167 | + |
| 168 | + function MyComponent() { |
| 169 | + return ( |
| 170 | + <GraphiQL fetcher={fetcher}> |
| 171 | + <GraphiQL.Toolbar> |
| 172 | + {/* Add custom styles for your buttons using the given class */} |
| 173 | + <div className="button-group"> |
| 174 | + <button>1</button> |
| 175 | + <button>2</button> |
| 176 | + <button>3</button> |
| 177 | + </div> |
| 178 | + </GraphiQL.Toolbar> |
| 179 | + </GraphiQL> |
| 180 | + ); |
| 181 | + } |
| 182 | + ``` |
| 183 | + |
| 184 | +### `window.g` has been removed |
| 185 | + |
| 186 | +In `[email protected]` the `GraphiQL` class component stored a reference to itself on a global property named `g`. This property has been removed as refs don't exist for function components. (Also, the property was only intended for internal use like testing in the first place.) |
0 commit comments