Must call joinMonster in a resolver on a field where the type is decorated with \"sqlTable\"
See original GitHub issueDescription
I used join-monster to convert graphql query into SQL query to fetch date from postgres. I have implemented a small example with the help of join-monster docs to achieve it. But when i try to run the graphql query i get error ‘“Must call joinMonster in a resolver on a field where the type is decorated with “sqlTable”.”’
Reproduction
schema.js
const graphql = require(‘graphql’); var _ = require(‘lodash’); const { Client } = require(‘pg’); const joinMonster = require(‘join-monster’).default; const knex = require(‘knex’);
const client = new Client({ user: ‘postgres’, host: ‘us-east-1.rds.amazonaws.com’, database: ‘graphqldb’, password: ‘’, port: 5432 });
client.connect();
var q = “SELECT * FROM cgh_graphql_user_details”;
client.query(q, function(err, results){ if(err) throw err; console.log(“results are”,results); client.end(); });
const { GraphQLObjectType, GraphQLID, GraphQLString, GraphQLInt, GraphQLList, GraphQLSchema
} = graphql
const User = new GraphQLObjectType({ name: ‘User’, sqlTable: ‘cgh_graphql_user_details’, uniqueKey: ‘id’, fields: () => ({ id: { type: GraphQLInt, sqlColumn: ‘id’ }, name: { description: ‘A user's first and last name’, type: GraphQLString, sqlColumn: ‘name’ }, age: { description: ‘A user's age’, type: GraphQLInt, sqlColumn: ‘age’ } }) })
const QueryRoot = new GraphQLObjectType({ name: ‘Query’,
fields: () => ({
user: {
type: User,
args: {id:{type: GraphQLInt}},
// how to write the WHERE condition
where: (usersTable, args, context) => {
if (args.id) return ${usersTable}.id = ${args.id}
},
resolve: (parent, args, context, resolveInfo) => {
// resolve the user and the comments and any other descendants in a single request and return the data!
// all you need to pass is the resolveInfo
and a callback for querying the database
return joinMonster(resolveInfo, {}, sql => {
// knex is a query library for SQL databases
return knex.raw(sql)
})
}
}
})
})
module.exports = new GraphQLSchema({ query: QueryRoot })
Environment
Development
Please provide the following:
- Version of this library used: - v3.0.1
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top GitHub Comments
@koesper do you have
sqlTable
nested underextensions: { joinMonster: ... }}
?https://join-monster.readthedocs.io/en/stable/CHANGELOG/
sqlTable and uniqueKey apparently need to go into an extensions > joinMonster attribute: https://join-monster.readthedocs.io/en/latest/map-to-table/#table-name-details
Nice software, not nice examples. This change worked for me.