I am trying to get postgraphile API working in a g...
# general
s
I am trying to get postgraphile API working in a google cloud function that connects to the postgresql cloud sql. Any reason why connection name is being passed in as
undefined
?
Copy code
let greetingFunction = new gcpFunction.HttpFunction(
  "greeting", 
  postgraphile(
    {
      user: process.env.DB_USERNAME,
      host: `/cloudsql/${process.env.CONNECTION_NAME}`,
      database: process.env.DB_NAME,
      password: process.env.DB_PASSWORD,
      port: 5432,
    }, 
    {
      graphiql: true,
    }
  ), 
  {},
  // pass params into function as env vars
  {
    CONNECTION_NAME: sql.instance.connectionName,
    DB_USERNAME: sql.user.name,
    DB_PASSWORD: sql.user.password,
    DB_NAME: sql.database.name
  }
);
running that returns the following error:
Copy code
Error: connect ENOENT /cloudsql/undefined/.s.PGSQL.5432
instead of using env variable I passed host in as a string and got this error:
Copy code
Error: connect ENOENT /cloudsql/engaged-arcanum-197821:us-central1:fleetgrid-sql-ff25239/.s.PGSQL.5432
w
I can’t quite follow the whole repro case here. How are you standing up the sql DB? What do you see if you export the db’s ‘connectionName`?
s
``` ```
Copy code
import * as gcp from "@pulumi/gcp"
import * as config from "./config"
import * as pulumi from "@pulumi/pulumi"

// Provision a database instance for our node.js app.
export const instance = new gcp.sql.DatabaseInstance("fleetgrid-sql", {
  databaseVersion: "POSTGRES_9_6",
  settings: {
    tier: "db-f1-micro",
    ipConfiguration: {
      authorizedNetworks: [{ value: "0.0.0.0/0" }],
    },
  },
});

// Create a user with the configured credentials for the node.js app to use.
export const user = new gcp.sql.User("fleetgrid-sql-user", {
  instance: instance.name,
  name: config.dbUsername,
  password: config.dbPassword,
});

// Create a database for our node.js app.
export const database = new gcp.sql.Database("fleetgrid-sql-database", {
  instance: instance.name,
  name: config.dbName
});

// export const host = pulumi
//   .all([ instance.connectionName ])
//   .apply(
//     ([ name ]) => {
//       return `/cloudsql/${name}`
//     }
//   )

export const host = instance.connectionName.apply(x => `/cloudsql/${x}`)
Copy code
import * as config from "./config"
import * as sql from "./sql"
import * as gcpFunction from "./gcpFunction"
import * as pulumi from "@pulumi/pulumi"
import postgraphile from 'postgraphile'

// API from /cloudsql/engaged-arcanum-197821:us-central1:fleetgrid-sql-ff25239!
// let apiFunction = new gcpFunction.HttpFunction("api", (req, res) => {
//   res.send(`API from ${process.env.PG_HOST}!!!`);
// }, {}, {
//   PG_HOST: sql.host,
// });

// `postgres://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@/cloudsql/${process.env.CONNECTION_NAME}/.s.PGSQL.5432/${process.env.DB_NAME}`
let apiFunction = new gcpFunction.HttpFunction(
  "api", 
  postgraphile(
    {
      user: process.env.PG_USER,
      host: '/cloudsql/engaged-arcanum-197821:us-central1:fleetgrid-sql-ff25239', // process.env.PG_HOST,
      database: process.env.PG_DATABASE,
      password: process.env.PG_PASSWORD,
      port: 5432,
    }, 
    {
      graphiql: true,
    }
  ), 
  {},
  // pass params into function as env vars
  {
    PG_HOST: sql.host,
    PG_USER: sql.user.name,
    PG_PASSWORD: sql.user.password,
    PG_DATABASE: sql.database.name
  }
);

export let url = apiFunction.httpsTriggerUrl;
Logging into the google cloud console I can see the database created. Also, if I uncomment out the first apiFunction it spits out the proper string... https://us-central1-engaged-arcanum-197821.cloudfunctions.net/api-function-4565d32
I was able to connect by using the cloud sql public IP address and hard coding the connection string like this...
Copy code
let apiFunction = new gcpFunction.HttpFunction(
  "api", 
  postgraphile(
    '<postgres://fleetgrid:ELa8ETvv32wcjH@35.224.119.123:5432/fleetgrid>', // process.env.CONNECTION_STRING,
    'fg',
    {
      // graphqlRoute: '/',
      graphiql: true,
    }
  ),
  {},
  {
    CONNECTION_STRING: sql.connectionString,
  }
);
if I try to use the env variable instead of hard coded string by using it like this...
process.env.CONNECTION_STRING
then I get this error:
Copy code
Error: connect ECONNREFUSED 127.0.0.1:5432
Any idea why variables being passed in aren't working? here is what sql.connection looks like:
Copy code
export const connectionString = pulumi
  .all([ user.name, user.password, instance.ipAddresses, database.name ])
  .apply(
    ([ name, password, host, database ]) => {
      return `postgres://${name}:${password}@${host[0].ipAddress}:5432/${database}`
    }
  )