Postgraphile: Third Party API Integration

Created on 3 Jun 2017  ยท  5Comments  ยท  Source: graphile/postgraphile

I'm very new with postgraphql and up to now it's awesome to create database abstractions with graphql. But how to handle third parties ? I mean, what is the best approach for third party integration like Stripe or any other API's?

๐Ÿ‘จโ€๐Ÿ’ป Fix in v4

Most helpful comment

This is easier than you think.

Build a NodeJS Express API that handles endpoints specifically for your extra requests. Then, use a catch-all endpoint and relay requests/responses to/from Postgraph on the same server. Node does what Postgraph can't, Postgraph does what you don't want to do in node. Problem solved.

All 5 comments

Hi! Until schema injection in #448 is done / merged, there currently isn't a way to make external API calls within PostGraphQL. Instead, any interaction with the outside world has to be done with Postgres directly. You have a few options there -

1) LISTEN / NOTIFY - you can have a script running in the background listening for when certain events occur. This would be useful for sending emails, or other actions that don't need to occur within a single request. Lots of slick helper libraries available for this like node-pg-pubsub and trunk.

2) Write your mutation functions in something like PL/Python and make whatever API calls you need. The issues with this approach though is that it puts additional work onto the database, it'd be difficult to debug, and if you're using a hosted Postgres database like Amazon RDS this just isn't an option at all.

3) Use a foreign data wrapper in combination with something like Multicorn. However, this suffers from the same limitations as 2.

Good luck!

This is easier than you think.

Build a NodeJS Express API that handles endpoints specifically for your extra requests. Then, use a catch-all endpoint and relay requests/responses to/from Postgraph on the same server. Node does what Postgraph can't, Postgraph does what you don't want to do in node. Problem solved.

Even better, check out https://github.com/postgraphql/postgraphql/blob/master/docs/library.md

Running postgraph as a library lets you embed postgraphql into your API, so you can build out around it. You now have a /graphql endpoint and then the rest of your endpoints for whatever.

@chadfurman ๐Ÿ‘ ๐Ÿ‘ ๐Ÿ‘

I'm not sure why using PostGraphQL as a library and having separate REST endpoints didn't occur to me sooner, but that would be a super easy solution for an external API integration piece I've been working on. Thanks for suggesting that.

EDIT: Where this approach falls short though is if you need to actually add data to your GraphQL results, of course.

It will be possible in PostGraphQL v4 to extend the schema with arbitrary plugins, I don't see this functionality being added in v3 so I'm going to close this to keep things tidy.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marshall007 picture marshall007  ยท  3Comments

safaiyeh picture safaiyeh  ยท  3Comments

5argon picture 5argon  ยท  4Comments

tazsingh picture tazsingh  ยท  3Comments

giacomorebonato picture giacomorebonato  ยท  3Comments