Resolver incompatibility with Postgraphile top-level Connections
See original GitHub issueHi!
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:
- Created 2 years ago
- Reactions:3
- Comments:7
Top GitHub Comments
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:
I lost hope to get any attention to this, so instead using this library went to solution similar to this