UNPKG

10.9 kBMarkdownView Raw
1# GraphiQL
2
3> **Security Notice:** All versions of `graphiql` < `1.4.3` are vulnerable to an XSS attack in cases where the GraphQL server to which the GraphiQL web app connects is not trusted. Learn more in [our security advisory](https://github.com/graphql/graphiql/tree/main/docs/security/2021-introspection-schema-xss.md).
4
5> **Breaking Changes & Improvements:** several interfaces are being dropped for new ones for GraphiQL 2.0.0! Read more in [this issue](https://github.com/graphql/graphiql/issues/1165)
6
7> **[`graphiql@2.0.0-beta`](https://github.com/graphql/graphiql/issues/983)** is a much larger ongoing effort that introduces plugins, i18n, and so many more features after a substantial rewrite using modern react.
8
9[![NPM](https://img.shields.io/npm/v/graphiql.svg)](https://npmjs.com/graphiql)
10![jsDelivr hits (npm)](https://img.shields.io/jsdelivr/npm/hm/graphiql)
11![npm downloads](https://img.shields.io/npm/dm/graphiql?label=npm%20downloads)
12![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/graphiql)
13![npm bundle size (version)](https://img.shields.io/bundlephobia/min/graphiql/latest)
14![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/graphiql/latest)
15[![License](https://img.shields.io/npm/l/graphiql.svg?style=flat-square)](LICENSE)
16[Discord Channel](https://discord.gg/NP5vbPeUFp)
17_/ˈɡrafək(ə)l/_ A graphical interactive in-browser GraphQL IDE. [Try the live demo](http://graphql.org/swapi-graphql).
18
19[![](https://raw.githubusercontent.com/graphql/graphiql/master/packages/graphiql/resources/graphiql.jpg)](http://graphql.org/swapi-graphql)
20
21## Features
22
23- Syntax highlighting.
24- Intelligent type ahead of fields, arguments, types, and more.
25- Real-time error highlighting and reporting for queries and variables.
26- Automatic query and variables completion.
27- Automatically adds required fields to queries.
28- Documentation explorer, search, with markdown support.
29- Query History using local storage
30- Run and inspect query results using _any_ promise that resolves JSON results. HTTPS or WSS not required.
31- Supports full [GraphQL Language Specification](https://github.com/graphql/graphql-wg):
32 - Queries, Mutations, Subscriptions, Fragments, Unions, directives, multiple operations per query, etc
33
34## Live Demos
35
36We have a few demos of `master` branch via the default netlify build (the same URL paths apply to deploy previews on PRs):
37
381. [`graphiql.min.js` demo](https://graphiql-test.netlify.com/) - the min.js bundle for the current ref
392. [`graphiql.js` demo](https://graphiql-test.netlify.com/dev) - development build is nice for react inspector, debugging, etc
403. [bundle analyzer for graphiql.min.js](https://graphiql-test.netlify.com/analyzer)
41
42## Examples
43
44- [`Unpkg (CDN)`](https://github.com/graphql/graphiql/blob/main/examples/graphiql-cdn/) - a single html file using cdn assets and a script tag
45- [`Webpack`](https://github.com/graphql/graphiql/blob/main/examples/graphiql-webpack/) - a starter for webpack
46- [`Create React App`](https://github.com/graphql/graphiql/blob/main/examples/graphiql-create-react-app) - an example using [Create React App](https://create-react-app.dev/)
47- [`Parcel`](https://github.com/graphql/graphiql/blob/main/examples/graphiql-parcel) - an example using [Parcel](https://parceljs.org/)
48- `Rollup` - TODO
49
50## Getting started
51
52### Modules
53
54You can use the `graphiql` module, however bear in mind that `react` `react-dom` and `graphql` will all need to be present already for it to work, as they are `peerDependencies`
55
56With `npm`:
57
58```sh
59npm install --save graphiql react react-dom graphql
60```
61
62Alternatively, if you are using [`yarn`](https://yarnpkg.com/):
63
64```
65yarn add graphiql react react-dom graphql
66```
67
68### UMD
69
70With `unpkg`/`jsdelivr`, etc.:
71
72```html
73<link href="https://unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" />
74<script crossorigin src="https://unpkg.com/graphiql/graphiql.min.js"></script>
75```
76
77(see: Usage UMD Bundle below for more required script tags)
78
79## Usage
80
81Build for the web with [webpack](https://webpack.js.org/) or [browserify](http://browserify.org/), or use the pre-bundled `graphiql.js` file. See the [cdn example](https://github.com/graphql/graphiql/blob/main/examples/graphiql-cdn/) in the git repository to see how to use the pre-bundled file, or see the [webpack example](https://github.com/graphql/graphiql/blob/main/examples/graphiql-webpack) to see how to bundle an application using the `GraphiQL` component.
82
83### Usage: NPM module
84
85**Note**: If you are having webpack issues or questions about webpack, make sure you've cross-referenced your webpack configuration with our own [webpack example](https://github.com/graphql/graphiql/blob/main/examples/graphiql-webpack) first. We now have tests in CI that ensure this always builds, and we ensure it works end-to-end with every publish.
86
87Using another GraphQL service? Here's how to get GraphiQL set up:
88
89GraphiQL provides a React component responsible for rendering the UI, which should be provided with a required `fetcher function for executing GraphQL operations against your schema.
90
91For HTTP transport implementations, we recommend using the [fetch](https://fetch.spec.whatwg.org/) standard API, but you can use anything that matches [the type signature](https://graphiql-test.netlify.app/typedoc/modules/graphiql-toolkit.html#fetcher), including async iterables and observables.
92
93You can also install `@graphiql/create-fetcher` to make it easier to create a simple fetcher for conventional http and websockets transports. It uses `graphql-ws@4.x` protocol by default.
94
95```js
96import React from 'react';
97import ReactDOM from 'react-dom';
98
99import GraphiQL from 'graphiql';
100import { createGraphiQLFetcher } from '@graphiql/toolkit';
101
102const fetcher = createGraphiQLFetcher({
103 url: window.location.origin + '/graphql',
104});
105
106ReactDOM.render(
107 <GraphiQL fetcher={fetcher} editorTheme={'dracula'} />,
108 document.body,
109);
110```
111
112[Read more about using `createGraphiQLFetcher` in the readme](https://github.com/graphql/graphiql/tree/main/packages/graphiql-toolkit/docs/create-fetcher.md) to learn how to add headers, support the legacy `subscriptions-transport-ws` protocol, and more.
113
114### Usage: UMD Bundle over CDN (Unpkg, JSDelivr, etc)
115
116Don't forget to include the CSS file on the page! If you're using `npm` or `yarn`, you can find it in `node_modules/graphiql/graphiql.css`, or you can download it from the [releases page](https://github.com/graphql/graphiql/releases).
117
118For an example of setting up a GraphiQL, check out the [example](https://github.com/graphql/graphiql/blob/main/examples/graphiql-cdn/) in this repository which also includes a few useful features highlighting GraphiQL's API.
119
120The most minimal way to set up GraphiQL is a single index.html file:
121
122```html
123<html>
124 <head>
125 <title>Simple GraphiQL Example</title>
126 <link href="https://unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" />
127 </head>
128 <body style="margin: 0;">
129 <div id="graphiql" style="height: 100vh;"></div>
130
131 <script
132 crossorigin
133 src="https://unpkg.com/react/umd/react.production.min.js"
134 ></script>
135 <script
136 crossorigin
137 src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"
138 ></script>
139 <script
140 crossorigin
141 src="https://unpkg.com/graphiql/graphiql.min.js"
142 ></script>
143
144 <script>
145 const fetcher = GraphiQL.createFetcher({ url: 'https://my/graphql' });
146
147 ReactDOM.render(
148 React.createElement(GraphiQL, { fetcher: fetcher }),
149 document.getElementById('graphiql'),
150 );
151 </script>
152 </body>
153</html>
154```
155
156**Notes**:
157
158- the inlined styles are important for ensuring GraphiQL is visible and fills the canvas.
159- using `React.createElement` directly is laborious, so follow the webpack instructions above for more highly customized implementation
160- we can use [`GraphiQL.createFetcher`](https://github.com/graphql/graphiql/tree/main/packages/graphiql-toolkit/docs/create-fetcher.md) in the UMD bundle only, so that it can be tree shaken out for modules
161
162### GraphiQL for my GraphQL Service/HTTP Server/Etc
163
164You may be using a runtime that already provides graphiql, or that provides it via a middleware. For example, we support [`express-graphql`](https://github.com/graphql/express-graphql)!
165
166I would suggest a search for "graphiql <my runtime>" such as "graphiql express", "graphiql absinthe", etc to learn a potentially simpler route to setup for your environment. There are many npm packages, ruby gems, java utilities for deploying graphiql.
167
168Here are some example searches:
169
170- https://www.npmjs.com/search?q=graphiql - ~117 hits
171- https://pypi.org/search/?q=graphiql - ~33 hits
172- https://search.maven.org/search?q=graphiql - ~15 hits
173- https://rubygems.org/search?utf8=%E2%9C%93&query=graphiql - ~6 hits
174- https://godoc.org/?q=graphiql - ~12 hits
175- https://packagist.org/?query=%22graphiql%22 - ~5 hits
176- https://crates.io/search?q=graphiql - ~2 hits
177
178This doesn't include runtimes or libraries where GraphiQL is used but isn't referenced in the package registry search entry.
179
180## Customize
181
182GraphiQL supports customization in UI and behavior by accepting React props and children.
183
184<span id="props"> </span>
185
186### Props
187
188`fetcher` is the only required prop for `<GraphiQL />`.
189
190For props documentation, see the [API Docs](https://graphiql-test.netlify.app/typedoc/modules/graphiql.html#graphiqlprops-1)
191
192### Children (this pattern will be dropped in 2.0.0)
193
194- `<GraphiQL.Logo>`: Replace the GraphiQL logo with your own.
195
196- `<GraphiQL.Toolbar>`: Add a custom toolbar above GraphiQL. If not provided, a
197 default toolbar may contain common operations. Pass the empty
198 `<GraphiQL.Toolbar />` if an empty toolbar is desired.
199
200- `<GraphiQL.Button>`: Add a button to the toolbar above GraphiQL.
201
202- `<GraphiQL.Menu>`: Add a dropdown menu to the toolbar above GraphiQL.
203
204 - `<GraphiQL.MenuItem>`: Items for a menu.
205
206- `<GraphiQL.Select>`: Add a select list to the toolbar above GraphiQL.
207
208 - `<GraphiQL.SelectOption>`: Options for a select list.
209
210- `<GraphiQL.Group>`: Add a group of associated controls to the
211 toolbar above GraphiQL. Expects children to be `<GraphiQL.Button>`,
212 `<GraphiQL.Menu>`, or `<GraphiQL.Select>`.
213
214- `<GraphiQL.Footer>`: Add a custom footer below GraphiQL Results.
215
216## Full Usage Example
217
218TODO: kitchen sink example project or codesandbox
219
220### Applying an Editor Theme
221
222In order to theme the editor portions of the interface, you can supply a `editorTheme` prop. You'll also need to load the appropriate CSS for the theme (similar to loading the CSS for this project). [See the themes available here](https://codemirror.net/demo/theme.html).
223
224```js
225// In your html
226<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/theme/solarized.css" />
227
228// In your GraphiQL JSX
229<GraphiQL
230 editorTheme="solarized light"
231/>
232```