Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@graphiql/react

graphql1.2mMIT0.28.2TypeScript support: included
react, graphql, sdk, codemirror

readme

Changelog | API Docs | NPM

@graphiql/react

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

Purpose

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.

Getting started

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 (
    <GraphiQLProvider fetcher={fetcher}>
      <div className="graphiql-container">Hello GraphQL</div>
    </GraphiQLProvider>
  );
}

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

import { QueryEditor } from '@graphiql/react';

function MyGraphQLIDE() {
  return (
    <GraphiQLProvider fetcher={fetcher}>
      <div className="graphiql-container">
        <QueryEditor />
      </div>
    </GraphiQLProvider>
  );
}

The package also ships the necessary CSS that all its UI components need. You can import them from @graphiql/react/dist/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.

Available contexts

There are multiple contexts that own different parts of the state that make up a complete GraphQL IDE. For each context there is a provider component (<name>ContextProvider) that makes sure the context is initialized and managed properly. These components contains all the logic related to state management. In addition, for each context there is also a hook (use<name>Context) that allows you to consume its current value.

Here is a list of all contexts that come with @graphiql/react

  • StorageContext: Provides a storage API that can be used to persist state in the browser (by default using localStorage)
  • EditorContext: Manages all the editors and tabs
  • SchemaContext: Fetches, validates and stores the GraphQL schema
  • ExecutionContext: Executes GraphQL requests
  • HistoryContext: Persists executed requests in storage
  • ExplorerContext: Handles the state for the docs explorer

All context properties are documented using JSDoc 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.

Theming

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

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.

Development

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.

changelog

Archived For up to date changelogs that are automatically generated by changesets, see CHANGELOG.md files in respective workspaces. For example, the graphiql changelog is located at packages/graphiql/CHANGELOG.md, and the language server changelog is located at packages/graphql-language-service-server/CHANGELOG.md

GraphiQL 0.14.2 - 11 Aug, 2019

Fixes

  • Fix SSR & use of window when introducing new extraKeys capability (#942)

GraphiQL 0.14.0 - 11 Aug, 2019

Features

  • Add defaultVariableEditorOpen prop (#744) - @acao

Fixes

  • Fix formatting of subscription errors - #636, #722 - @benjie
  • preserve ctrl-f key for macOS - #759 - @pd4d10
  • Fix earlier 'Mode graphql failed to advance stream' on Linux by eating an exotic whitespace character - #735 closed by #932 - @benjie
  • Fix: check this.editor exists before this.editor.off in QueryEditor

Codemirror GraphQL - 0.9 - 11 Aug, 2019

Chores

  • BREAKING: Update to gls-interface and gls-parser ^2.1
  • BREAKING: Deprecate support for GraphQL 0.11 and below
  • BREAKING: introduce MIT license
  • BREAKING: Support GraphQL 14

GraphQL Language Service Server 2.1.0 - 11 Aug, 2019

Features

  • Replace babylon with @babel/parser (#879) @ganemone
  • Add support for gql template tags (#883) @ganemone @Neitsch

Chores

  • BREAKING: remove incompatible dependencies on graphql 0.11 and below
  • BREAKING: add peer support for graphql 14.x
  • BREAKING: change copyright to MIT
  • update formatting for monorepo eslint/prettier rules
  • update readme, badges

GraphQL Language Service Parser 2.1.0 - 11 Aug, 2019

Fixes

  • Fix 'mode graphql failed to advance stream' error from shift-alt-space, etc - #932 - @benjie

GraphQL Language Service Interface 2.1.0 - 11 Aug, 2019

Features

  • add __typename field suggestion against object type - (#903) @yoshiakis
  • Update sortText logic, so that field sort is schema driven rather than alphabetically sorted - (#884) @ganemone

Chores

  • BREAKING: add peer support for graphql 14.x
  • MINOR BREAKING: Use MIT license
  • add test case for language service hover - @divyenduz @AGS-

GraphQL Language Service 2.1.0

  • BREAKING: add peer support for graphql 14.x
  • BREAKING: remove incompatible dependencies on graphql 0.11 and below (b/c of gls-utils 2.x)

GraphQL Language Service Utils 2.1.0 - 11 Aug, 2019

Chores

  • BREAKING: change copyright to MIT
  • update formatting for monorepo eslint/prettier rules
  • update readme, badges

GraphQL Language Service Types 1.3.0 - 11 Aug, 2019

Chores

  • BREAKING: change copyright to MIT
  • BREAKING: add peer support for graphql 14.x
  • update formatting for monorepo eslint/prettier rules
  • update readme, badges

GraphiQL 0.13.2 - 21 June, 2019

Features

  • Hint/popup/etc DOM nodes use container rather than creating children of<body> - #791 - @codestryke
  • Add readOnly prop and pass to QueryEditor and VariableEditor - #718 - @TheSharpieOne
  • Add operationName to introspection query - #664 - @jbblanchet
  • Image Preview Functionality - #789 - @dunnbobcat @asiandrummer

Fixes

  • Destroy image hover tooltip when it isn't needed - #874 - @acao
  • Copy non-formatted query to avoid stripping out comments - #832 - @jaebradley
  • Normalizes no-break spaces - #781 - @zouxuoz
  • Prevents crashing on Shift-Alt-Space - #781 - @zouxuoz
  • Fix UI state change after favorite a query - #747 - @benjie

Chores

  • BREAKING: Upgrade to codemirror-graphql 0.8.3 - #773 - @jonaskello
  • BREAKING: Change copyright to GraphQL Contributors, License to MIT
  • Netlify deployments per PR - @orta
  • Add unit test coverage
  • Switch to Jest

Codemirror Graphql Mode 0.8.4 - 11 Aug, 2018

You will now be importing async methods from gls-interface 2.0.0, thus your bundler will require regenerator runtime

Chores

  • BREAKING - Use GLS interface/parser 2.1.0 for graphql 14
  • BREAKING - This introduces async/await

GraphQL Language Service Interface 2.0.0 - 11 Sep, 2018

Chores

  • BREAKING: upgrade internal dependencies - gls-parser, gls-types, and gls-utils to 2.0.0 - @lostplan
  • BREAKING: remove incompatible dependencies on graphql 0.11 and below - @lostplan

GraphQL Language Service Utils 2.0.0 - 11 Sep, 2018

Chores

GraphQL Language Service Utils 1.2.2 - 11 Sep, 2018

Chores

  • add graphql-js 0.13 to peer deps of types package (graphql/graphql-language-service#241)

GraphQL Language Service Server 2.0.0 - 11 Sep, 2019

Chores

  • add graphql-js 0.13 to peer dependencies (graphql/graphql-language-service#241)
  • BREAKING: upgrade internal dependencies - gls-interface, gls-server and gls-utils to 2.0.0 @lostplan

GraphQL Language Service 2.0.0 - 11 Sep, 2018

Chores

  • BREAKING: upgrade internal dependencies - gls-interface, gls-server and gls-utils to 2.0.0 @Sol