Skip to content

Instantly share code, notes, and snippets.

@EyMaddis
Created March 14, 2022 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EyMaddis/e292978c923080d95fae045770da1cf3 to your computer and use it in GitHub Desktop.
Save EyMaddis/e292978c923080d95fae045770da1cf3 to your computer and use it in GitHub Desktop.
Postgraphile as a Next.js API
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,
},
}
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