Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optional argument ordering #194

Merged
merged 1 commit into from
Oct 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/postgraphql/__tests__/postgraphql-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,26 @@ test('will use a connected client from the pool, the schemas, and options to cre
expect(pgClient.release.mock.calls).toEqual([[]])
})

test('will use a connected client from the pool, the default schema, and options to create a GraphQL schema', async () => {
createPostGraphQLSchema.mockClear()
createPostGraphQLHttpRequestHandler.mockClear()
const pgPool = new Pool()
const options = Symbol('options')
const pgClient = { release: jest.fn() }
pgPool.connect.mockReturnValue(Promise.resolve(pgClient))
await postgraphql(pgPool, options)
expect(pgPool.connect.mock.calls).toEqual([[]])
expect(createPostGraphQLSchema.mock.calls).toEqual([[pgClient, ['public'], options]])
expect(pgClient.release.mock.calls).toEqual([[]])
})

test('will use a created GraphQL schema to create the HTTP request handler and pass down options', async () => {
createPostGraphQLHttpRequestHandler.mockClear()
const pgPool = new Pool()
const gqlSchema = Symbol('graphqlSchema')
const gqlSchema = Symbol('gqlSchema')
const options = { a: 1, b: 2, c: 3 }
createPostGraphQLSchema.mockReturnValueOnce(Promise.resolve(gqlSchema))
await postgraphql(pgPool, null, options)
await postgraphql(pgPool, [], options)
expect(createPostGraphQLHttpRequestHandler.mock.calls.length).toBe(1)
expect(createPostGraphQLHttpRequestHandler.mock.calls[0].length).toBe(1)
expect(Object.keys(createPostGraphQLHttpRequestHandler.mock.calls[0][0])).toEqual(['a', 'b', 'c', 'getGqlSchema', 'pgPool'])
Expand Down
59 changes: 43 additions & 16 deletions src/postgraphql/postgraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,57 @@ import createPostGraphQLSchema from './schema/createPostGraphQLSchema'
import createPostGraphQLHttpRequestHandler, { HttpRequestHandler } from './http/createPostGraphQLHttpRequestHandler'
import watchPgSchemas from './watch/watchPgSchemas'

type PostGraphQLOptions = {
classicIds?: boolean,
dynamicJson?: boolean,
graphqlRoute?: string,
graphiqlRoute?: string,
graphiql?: boolean,
pgDefaultRole?: string,
jwtSecret?: string,
jwtPgTypeIdentifier?: string,
watchPg?: boolean,
showErrorStack?: boolean,
disableQueryLog?: boolean,
disableDefaultMutations?: boolean,
enableCors?: boolean,
}

/**
* Creates a PostGraphQL Http request handler by first introspecting the
* database to get a GraphQL schema, and then using that to create the Http
* request handler.
*/
export default function postgraphql (poolOrConfig?: Pool | PoolConfig | string, schema?: string | Array<string>, options?: PostGraphQLOptions): HttpRequestHandler
export default function postgraphql (poolOrConfig?: Pool | PoolConfig | string, options?: PostGraphQLOptions): HttpRequestHandler
export default function postgraphql (
poolOrConfig?: Pool | PoolConfig | string,
schema: string | Array<string> = 'public',
options: {
classicIds?: boolean,
dynamicJson?: boolean,
graphqlRoute?: string,
graphiqlRoute?: string,
graphiql?: boolean,
pgDefaultRole?: string,
jwtSecret?: string,
jwtPgTypeIdentifier?: string,
watchPg?: boolean,
showErrorStack?: boolean,
disableQueryLog?: boolean,
disableDefaultMutations?: boolean,
enableCors?: boolean,
} = {},
schemaOrOptions?: string | Array<string> | PostGraphQLOptions,
maybeOptions?: PostGraphQLOptions,
): HttpRequestHandler {
let schema: string | Array<string>
let options: PostGraphQLOptions

// If the second argument is undefined, use defaults for both `schema` and
// `options`.
if (typeof schemaOrOptions === 'undefined') {
schema = 'public'
options = {}
}
// If the second argument is a string or array, it is the schemas so set the
// `schema` value and try to use the third argument (or a default) for
// `options`.
else if (typeof schemaOrOptions === 'string' || Array.isArray(schemaOrOptions)) {
schema = schemaOrOptions
options = maybeOptions || {}
}
// Otherwise the second argument is the options so set `schema` to the
// default and `options` to the second argument.
else {
schema = 'public'
options = schemaOrOptions
}

// Creates the Postgres schemas array.
const pgSchemas: Array<string> = Array.isArray(schema) ? schema : [schema]

Expand Down