PostGraphQL will provide GraphQL access automatically to your data stored in a PostgreSQL database.
Assuming we have PostgreSQL installed already with at least one database created but we don’t have Node yet – install Node Version Manager (nvm), the latest version of Node.js and PostGraphQL:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash nvm install nodeĀ npm install -g postgraphql
Assuming we have a database called my_database and a user my_database_usr, in one terminal run the PostGraphQL server:
postgraphql -c "postgres://my_database_usr:password@localhost:5432/my_database"
The server should be running on port 5000 on localhost.
Note: make sure you have a PostgreSQL version newer than 9.4 or else you will see this error:
error: function pg_catalog.pg_relation_is_updatable(oid, boolean) does not exist
In another terminal, send an HTTP POST request to see what data is available:
curl -X POST "http://localhost:5000/graphql" -H "Content-type: application/json" -d "{\"query\": \"{__schema{types{name}}}\"}"
The following example query assumes a table “books” with a field “title” in my_database. The cURL command is below:
curl -X POST "http://localhost:5000/graphql" -H "Content-type: application/json" -d "{\"query\": \"{allBooks{edges{node{title}}}}\"}"
In a browser you can also explore the data with GraphiQL at:
http://localhost:5000/graphiql