graphql/graphiql

Name: graphiql

Owner: Facebook GraphQL

Description: An in-browser IDE for exploring GraphQL.

Created: 2015-08-11 02:56:22.0

Updated: 2018-01-18 18:53:17.0

Pushed: 2018-01-15 12:52:54.0

Homepage: null

Size: 1858

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

GraphiQL

/??raf?k(?)l/ A graphical interactive in-browser GraphQL IDE. Try the live demo.

Build Status CDNJS npm

Getting started

Using a node.js server? Just use express-graphql! It can automatically present GraphiQL. Using another GraphQL service? GraphiQL is pretty easy to set up. With npm:

install --save graphiql

Alternatively, if you are using yarn:

 add graphiql

GraphiQL provides a React component responsible for rendering the UI, which should be provided with a function for fetching from GraphQL, we recommend using the fetch standard API.

rt React from 'react';
rt ReactDOM from 'react-dom';
rt GraphiQL from 'graphiql';
rt fetch from 'isomorphic-fetch';

tion graphQLFetcher(graphQLParams) {
turn fetch(window.location.origin + '/graphql', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(graphQLParams),
.then(response => response.json());


tDOM.render(<GraphiQL fetcher={graphQLFetcher} />, document.body);

Build for the web with webpack or browserify, or use the pre-bundled graphiql.js file. See the example in the git repository to see how to use the pre-bundled file.

Don'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.

For an example of setting up a GraphiQL, check out the example in this repository which also includes a few useful features highlighting GraphiQL's API.

Features
Usage

GraphiQL exports a single React component which is intended to encompass the entire browser viewport. This React component renders the GraphiQL editor.

rt GraphiQL from 'graphiql';

phiQL />

GraphiQL supports customization in UI and behavior by accepting React props and children.

Props:

Children:

Usage Examples
s CustomGraphiQL extends React.Component {
nstructor(props) {
super(props);
this.state = {
  // REQUIRED:
  // `fetcher` must be provided in order for GraphiQL to operate
  fetcher: this.props.fetcher,

  // OPTIONAL PARAMETERS
  // GraphQL artifacts
  query: '',
  variables: '',
  response: '',

  // GraphQL Schema
  // If `undefined` is provided, an introspection query is executed
  // using the fetcher.
  schema: undefined,

  // Useful to determine which operation to run
  // when there are multiple of them.
  operationName: null,
  storage: null,
  defaultQuery: null,

  // Custom Event Handlers
  onEditQuery: null,
  onEditVariables: null,
  onEditOperationName: null,

  // GraphiQL automatically fills in leaf nodes when the query
  // does not provide them. Change this if your GraphQL Definitions
  // should behave differently than what's defined here:
  // (https://github.com/graphql/graphiql/blob/master/src/utility/fillLeafs.js#L75)
  getDefaultFieldNames: null
};


 Example of using the GraphiQL Component API via a toolbar button.
ndleClickPrettifyButton(event) {
const editor = this.graphiql.getQueryEditor();
const currentText = editor.getValue();
const { parse, print } = require('graphql');
const prettyText = print(parse(currentText));
editor.setValue(prettyText);


nder() {
return (
  <GraphiQL ref={c => { this.graphiql = c; }} {...this.state}>
    <GraphiQL.Logo>
      Custom Logo
    </GraphiQL.Logo>
    <GraphiQL.Toolbar>

      // GraphiQL.Button usage
      <GraphiQL.Button
        onClick={this.handleClickPrettifyButton}
        label="Prettify"
        title="Prettify Query (Shift-Ctrl-P)"
      />

      // Some other possible toolbar items
      <GraphiQL.Menu label="File" title="File">
        <GraphiQL.MenuItem label="Save" title="Save" onSelect={...}>
      </GraphiQL.Menu>

      <OtherReactComponent someProps="true" />

    </GraphiQL.Toolbar>
    <GraphiQL.Footer>
      // Footer works the same as Toolbar
      // add items by appending child components
    </GraphiQL.Footer>
  </GraphiQL>
);


Applying an Editor Theme

In 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.

n your html
k rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.23.0/theme/solarized.css" />

n your GraphiQL JSX
phiQL
itorTheme="solarized light"

Query Samples

Query

GraphQL queries declaratively describe what data the issuer wishes to fetch from whoever is fulfilling the GraphQL query.

y FetchSomeIDQuery($someId: String!) {
man(id: $someId) {
name


More examples available from: GraphQL Queries.

Mutation

Given this schema,

t schema = new GraphQLSchema({
ery: new GraphQLObjectType({
fields: {
  numberHolder: { type: numberHolderType },
},
name: 'Query',
,
tation: new GraphQLObjectType({
fields: {
  immediatelyChangeTheNumber: {
    type: numberHolderType,
    args: { newNumber: { type: GraphQLInt } },
    resolve: (function (obj, { newNumber }) {
      return obj.immediatelyChangeTheNumber(newNumber);
    })
  }
},
name: 'Mutation',


then the following mutation queries are possible:

tion TestMutation {
rst: immediatelyChangeTheNumber(newNumber: 1) {
theNumber


Read more in this mutation test in graphql-js.

Relay has another good example using a common pattern for composing mutations. Given the following GraphQL Type Definitions,

t IntroduceShipInput {
ctionId: ID!
ipName: String!
ientMutationId: String!


 IntroduceShipPayload {
ction: Faction
ip: Ship
ientMutationId: String!

mutation calls are composed as such:

tion AddBWingQuery($input: IntroduceShipInput!) {
troduceShip(input: $input) {
ship {
  id
  name
}
faction {
  name
}
clientMutationId



nput": {
"shipName": "B-Wing",
"factionId": "1",
"clientMutationId": "abcde"


Read more from Relay Mutation Documentation.

Fragment

Fragments allow for the reuse of common repeated selections of fields, reducing duplicated text in the document. Inline Fragments can be used directly within a selection to condition upon a type condition when querying against an interface or union. Therefore, instead of the following query:


ke: human(id: "1000") {
name
homePlanet

ia: human(id: "1003") {
name
homePlanet


using fragments, the following query is possible.


ke: human(id: "1000") {
...HumanFragment

ia: human(id: "1003") {
...HumanFragment



ment HumanFragment on Human {
me
mePlanet

Read more from GraphQL Fragment Specification.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.