Skip to content

Commit

Permalink
feat(postgraphql) add extendedErrors option tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zopf committed Jul 18, 2017
1 parent d3b6f61 commit f39b991
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
5 changes: 3 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ The usage of the `postgraphql` binary is as follows. To pull up this documentati
-l, --body-size-limit <string> set the maximum size of JSON bodies that can be parsed (default 100kB) The size can be given as a human-readable string, such as '200kB' or '5MB' (case insensitive).
--secret <string> DEPRECATED: Use jwt-secret instead
-e, --jwt-secret <string> the secret to be used when creating and verifying JWTs. if none is provided auth will be disabled
-A, --jwt-audience <string> a comma separated list of audiences your jwt token can contain. If no audience is given the audience defaults to `postgraphql`
--jwt-role <string> a comma seperated list of strings that create a path in the jwt from which to extract the postgres role. if none is provided it will use the key `role` on the root of the jwt.
-A, --jwt-audiences <string> a comma separated list of audiences your jwt token can contain. If no audience is given the audience defaults to `postgraphql`
--jwt-role <string> a comma separated list of strings that create a path in the jwt from which to extract the postgres role. if none is provided it will use the key `role` on the root of the jwt.
--export-schema-json [path] enables exporting the detected schema, in JSON format, to the given location. The directories must exist already, if the file exists it will be overwritten.
--export-schema-graphql [path] enables exporting the detected schema, in GraphQL schema format, to the given location. The directories must exist already, if the file exists it will be overwritten.
--show-error-stack [setting] show JavaScript error stacks in the GraphQL result errors
--extended-errors <string> a comma separated list of extended Postgres error fields to display in the GraphQL result. Possible strings: `['hint', 'detail', 'errcode']`. Default: `[]`
Get Started:
Expand Down
3 changes: 2 additions & 1 deletion src/postgraphql/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ program
.option('--secret <string>', 'DEPRECATED: Use jwt-secret instead')
.option('-e, --jwt-secret <string>', 'the secret to be used when creating and verifying JWTs. if none is provided auth will be disabled')
.option('-A, --jwt-audiences <string>', 'a comma separated list of audiences your jwt token can contain. If no audience is given the audience defaults to `postgraphql`', (option: string) => option.split(','))
.option('--jwt-role <string>', 'a comma seperated list of strings that create a path in the jwt from which to extract the postgres role. if none is provided it will use the key `role` on the root of the jwt.', (option: string) => option.split(','))
.option('--jwt-role <string>', 'a comma separated list of strings that create a path in the jwt from which to extract the postgres role. if none is provided it will use the key `role` on the root of the jwt.', (option: string) => option.split(','))
.option('--export-schema-json [path]', 'enables exporting the detected schema, in JSON format, to the given location. The directories must exist already, if the file exists it will be overwritten.')
.option('--export-schema-graphql [path]', 'enables exporting the detected schema, in GraphQL schema format, to the given location. The directories must exist already, if the file exists it will be overwritten.')
.option('--show-error-stack [setting]', 'show JavaScript error stacks in the GraphQL result errors')
.option('--extended-errors <string>', 'a comma separated list of extended Postgres error fields to display in the GraphQL result. Possible strings: `[\'hint\', \'detail\', \'errcode\']`. Default: `[]`', (option: string) => option.split(',').map(field => field.toLowerCase()))

program.on('--help', () => console.log(`
Get Started:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function extendedFormatError(error: GraphQLError, fields: Array<String>):
throw new Error('Received null or undefined error.')
}
const originalError = error.originalError as GraphQLErrorExtended
fields = fields.map(field => field.toLowerCase())
return {
message: error.message,
locations: error.locations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ const gqlSchema = new GraphQLSchema({
resolve: (source, args, context) =>
context[$$pgClient].query('EXECUTE'),
},
testError: {
type: GraphQLString,
resolve: (source, args, context) => {
const err = new Error('test message')
err.detail = 'test detail'
err.hint = 'test hint'
err.code = '12345'
throw err
},
},
},
}),
mutation: new GraphQLObjectType({
Expand Down Expand Up @@ -106,6 +116,7 @@ for (const [name, createServerFromHandler] of Array.from(serverCreators)) {
createServerFromHandler(createPostGraphQLHttpRequestHandler(Object.assign({}, defaultOptions, options)))

describe(name, () => {

test('will 404 for route other than that specified', async () => {
const server1 = createServer()
const server2 = createServer({ graphqlRoute: '/x' })
Expand Down Expand Up @@ -389,6 +400,47 @@ for (const [name, createServerFromHandler] of Array.from(serverCreators)) {
)
})

test('will report a simple error in the default case', async () => {
pgPool.connect.mockClear()
pgClient.query.mockClear()
pgClient.release.mockClear()
const server = createServer()
await (
request(server)
.post('/graphql')
.send({ query: '{testError}' })
.expect(200)
.expect('Content-Type', /json/)
.expect({ data: { testError: null }, errors: [ {
message: 'test message',
locations: [{ line: 1, column: 2 }],
path: ['testError'],
} ] })
)
})

test('will report an extended error when extendedErrors is enabled', async () => {
pgPool.connect.mockClear()
pgClient.query.mockClear()
pgClient.release.mockClear()
const server = createServer({ extendedErrors: ['hint', 'detail', 'errcode'] })
await (
request(server)
.post('/graphql')
.send({ query: '{testError}' })
.expect(200)
.expect('Content-Type', /json/)
.expect({ data: { testError: null }, errors: [ {
message: 'test message',
locations: [{ line: 1, column: 2 }],
path: ['testError'],
hint: 'test hint',
detail: 'test detail',
errcode: '12345',
} ] })
)
})

test('will serve a favicon when graphiql is enabled', async () => {
const server1 = createServer({ graphiql: true })
const server2 = createServer({ graphiql: true, route: '/graphql' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
formatError as defaultFormatError,
print as printGraphql,
} from 'graphql'
import { extendedFormatError } from '../../graphql/utils/extendedFormatError'
import { extendedFormatError } from '../extendedFormatError'
import { $$pgClient } from '../../postgres/inventory/pgClientFromContext'
import renderGraphiQL from './renderGraphiQL'
import debugPgClient from './debugPgClient'
Expand Down

0 comments on commit f39b991

Please sign in to comment.