I'm working on a project using the Serverless framework, and I'm wondering how to integrate with postgraphql.
There's really no "booting up" for each Lambda, so it's not like I'd use PM2 to keep postgraphql running. The lifecycle would be more like,
Any pointers?
Thanks!
This is an interesting case. PostGraphQL needs a “boot” step currently to introspect a PostgreSQL schema, so it can’t currently be run statelessly. However, in the future, we’re moving to a single SQL query approach to introspecting PostgreSQL. At that point the inspection result is just data and not some awkward form that isn’t easily serializable. In the future then we may be able to do the following (either through bash like below or an equivalent Node.js API):
postgraphql file://path/to/schema/file.json
(vs postgraphql postgres://localhost:5432…)
There will always be some startup tax as we generate the appropriate GraphQL objects, however. Whether this is more or less then the startup tax from calling new GraphQLObjectType yourself is yet to be seen.
I’ve yet to play around more with Serverless and Lambda, but I love the idea of GraphQL cloud “functions.” However, I’m not sure if I’d recommend PostGraphQL for a cloud function model or any other app that needs to do some housekeeping on start.
Hm. I think it might be reasonable to periodically cache our schema in a static *.json file.
If we were to do this in a fork of PostGraphQL, can you point out where I could substitute this file in place of the introspection query?
(Or are you saying that it's not easily serializable and wouldn't really be an option until you move to the single SQL query approach?)
Thanks for your help, @calebmer
It probably can’t be serialized easily until we move to the single query approach. It’s likely _possible_ though if you want to give it a shot. Check out the two files getCatalog.js and more importantly to you catalog.js (the latter is what you’d want to serialize).
Cool -- thank you. Will take a look.
Where can I get details to use postgraphql with AWS Lambda?
@javatask with the PostGraphQL 2.0 upgrade, this should be a lot easier, but would require a PR. All that would be involved is running the introspection SQL query beforehand and construct a PgCatalog from that replacing the code where we connect a Postgres client to run the introspection.
Thanks!
I'll try and will write article on medium
@javatask, @marclar any updates? Did you manage to do it?
@rentrop: here's an example of Postgraphile running on AWS Lambda: https://github.com/graphile/postgraphile-lambda-example
(Version 4 has writeCache/readCache functionality which removes the need for introspection on startup; however booting the PostGraphile code still takes a few seconds. Improvements are coming to this.)
Most helpful comment
@javatask with the PostGraphQL 2.0 upgrade, this should be a lot easier, but would require a PR. All that would be involved is running the introspection SQL query beforehand and construct a
PgCatalogfrom that replacing the code where we connect a Postgres client to run the introspection.