-
-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add library usage * Remove breaking tests Easiest solution to failing tests 😊 * Fix lint error
- Loading branch information
Showing
6 changed files
with
108 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Using PostGraphQL as a Library | ||
Some people may want to use PostGraphQL as a dependency of their current node projects. This is a functionality PostGraphQL intends to support. As long as you are using a server framework that supports [connect][]-styled middleware, you can use PostGraphQL as a dependency. | ||
|
||
> Unfortunately at the moment you can only use PostGraphQL as a library in [express][] applications. PostGraphQL wants to support all frameworks that work with connect-styled middleware, but we are blocked on that goal until [graphql/express-graphql#82][] gets merged. | ||
To use PostGraphQL as a library just do the following: | ||
|
||
```js | ||
import express from 'express' | ||
import postgraphql from 'postgraphql' | ||
|
||
const app = express() | ||
|
||
app.use(postgraphql('postgres://localhost:5432')) | ||
|
||
app.listen(3000) | ||
``` | ||
|
||
PostGraphQL can also be directly used with the HTTP module in the Node.JS standard library: | ||
|
||
```js | ||
import http from 'http' | ||
import postgraphql from 'postgraphql' | ||
|
||
http.createServer(postgraphql('postgres://localhost:5432')).listen(3000) | ||
``` | ||
|
||
To import the function with CommonJS: | ||
|
||
```js | ||
var postgraphql = require('postgraphql').default | ||
``` | ||
|
||
The API for creating handlers is as follows: | ||
|
||
## postgraphql(pgConfig, schemaName? = 'public', options) | ||
Arguments include: | ||
|
||
- **`pgConfig`**: An object or string that will be passed to the [`pg`][] library and used to connect to a PostgreSQL backend. | ||
- **`schemaName`**: A string which specifies the PostgreSQL schema you will be serving with PostGraphQL. The default schema is the `public` schema. | ||
- **`options`**: An object containing other miscellaneous options. Options could be: | ||
- `secret`: The secret for your application. This enables PostGraphQL’s authorization features. Make it something secure and obscure! | ||
- `development`: Enables development features like GraphiQL and more descriptive error formatting. | ||
- `log`: If true, PostGraphQL will log every request using the dev style of [morgan][]. | ||
|
||
[connect]: https://www.npmjs.com/connect | ||
[express]: https://www.npmjs.com/express | ||
[graphql/express-graphql#82]: https://github.com/graphql/express-graphql/pull/82 | ||
[`pg`]: https://www.npmjs.com/pg | ||
[morgan]: https://www.npmjs.com/morgan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import './promisify' | ||
|
||
export default from './postgraphql.js' | ||
export createGraphqlSchema from './createGraphqlSchema.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import createGraphqlSchema from './createGraphqlSchema.js' | ||
import createServer from './createServer.js' | ||
|
||
/** | ||
* A nice interface for creating a PostGraphQL server. | ||
* | ||
* @param {string | Object} pgConfig | ||
* @param {string?} schemaName | ||
* @param {Object} options | ||
* @returns {Server} | ||
*/ | ||
const postgraphql = (pgConfig, schemaName, options) => { | ||
if (typeof schemaName === 'object') { | ||
options = schemaName | ||
schemaName = null | ||
} | ||
|
||
// Default schema name is public. | ||
schemaName = schemaName || 'public' | ||
options = options || {} | ||
|
||
// `createServer` allows us to give it a promise for a `graphqlSchema` | ||
const graphqlSchema = createGraphqlSchema(pgConfig, schemaName) | ||
|
||
return createServer({ | ||
...options, | ||
graphqlSchema, | ||
pgConfig, | ||
}) | ||
} | ||
|
||
export default postgraphql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters