question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resolver incompatibility with Postgraphile top-level Connections

See original GitHub issue

Hi!

I am trying to use the awesome graphql-shield library with Postgraphile. In order to do so, I must use graphql-middleware. After thoroughly narrowing in on the problem, I’ve arrived at the following base case:

Let’s say we have an item table in Postgres. Postgraphile generates a schema automatically, with AllItems and itemById fields. We send the schema through

applyMiddleware(schema, async (resolve, root, args, context, info) => {
  const resultingSchema = await resolve(root, args, context, info);
  return resultingSchema;
};)`

which is then returned to Postgraphile plugin system, which becomes the finalized schema.

I then execute a query like so:

query MyQuery {
     allItems {
          nodes {
              name
          }
     }
     itemByID(id: 5) {
          name
     }
}

The itemByID will execute properly and return the right data, but allItems does not:

  "stack": "TypeError: Cannot read property 'map' of undefined\n    
     at resolve ([redacted]/node_modules/graphile-build-pg/src/plugins/PgTablesPlugin.js:501:44)\n   
     at ([redacted]/node_modules/graphql-middleware/src/applicator.ts:35:9\n    
    [redacted irrelevant calls from application code/router]

It seems to be a problem with Connection type fields on the top level, which include nodes and edges arrays of objects (you can see nodes being queried in the allItems field). I did some experimentation, and it can even handle relational nested Connections inside itemByID if I declared them (let’s say relatedItems for example). But it appears to be improperly handling and traversing Connections specifically when they’re at the top level of the query, like allItems.

To simplify the Connections format, I even tried Postgraphile with the simple connections option instead of Relay-format connections. That gets rid of the nodes and edges array fields on Connections and so just returns an array immediately from allItems. But the resolving/traversal still remained a problem: They were now actually properly returning an array of objects but there were values of null for every object field in the object array, meaning the leaf scalar values didn’t appear to be resolved properly. So exchanging one problem for another in resolution.

After looking closely, the problem seems to lie somewhere in the applicator’s generateResolverFromSchemaAndMiddleware properly traversing the resolvers or when running the addResolversToSchema that generates a new schema for output.

It just seems like in the current state, that graphql-middleware is not compatible with Postgraphile’s generated schemas as inputs.

Any pointers, clues, or solutions you can think of? We are so excited to use the graphql-shield library, but this schema translation step seems to be broken.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

1reaction
pschmidtudigcommented, Jun 2, 2022

I found a workaround for this (somewhat by accident). If you add a step to first wrap the schema using @graphql-tools/wrap, you can apply the middleware as usual and graphql-shield works as expected.

I’ve tested this with postgraphile running on both Apollo Server (schema only) and Express (as library)

Example plugin:

const { makeProcessSchemaPlugin } = require("graphile-utils");
const permissions = require('../auth/permissions')
const { wrapSchema } = require('@graphql-tools/wrap')
const { applyMiddleware } = require('graphql-middleware')

module.exports = makeProcessSchemaPlugin(schema => {
    const wrappedSchema = wrapSchema({
        schema: schema,
        transforms: []
    });

return applyMiddleware(wrappedSchema, permissions);
});
0reactions
galvakojiscommented, Oct 28, 2021

I’m also having this issue. Has a workaround or any other insight been found?

I lost hope to get any attention to this, so instead using this library went to solution similar to this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolver incompatibility with Postgraphile top-level Connections
Hi! I am trying to use the awesome graphql-shield library with Postgraphile. In order to do so, I must use graphql-middleware.
Read more >
PostGraphile | makeExtendSchemaPlugin (graphile-utils)
Using makeExtendSchemaPlugin you can write a plugin that will merge additional GraphQL types and resolvers into your schema using a similar ...
Read more >
PostGraphile | Schema Plugins - Graphile Engine
The PostGraphile GraphQL schema is constructed out of a number of Graphile Engine plugins. The core PG-related plugins can be found here:.
Read more >
Using PostGraphile as a Library
Making HTTP data available to resolvers ... PostGraphileResponseNode - for Node, Express, Connect, Nest, Restify, and Fastify v2 (NOT v3) ...
Read more >
PostGraphile | Connections
When a GraphQL field is expected to return a large list of database records, we typically implement a connection adhering to the Relay...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found