Skip to content

Commit

Permalink
Improve the dev and demo experience by aligning with ApolloServer reg…
Browse files Browse the repository at this point in the history
…arding when to enable introspection and the playground by default. (#170)

https://www.apollographql.com/docs/apollo-server/testing/graphql-playground/#enabling-graphql-playground-in-production
Conservative when NODE_ENV = production, requiring explicit selection, sensible defaults in development mode.
  • Loading branch information
rhyslbw authored May 9, 2020
1 parent cb00b56 commit 7ef646e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/__test__/Server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Server', () => {
afterEach(() => {
httpServer.close()
})
it('is returns data for all valid queries', async () => {
it('returns data for all valid queries', async () => {
const validQueryResult = await client.query({
query: gql`query validButNotWhitelisted {
cardano {
Expand Down
13 changes: 6 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IntrospectionNotPermitted, MissingConfig, TracingRequired } from './lib
import { CorsOptions } from 'apollo-server-express'

export type Config = {
allowIntrospection: boolean
allowIntrospection?: boolean
allowedOrigins: CorsOptions['origin']
apiPort: number
cacheEnabled: boolean
Expand Down Expand Up @@ -35,9 +35,8 @@ export function getConfig (): Config {
if (whitelistPath && allowIntrospection) {
throw new IntrospectionNotPermitted('whitelist')
}

return {
allowIntrospection: allowIntrospection || false,
allowIntrospection,
allowedOrigins: allowedOrigins || true,
apiPort: apiPort || 3100,
cacheEnabled: cacheEnabled || false,
Expand All @@ -62,14 +61,14 @@ function filterAndTypecastEnvs (env: any) {
WHITELIST_PATH
} = env
return {
allowIntrospection: ALLOW_INTROSPECTION === 'true',
allowIntrospection: ALLOW_INTROSPECTION === 'true' ? true : undefined,
allowedOrigins: ALLOWED_ORIGINS,
apiPort: Number(API_PORT),
cacheEnabled: CACHE_ENABLED === 'true',
cacheEnabled: CACHE_ENABLED === 'true' ? true : undefined,
hasuraUri: HASURA_URI,
prometheusMetrics: PROMETHEUS_METRICS === 'true',
prometheusMetrics: PROMETHEUS_METRICS === 'true' ? true : undefined,
queryDepthLimit: Number(QUERY_DEPTH_LIMIT),
tracing: TRACING === 'true',
tracing: TRACING === 'true' ? true : undefined,
whitelistPath: WHITELIST_PATH
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ buildContext(config.hasuraUri)
cacheControl: config.cacheEnabled ? { defaultMaxAge: 20 } : undefined,
context,
introspection: config.allowIntrospection,
playground: config.allowIntrospection,
plugins,
resolvers,
validationRules,
Expand All @@ -42,6 +43,9 @@ buildContext(config.hasuraUri)
app.listen({ port: config.apiPort }, () => {
const serverUri = `http://localhost:${config.apiPort}`
console.log(`GraphQL HTTP server at ${serverUri}${server.graphqlPath}`)
if (process.env.NODE_ENV !== 'production' && config.whitelistPath) {
console.warn('As a whitelist is in effect, the GraphQL Playground is available, but will not allow schema exploration')
}
if (config.prometheusMetrics) {
console.log(`Prometheus metrics at ${serverUri}/metrics`)
}
Expand Down

0 comments on commit 7ef646e

Please sign in to comment.