hapi-plugin-graphiql ==================== [HAPI](http://hapijs.com/) plugin for [GraphiQL](https://github.com/graphql/graphiql) integration
Installation
------------
```shell
$ npm install hapi hapi-plugin-graphiql
```
About
-----
This is a small plugin for the [HAPI](http://hapijs.com/)
server framework for seamless integration of
[GraphiQL](https://github.com/graphql/graphiql), an interactive
[GraphQL](http://graphql.org/) user interface. This variant of GraphiQL
especially integrates a username/password based login dialog and reports
network responses in the GraphiQL status bar. In case of its
downstream/local copy of GraphiQL, it also applies some [additional
changes to GraphiQL's internals](local/).
Usage
-----
The following example shows the plugin in action.
The shown options are actually the default ones:
```js
server.register({
register: require("hapi-plugin-graphiql"),
options: {
graphiqlSource: "downstream",
graphiqlGlobals: "",
graphiqlURL: "/api",
graphqlFetchURL: "/api/data/graph",
graphqlFetchOpts: `{
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(params),
credentials: "same-origin"
}`,
loginFetchURL: "/api/auth/login",
loginFetchOpts: `{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: username,
password: password
}),
credentials: "same-origin"
}`,
loginFetchSuccess: "",
loginFetchError: "",
graphqlExample:
"query Example {\n" +
" Session {\n" +
" __typename # schema introspection\n" +
" }\n" +
"}\n",
documentationURL: "",
documentationFile: ""
}
})
```
This assumes you have a REST-based authentication endpoint
`/api/auth/login` and a GraphQL endpoint `/api/data/graphql`. The
GraphiQL UI then can be accessed under `/api`. The authentication
endpoint is assumed to accept `{ username: "...", password: "..." }` and
sets the authentication token as a HTTP cookie for `/api`. If you your
authentication endpoint returns `{ token: "..." }` and the token has to be passed in an
`Authorization` header as a Bearer token, use the following configuration instead:
```js
server.register({
register: require("hapi-plugin-graphiql"),
options: {
graphiqlSource: "downstream",
graphiqlGlobals: `var token = null;`,
graphiqlURL: "/api",
graphqlFetchURL: "/api/data/graph",
graphqlFetchOpts: `{
method: "POST",
headers: Object.assign({
"Content-Type": "application/json",
"Accept": "application/json"
}, token ? {
"Authorization": "Bearer " + token
} : {}),
body: JSON.stringify(params),
credentials: "same-origin"
}`,
loginFetchURL: "/api/auth/login",
loginFetchOpts: `{
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: username,
password: password
}),
credentials: "same-origin"
}`,
loginFetchSuccess: `token = JSON.parse(response.text()).token;`,
loginFetchError: `token = null;`,
graphqlExample:
"query Example {\n" +
" Session {\n" +
" __typename # schema introspection\n" +
" }\n" +
"}\n"
}
})
```
Options
-------
The supported configuration options are:
- `graphiqlSource`: