Postgraphile: How to invoke the scheme generated by postgraphile

Created on 26 Mar 2019  Ā·  5Comments  Ā·  Source: graphile/postgraphile

I want to generate something like this but I do not know which side to call the graphql scheme that generates postgraphile

app.use(postgraphile(pgConnectionString, pgSchemas, pgOptions));
app.get("/foo", (req, res) => {
// get a pg query / pg client from the postgraphile middleware
// make some query
const result = await pgQuery.query(
"SELECT ...."
);
// do something with result
});

ā” question

Most helpful comment

Here’s a couple places you can read about it:

All 5 comments

Hi Carl,

I'm a little confused by your question, so there's two answers:

I want to share the PgPool between postgraphile and my own code:

const pg = require('pg');
const postgraphile = require('postgraphile');

const pool = new pg.Pool({connectionString});

app.use(postgraphile(pool, pgSchemas, pgOptions));
app.get("/foo", (req, res) => {
  const result = await pool.query(
    "SELECT ...."
  );
});

I want to get the GraphQL schema from postgraphile and call it from my own code:


const pool = new pg.Pool({connectionString});

// ref: https://www.graphile.org/postgraphile/usage-schema/
export async function performQuery(
  schema,
  query,
  variables,
  jwtToken,
  operationName
) {
  return await withPostGraphileContext(
    {
      pgPool: pool,
      jwtToken: jwtToken,
      jwtSecret: "...",
      pgDefaultRole: "..."
    },
    async context => {
      // Execute your GraphQL query in this function with the provided
      // `context` object, which should NOT be used outside of this
      // function.
      return await graphql(
        schema, // The schema from `createPostGraphileSchema`
        query,
        null,
        { ...context }, // You can add more to context if you like
        variables,
        operationName
      );
    }
  );
}
const middleware = postgraphile(pool, pgSchemas, pgOptions);
app.use(middleware);
app.get("/foo", (req, res) => {
  const schema = await middleware.getGqlSchema();
  const result = await performQuery(schema, '{__typename}');

});

[semi-automated message] Thanks for your question; hopefully we're well on the way to helping you solve your issue. This doesn't currently seem to be a bug in the library so I'm going to close the issue, but please feel free to keep requesting help below and if it does turn out to be a bug we can definitely re-open it šŸ‘

@benjie what is "operationName"? It is not defined anywhere in the docs.

Here’s a couple places you can read about it:

Thank you, it has helped me a lot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CarlFMateus picture CarlFMateus  Ā·  4Comments

marshall007 picture marshall007  Ā·  3Comments

giacomorebonato picture giacomorebonato  Ā·  3Comments

tazsingh picture tazsingh  Ā·  3Comments

jayp picture jayp  Ā·  3Comments