GraphQL Playground

GraphQL IDE for better development workflows (GraphQL Subscriptions, intera...

README

npm version graphql

SECURITY WARNING: both graphql-playground-html and all four (4) of it's middleware dependents untilgraphql-playground-html@1.6.22 were subject to an XSS Reflection attack vulnerability only to unsanitized user input strings to the functions therein. This was resolved in graphql-playground-html@^1.6.22. More Information CVE-2020-4038



Future of this repository: See this issue for details.


GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration).
undefined

Installation


  1. ```sh
  2. $ brew install --cask graphql-playground
  3. ```

Features


- ✨ Context-aware autocompletion & error highlighting
- 📚 Interactive, multi-column docs (keyboard support)
- ⚡️ Supports real-time GraphQL Subscriptions
- ⚙ GraphQL Config support with multiple Projects & Endpoints
- 🚥 Apollo Tracing support

Security Details

NOTE: only _unsanitized user input_ to the functions in these packages is vulnerable to the recently reported XSS Reflection attack.


Impact


Impacted are any and all unsanitized user-defined input to:

-renderPlaygroundPage()
-koaPlayground()
-expressPlayground()
-koaPlayground()
-`lambdaPlayground()

If you used static values, such as graphql-playground-electron does in it's webpack config, as well as the most common middleware implementations out there, they were not vulnerable to the attack.


The only reason this vulnerability exists is because we are using template strings in renderPlaygroundPage() with potentially unsanitized user defined variables. This allows an attacker to inject html and javascript into the page.

Common examples may be user-defined path parameters, query string, unsanitized UI provided values in database, etc., that are used to build template strings or passed directly to a renderPlaygroundPage() or the matching middleware function equivalent listed above.

Impacted Packages


All versions of these packages are impacted until the ones specified below, which are now safe for user defined input:

- graphql-playground-html: ☔ safe @ 1.6.22
- graphql-playground-express ☔ safe @ 1.7.16
- graphql-playground-koa ☔ safe @ 1.6.15
- graphql-playground-hapi ☔ safe @ 1.6.13
- graphql-playground-lambda ☔ safe @ 1.7.17
- graphql-playground-electron has always been ☔ safe from XSS attacks! This is because configuration is statically defined it's webpack config
- graphql-playground-react is safe because it does not use renderPlaygroundPage() anywhere, and thus is not susceptible to template string XSS reflection attacks.

More Information


See the security docs for more details on how your implementation might be impacted by this vulnerability. It contains safe examples, unsafe examples, workarounds, and more details.


FAQ


How is this different from GraphiQL?


GraphQL Playground uses components of GraphiQL under the hood but is meant as a more powerful GraphQL IDE enabling better (local) development workflows. Compared to GraphiQL, the GraphQL Playground ships with the following additional features:

- Interactive, multi-column schema documentation
- Automatic schema reloading
- Support for GraphQL Subscriptions
- Query history
- Configuration of HTTP headers
- Tabs

See the following question for more additonal features.

What's the difference between the desktop app and the web version?


The desktop app is the same as the web version but includes these additional features:

- Partial support for graphql-config enabling features like multi-environment setups (no support for sending HTTP headers).
- Double click on *.graphql files.

How does GraphQL Bin work?


You can easily share your Playgrounds with others by clicking on the "Share" button and sharing the generated link. You can think about GraphQL Bin like Pastebin for your GraphQL queries including the context (endpoint, HTTP headers, open tabs etc).


You can also find the announcement blog post here.


Settings


In the top right corner of the Playground window you can click on the settings icon.
These are the settings currently available:

  1. ``` js
  2. {
  3.   'editor.cursorShape': 'line', // possible values: 'line', 'block', 'underline'
  4.   'editor.fontFamily': `'Source Code Pro', 'Consolas', 'Inconsolata', 'Droid Sans Mono', 'Monaco', monospace`,
  5.   'editor.fontSize': 14,
  6.   'editor.reuseHeaders': true, // new tab reuses headers from last tab
  7.   'editor.theme': 'dark', // possible values: 'dark', 'light'
  8.   'general.betaUpdates': false,
  9.   'prettier.printWidth': 80,
  10.   'prettier.tabWidth': 2,
  11.   'prettier.useTabs': false,
  12.   'request.credentials': 'omit', // possible values: 'omit', 'include', 'same-origin'
  13.   'schema.polling.enable': true, // enables automatic schema polling
  14.   'schema.polling.endpointFilter': '*localhost*', // endpoint filter for schema polling
  15.   'schema.polling.interval': 2000, // schema polling interval in ms
  16.   'schema.disableComments': boolean,
  17.   'tracing.hideTracingResponse': true,
  18.   'tracing.tracingSupported': true, // set false to remove x-apollo-tracing header from Schema fetch requests
  19. }
  20. ```

Usage


Properties


The React component `` and all middlewares expose the following options:

- props (Middlewares & React Component)
  - endpoint [string](optional) - the GraphQL endpoint url.
  - subscriptionEndpoint [string](optional) - the GraphQL subscriptions endpoint url.
  - workspaceName [string](optional) - in case you provide a GraphQL Config, you can name your workspace here
  - config [string](optional) - the JSON of a GraphQL Config. See an example here
  - settings [ISettings](optional) - Editor settings in json format as described here

  1. ```ts
  2. interface ISettings {
  3.   'editor.cursorShape': 'line' | 'block' | 'underline'
  4.   'editor.fontFamily': string
  5.   'editor.fontSize': number
  6.   'editor.reuseHeaders': boolean
  7.   'editor.theme': 'dark' | 'light'
  8.   'general.betaUpdates': boolean
  9.   'prettier.printWidth': number
  10.   'prettier.tabWidth': number
  11.   'prettier.useTabs': boolean
  12.   'request.credentials': 'omit' | 'include' | 'same-origin'
  13.   'request.globalHeaders': { [key: string]: string }
  14.   'schema.polling.enable': boolean
  15.   'schema.polling.endpointFilter': string
  16.   'schema.polling.interval': number
  17.   'schema.disableComments': boolean
  18.   'tracing.hideTracingResponse': boolean
  19.   'tracing.tracingSupported': boolean
  20. }
  21. ```

- schema [IntrospectionResult](optional) - The result of an introspection query (an object of this form: {__schema: {...}}) The playground automatically fetches the schema from the endpoint. This is only needed when you want to override the schema.
- tabs [Tab[]](optional) - An array of tabs to inject. Note: When using this feature, tabs will be resetted each time the page is reloaded

  1. ```ts
  2. interface Tab {
  3.   endpoint: string
  4.   query: string
  5.   name?: string
  6.   variables?: string
  7.   responses?: string[]
  8.   headers?: { [key: string]: string }
  9. }
  10. ```

In addition to this, the React app provides some more properties:

- props (React Component)
- createApolloLink [(session: Session, subscriptionEndpoint?: string) => ApolloLink] - this is the equivalent to the fetcher of GraphiQL. For each query that is being executed, this function will be called

createApolloLink is only available in the React Component and not the middlewares, because the content must be serializable as it is being printed into a HTML template.

As HTML Page


If you simply want to render the Playground HTML on your own, for example when implementing a GraphQL Server, there are 2 options for you:


Note: In case you do not want to serve assets from a CDN (like jsDelivr) and instead use a local copy, you will need to install graphql-playground-react from npm, and then replace all instances of //cdn.jsdelivr.net/npm with ./node_modules. An example can be found here

As React Component


Install


  1. ```sh
  2. yarn add graphql-playground-react
  3. ```

Use


GraphQL Playground provides a React component responsible for rendering the UI and Session management.
There are 3 dependencies needed in order to run the graphql-playground-react React component.

1.  _Open Sans_ and _Source Code Pro_ fonts
2. Rendering the `` component

The GraphQL Playground requires React 16.

Including Fonts (1.)

  1. ``` html
  2. <link
  3.   href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700|Source+Code+Pro:400,700"
  4.   rel="stylesheet"
  5. />
  6. ```

Including stylesheet and the component (2., 3.)

  1. ``` js
  2. import React from 'react'
  3. import ReactDOM from 'react-dom'
  4. import { Provider } from 'react-redux'
  5. import { Playground, store } from 'graphql-playground-react'

  6. ReactDOM.render(
  7.   <Provider store={store}>
  8.     <Playground endpoint='https://api.graph.cool/simple/v1/swapi' />
  9.   </Provider>,
  10.   document.body,
  11. )
  12. ```

As Server Middleware


Install


  1. ```sh
  2. # Pick the one that matches your server framework
  3. yarn add graphql-playground-middleware-express  # for Express or Connect
  4. yarn add graphql-playground-middleware-hapi
  5. yarn add graphql-playground-middleware-koa
  6. yarn add graphql-playground-middleware-lambda
  7. ```

Usage with example


We have a full example for each of the frameworks below:




- Lambda (as serverless handler): See serverless-graphql-apollo or a quick example below.

As serverless handler


Install


  1. ```sh
  2. yarn add graphql-playground-middleware-lambda
  3. ```

Usage


handler.js

  1. ``` js
  2. import lambdaPlayground from 'graphql-playground-middleware-lambda'
  3. // or using require()
  4. // const lambdaPlayground = require('graphql-playground-middleware-lambda').default

  5. exports.graphqlHandler = function graphqlHandler(event, context, callback) {
  6.   function callbackFilter(error, output) {
  7.     // eslint-disable-next-line no-param-reassign
  8.     output.headers['Access-Control-Allow-Origin'] = '*'
  9.     callback(error, output)
  10.   }

  11.   const handler = graphqlLambda({ schema: myGraphQLSchema })
  12.   return handler(event, context, callbackFilter)
  13. }

  14. exports.playgroundHandler = lambdaPlayground({
  15.   endpoint: '/dev/graphql',
  16. })
  17. ```

serverless.yml

  1. ```yaml
  2. functions:
  3.   graphql:
  4.     handler: handler.graphqlHandler
  5.     events:
  6.       - http:
  7.           path: graphql
  8.           method: post
  9.           cors: true
  10.   playground:
  11.     handler: handler.playgroundHandler
  12.     events:
  13.       - http:
  14.           path: playground
  15.           method: get
  16.           cors: true
  17. ```

Security Issue


There is an XSS Reflection Vulnerability when using these middlewares with unsanitized user input before

Development


  1. ```sh
  2. $ cd packages/graphql-playground-react
  3. $ yarn
  4. $ yarn start
  5. ```

Open

Contributing to this project


This repository is managed by EasyCLA. Project participants must sign the free (GraphQL Specification Membership agreement before making a contribution. You only need to do this one time, and it can be signed by individual contributors or their employers.

To initiate the signature process please open a PR against this repo. The EasyCLA bot will block the merge if we still need a membership agreement from you.

You can find detailed information here. If you have issues, please email operations@graphql.org.

If your company benefits from GraphQL and you would like to provide essential financial support for the systems and people that power our community, please also consider membership in the GraphQL Foundation.

Custom Theme


From graphql-playground-react@1.7.0 on you can provide a codeTheme property to the React Component to customize your color theme.
These are the available options:

  1. ```ts
  2. export interface EditorColours {
  3.   property: string
  4.   comment: string
  5.   punctuation: string
  6.   keyword: string
  7.   def: string
  8.   qualifier: string
  9.   attribute: string
  10.   number: string
  11.   string: string
  12.   builtin: string
  13.   string2: string
  14.   variable: string
  15.   meta: string
  16.   atom: string
  17.   ws: string
  18.   selection: string
  19.   cursorColor: string
  20.   editorBackground: string
  21.   resultBackground: string
  22.   leftDrawerBackground: string
  23.   rightDrawerBackground: string
  24. }
  25. ```

Versions


This is repository is a "mono repo" and contains multiple packages using Yarn workspaces. Please be aware that versions arenot synchronised between packages. The versions of the release page refer to the electron app.

Packages


In the folder packages you'll find the following packages:

- graphql-playground-electron: Cross-platform electron app which uses graphql-playground-react
- graphql-playground-html: Simple HTML page rendering a version of graphql-playground-react hosted on JSDeliver
- graphql-playground-middleware-express: Express middleware using graphql-playground-html
- graphql-playground-middleware-hapi: Hapi middleware using graphql-playground-html
- graphql-playground-middleware-koa: Koa middleware using graphql-playground-html
- graphql-playground-middleware-lambda: AWS Lambda middleware using graphql-playground-html
- graphql-playground-react: Core of GraphQL Playground built with ReactJS



Join our Discord Server if you run into issues or have questions. We love talking to you!

Prisma