Created
March 14, 2022 14:40
-
-
Save EyMaddis/e292978c923080d95fae045770da1cf3 to your computer and use it in GitHub Desktop.
Postgraphile as a Next.js API
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
import { NextApiRequest, NextApiResponse } from 'next' | |
import { middleware } from '../../src/graphile/postgraphileMiddleware.js' | |
export default async function(req: NextApiRequest, res: NextApiResponse) { | |
return new Promise((resolve, reject) => { | |
middleware(req, res) | |
res.once('finish', () => resolve(true)) | |
res.once('error', e => reject(e)) | |
}) | |
} | |
export const config = { | |
api: { | |
bodyParser: false, | |
}, | |
} |
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
import PgSimplifyInflectorPlugin from '@graphile-contrib/pg-simplify-inflector' | |
import { postgraphile } from 'postgraphile' | |
const sqlClient = new pg.Pool({ | |
connectionString: DATABASE_URL, | |
// other settings | |
}) | |
export const middleware = postgraphile(sqlClient, 'public', { | |
graphiql: process.env.NODE_ENV !== 'production', | |
appendPlugins: [ | |
PgSimplifyInflectorPlugin, | |
// ... your plugins | |
], | |
graphqlRoute: '/api/graphql', | |
graphiqlRoute: '/api/graphiql', | |
showErrorStack: true, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment