GraphiQL API Documentation
    Preparing search index...

    Module @graphiql/react

    Changelog | API Docs | NPM

    @graphiql/react

    A React SDK for building integrated GraphQL developer experiences for the web.

    This package contains a set of building blocks that allow its users to build GraphQL IDEs with ease. It's the set of components that make up GraphiQL, the first and official GraphQL IDE, owned and maintained by the GraphQL Foundation.

    There are two kinds of building blocks that this package provides: Stateful context providers for state management and simple UI components.

    All the state for your GraphQL IDE lives in multiple contexts. The easiest way to get started is by using the GraphiQLProvider component that renders all the individual providers.

    There is one required prop called fetcher. This is a function that performs GraphQL request against a given endpoint. You can easily create a fetcher using the method createGraphiQLFetcher from the @graphiql/toolkit package.

    import { GraphiQLProvider } from '@graphiql/react';
    import { createGraphiQLFetcher } from '@graphiql/toolkit';
    
    const fetcher = createGraphiQLFetcher({
      url: 'https://my.graphql.api/graphql',
    });
    
    function MyGraphQLIDE() {
      return (
        
          
    Hello GraphQL
    ); }

    Inside the provider you can now use any UI component provided by @graphiql/react. For example, you can render an operation editor like this:

    import { QueryEditor } from '@graphiql/react';
    
    function MyGraphQLIDE() {
      return (
        
          
    ); }

    The package also ships the necessary CSS that all its UI components need. You can import them from @graphiql/react/style.css.

    Note: In order for these styles to apply, the UI components need to be rendered inside an element that has a class name graphiql-container.

    By default, the UI components will try to use the Roboto font for regular text and the Fira Code font for mono-space text. If you want to use the default fonts you can load them using these files:

    • @graphiql/react/font/roboto.css
    • @graphiql/react/font/fira-code.css.

    You can, of course, use any other method to load these fonts (for example, loading them from Google Fonts).

    Further details on how to use @graphiql/react can be found in the reference implementation of a GraphQL IDE - GraphiQL - in the graphiql package.

    GraphiQL uses a set of state management stores, each responsible for a specific part of the IDE's behavior. These stores contain all logic related to state management and can be accessed via custom React hooks.

    • useMonaco: Access monaco-editor exports and the monaco-graphql instance. Designed for safe use in SSR environments.
    • useGraphiQL: Access the current state.
    • useGraphiQLActions: Trigger actions that mutate the state. This hook never rerenders.

    The useGraphiQLActions hook exposes all actions across store slices. The useGraphiQL hook provides access to the following store slices:

    Store Slice Responsibilities
    storage Provides a storage API that can be used to persist state in the browser (by default using localStorage)
    editor Manages query, variables, headers, and response editors and tabs
    execution Handles the execution of GraphQL requests
    plugin Manages plugins and the currently active plugin
    schema Fetches, validates, and stores the GraphQL schema
    theme Manages the current theme and provides a method to update it
    import { useGraphiQL, useGraphiQLActions } from '@graphiql/react';

    // Get an action to fetch the schema and an action to change theme
    const { introspect, setTheme } = useGraphiQLActions();

    // Use a selector to access specific parts of the state like current schema and theme
    const { schema, theme } = useGraphiQL(state => ({
    schema: state.schema,
    theme: state.theme,
    }));

    All store properties are documented using TSDoc comments. If you're using an IDE like VSCode for development, these descriptions will show up in auto-complete tooltips. All these descriptions can also be found in the API Docs.

    All the components from @graphiql/react have been designed with customization in mind. We achieve this using CSS variables.

    All variables that are available for customization can be found in the root.css file.

    Colors are defined using the HSL format. All CSS variables for colors are defined as a list of the three values that make up HSL (hue, saturation and lightness).

    This approach allows @graphiql/react to use transparent colors by passing the value of the CSS variable in the hsla function. This enables us to provide truly reusable UI elements where good contrasts are preserved regardless of the elements background.

    If you want to develop with @graphiql/react locally - in particular when working on the graphiql package - all you need to do is run yarn dev in the package folder in a separate terminal. This will build the package using Vite. When using it in combination with yarn dev:graphiql (running in the repo root) this will give you auto-reloading when working on graphiql and @graphiql/react simultaneously.

    Enumerations

    KeyCode

    Classes

    KeyMod
    Range
    Uri

    Interfaces

    GraphiQLPlugin
    SchemaReference
    SlicesWithActions

    Type Aliases

    EditorProps
    MonacoEditor
    TabsState
    Theme

    Variables

    ArgumentIcon
    Button
    ButtonGroup
    ChevronDownIcon
    ChevronLeftIcon
    ChevronUpIcon
    CloseIcon
    CopyIcon
    createBoundedUseStore
    DeprecatedArgumentIcon
    DeprecatedEnumValueIcon
    DeprecatedFieldIcon
    Dialog
    DirectiveIcon
    DocsFilledIcon
    DocsIcon
    EnumValueIcon
    ExecuteButton
    FieldIcon
    GraphiQLProvider
    HeaderEditor
    HistoryIcon
    ImagePreview
    ImplementsIcon
    isMacOs
    KEY_MAP
    KeyboardShortcutIcon
    MagnifyingGlassIcon
    markdown
    MarkdownContent
    MergeIcon
    onEditorContainerKeyDown
    PenIcon
    PlayIcon
    PlusIcon
    PrettifyIcon
    QueryEditor
    ReloadIcon
    ResponseEditor
    RootTypeIcon
    SettingsIcon
    Spinner
    StarFilledIcon
    StarIcon
    StopIcon
    Tab
    Tabs
    ToolbarButton
    ToolbarMenu
    Tooltip
    TrashIcon
    TypeIcon
    UnStyledButton
    useDidUpdate
    useEditorStore
    useExecutionStore
    useMonaco
    usePluginStore
    useSchemaStore
    useStorageContext
    VariableEditor
    VisuallyHidden

    Functions

    cleanupDisposables
    cn
    createEditor
    debounce
    formatJSONC
    formatShortcutForOS
    getOrCreateModel
    parseJSONC
    pick
    tryParseJSONC
    useChangeHandler
    useCopyQuery
    useDragResize
    useEditorContext
    useEditorState
    useExecutionContext
    useGraphiQL
    useGraphiQLActions
    useHeadersEditorState
    useMergeQuery
    useOperationsEditorState
    useOptimisticState
    usePluginContext
    usePrettifyEditors
    useSchemaContext
    useStorage
    useTheme
    useVariablesEditorState