Skip to content

Commit

Permalink
feat: Add support for defaultPlaygroundQuery
Browse files Browse the repository at this point in the history
Fixes #512
  • Loading branch information
maticzav committed Jan 15, 2019
1 parent 33bed88 commit c9f10c4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
54 changes: 27 additions & 27 deletions README.md
Expand Up @@ -74,17 +74,16 @@ server.start(() => console.log('Server is running on localhost:4000'))

The `props` argument accepts the following fields:


| Key | Type | Default | Note |
| ------------------ | --------------------------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `typeDefs` | `String` or `Function` or `DocumentNode` or `array` of previous | `null` | Contains GraphQL type definitions in [SDL](https://blog.graph.cool/graphql-sdl-schema-definition-language-6755bcb9ce51) or file path to type definitions (required if `schema` is not provided \*) |
| `resolvers` | Object | `null` | Contains resolvers for the fields specified in `typeDefs` (required if `schema` is not provided \*) |
| `resolverValidationOptions` | Object | `null` | Object which controls the resolver validation behaviour (see ["Generating a schema"](https://www.apollographql.com/docs/graphql-tools/generate-schema.html#makeExecutableSchema)) for more information |
| `schema` | Object | `null` | An instance of [`GraphQLSchema`](http://graphql.org/graphql-js/type/#graphqlschema) (required if `typeDefs` and `resolvers` are not provided \*) |
| `mocks` | Object or Boolean | `null` | Applies [mocks to schema](https://github.com/apollographql/graphql-tools/blob/master/docs/source/mocking.md). Setting this to true will apply a default mock, however you can pass an object to customize the mocks similar to the resolvers map. |
| `context` | Object or Function | `{}` | Contains custom data being passed through your resolver chain. This can be passed in as an object, or as a Function with the signature `(req: ContextParameters) => any` \*\* |
| `schemaDirectives` | Object | `null` | [`Apollo Server schema directives`](https://www.apollographql.com/docs/graphql-tools/schema-directives.html) that allow for transforming schema types, fields, and arguments |
| `middlewares` | `array` of Middleware | `[]` | A list of [`GraphQLMiddleware`](https://github.com/graphcool/graphql-middleware) middleware. |
| Key | Type | Default | Note |
| --------------------------- | --------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `typeDefs` | `String` or `Function` or `DocumentNode` or `array` of previous | `null` | Contains GraphQL type definitions in [SDL](https://blog.graph.cool/graphql-sdl-schema-definition-language-6755bcb9ce51) or file path to type definitions (required if `schema` is not provided \*) |
| `resolvers` | Object | `null` | Contains resolvers for the fields specified in `typeDefs` (required if `schema` is not provided \*) |
| `resolverValidationOptions` | Object | `null` | Object which controls the resolver validation behaviour (see ["Generating a schema"](https://www.apollographql.com/docs/graphql-tools/generate-schema.html#makeExecutableSchema)) for more information |
| `schema` | Object | `null` | An instance of [`GraphQLSchema`](http://graphql.org/graphql-js/type/#graphqlschema) (required if `typeDefs` and `resolvers` are not provided \*) |
| `mocks` | Object or Boolean | `null` | Applies [mocks to schema](https://github.com/apollographql/graphql-tools/blob/master/docs/source/mocking.md). Setting this to true will apply a default mock, however you can pass an object to customize the mocks similar to the resolvers map. |
| `context` | Object or Function | `{}` | Contains custom data being passed through your resolver chain. This can be passed in as an object, or as a Function with the signature `(req: ContextParameters) => any` \*\* |
| `schemaDirectives` | Object | `null` | [`Apollo Server schema directives`](https://www.apollographql.com/docs/graphql-tools/schema-directives.html) that allow for transforming schema types, fields, and arguments |
| `middlewares` | `array` of Middleware | `[]` | A list of [`GraphQLMiddleware`](https://github.com/graphcool/graphql-middleware) middleware. |

> (\*) There are two major ways of providing the [schema](https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e) information to the `constructor`:
>
Expand Down Expand Up @@ -117,19 +116,20 @@ Once your `GraphQLServer` is instantiated, you can call the `start` method on it

The `options` object has the following fields:

| Key | Type | Default | Note |
| --------------- | ---------------------------------------------------------------- | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cors` | Object | `null` | Contains [configuration options](https://github.com/expressjs/cors#configuration-options) for [cors](https://github.com/expressjs/cors) |
| `tracing` | Boolean or [TracingOptions](/src/types.ts#L49-L51) | `'http-header'` | Indicates whether [Apollo Tracing](https://github.com/apollographql/apollo-tracing) should be enabled or disabled for your server (if a string is provided, accepted values are: `'enabled'`, `'disabled'`, `'http-header'`) |
| `port` | Number or String | `4000` | Determines the port your server will be listening on (note that you can also specify the port by setting the `PORT` environment variable) |
| `endpoint` | String | `'/'` | Defines the HTTP endpoint of your server |
| `subscriptions` | Object or String or `false` | `'/'` | Defines the subscriptions (websocket) endpoint for your server; accepts an object with [subscription server options](https://github.com/apollographql/subscriptions-transport-ws#constructoroptions-socketoptions) `path`, `keepAlive`, `onConnect` and `onDisconnect`; setting to `false` disables subscriptions completely |
| `playground` | String or `false` | `'/'` | Defines the endpoint where you can invoke the [Playground](https://github.com/graphcool/graphql-playground); setting to `false` disables the playground endpoint |
| `uploads` | [UploadOptions](/src/types.ts#L39-L43) or `false` or `undefined` | `null` | Provides information about upload limits; the object can have any combination of the following three keys: `maxFieldSize`, `maxFileSize`, `maxFiles`; each of these have values of type Number; setting to `false` disables file uploading |
| `https` | [HttpsOptions](/src/types.ts#L62-L65) or `undefined` | `undefined` | Enables HTTPS support with a key/cert
| `getEndpoint` | String or Boolean | `false` | Adds a graphql HTTP GET endpoint to your server (defaults to `endpoint` if `true`). Used for leveraging CDN level caching. |
| `deduplicator` | Boolean | `true` | Enables [graphql-deduplicator](https://github.com/gajus/graphql-deduplicator). Once enabled sending the header `X-GraphQL-Deduplicate` will deduplicate the data. |
| `bodyParserOptions` | BodyParserJSONOptions | [BodyParserJSONOptions Defaults](https://github.com/expressjs/body-parser#bodyparserjsonoptions) | Allows pass through of [body-parser options](https://github.com/expressjs/body-parser#bodyparserjsonoptions) |
| Key | Type | Default | Note |
| ------------------------ | ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cors` | Object | `null` | Contains [configuration options](https://github.com/expressjs/cors#configuration-options) for [cors](https://github.com/expressjs/cors) |
| `tracing` | Boolean or [TracingOptions](/src/types.ts#L49-L51) | `'http-header'` | Indicates whether [Apollo Tracing](https://github.com/apollographql/apollo-tracing) should be enabled or disabled for your server (if a string is provided, accepted values are: `'enabled'`, `'disabled'`, `'http-header'`) |
| `port` | Number or String | `4000` | Determines the port your server will be listening on (note that you can also specify the port by setting the `PORT` environment variable) |
| `endpoint` | String | `'/'` | Defines the HTTP endpoint of your server |
| `subscriptions` | Object or String or `false` | `'/'` | Defines the subscriptions (websocket) endpoint for your server; accepts an object with [subscription server options](https://github.com/apollographql/subscriptions-transport-ws#constructoroptions-socketoptions) `path`, `keepAlive`, `onConnect` and `onDisconnect`; setting to `false` disables subscriptions completely |
| `playground` | String or `false` | `'/'` | Defines the endpoint where you can invoke the [Playground](https://github.com/graphcool/graphql-playground); setting to `false` disables the playground endpoint |
| `playgroundDefaultQuery` | String | `undefined` | Defines default query displayed in Playground. |
| `uploads` | [UploadOptions](/src/types.ts#L39-L43) or `false` or `undefined` | `null` | Provides information about upload limits; the object can have any combination of the following three keys: `maxFieldSize`, `maxFileSize`, `maxFiles`; each of these have values of type Number; setting to `false` disables file uploading |
| `https` | [HttpsOptions](/src/types.ts#L62-L65) or `undefined` | `undefined` | Enables HTTPS support with a key/cert |
| `getEndpoint` | String or Boolean | `false` | Adds a graphql HTTP GET endpoint to your server (defaults to `endpoint` if `true`). Used for leveraging CDN level caching. |
| `deduplicator` | Boolean | `true` | Enables [graphql-deduplicator](https://github.com/gajus/graphql-deduplicator). Once enabled sending the header `X-GraphQL-Deduplicate` will deduplicate the data. |
| `bodyParserOptions` | BodyParserJSONOptions | [BodyParserJSONOptions Defaults](https://github.com/expressjs/body-parser#bodyparserjsonoptions) | Allows pass through of [body-parser options](https://github.com/expressjs/body-parser#bodyparserjsonoptions) |

Additionally, the `options` object exposes these `apollo-server` options:

Expand Down Expand Up @@ -167,8 +167,9 @@ See the original documentation in [`graphql-subscriptions`](https://github.com/a
#### `mocking`

Mocking the schema is straight forward, along wit

```javascript
import { GraphqlServer, MockList } from 'graphql-yoga';
import { GraphqlServer, MockList } from 'graphql-yoga'

const typeDefs = `
type Query {
Expand All @@ -180,9 +181,8 @@ const typeDefs = `
const mocks = {
Query: () => ({
hello: () => 'Hello World',
listOfStrings: () => new MockList([2,6]),
listOfStrings: () => new MockList([2, 6]),
}),

}

const server = new GraphQLServer({ typeDefs, mocks })
Expand Down
20 changes: 14 additions & 6 deletions src/index.ts
Expand Up @@ -327,12 +327,20 @@ export class GraphQLServer {
}

if (this.options.playground) {
const playgroundOptions = this.subscriptionServerOptions
? {
endpoint: this.options.endpoint,
subscriptionsEndpoint: this.subscriptionServerOptions.path,
}
: { endpoint: this.options.endpoint }
const playgroundOptions = {
endpoint: this.options.endpoint,
subscriptionsEndpoint: this.subscriptionServerOptions
? this.subscriptionServerOptions.path
: undefined,
tabs: this.options.defaultPlaygroundQuery
? [
{
endpoint: this.options.endpoint,
query: this.options.defaultPlaygroundQuery,
},
]
: undefined,
}

app.get(this.options.playground, expressPlayground(playgroundOptions))
}
Expand Down

0 comments on commit c9f10c4

Please sign in to comment.