joinmonster 不是一个函数 - GraphQL

Posted

技术标签:

【中文标题】joinmonster 不是一个函数 - GraphQL【英文标题】:joinmonster is not a function - GraphQL 【发布时间】:2018-09-20 03:55:39 【问题描述】:

我正在使用带有 GraphQL 和 postgres 的库 join-monster,并将 GraphiQL 作为客户端。查询数据库时,出现错误:“joinMonster 不是函数”。 joinMonster() 方法由库提供,并在解析器中使用。

到数据库的连接是通过 knex 进行的,显然它可以工作。如果我运行以下代码,我会从表格中获取数据:

knex('students').then(rows => console.log(rows))

Database diagram

GraphiQL outpup

这是架构解析器代码:

const joinMonster = require('join-monster');

const knex = require('knex')(
    client: 'postgres',
    connection: 
    host: 'localhost',
    user: 'postgres',
    password: 'myPassword',
    database: 'test'
  
);

const  graphQLSchema  = require("graphql");

const 
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLList,
  GraphQLNonNull,
  GraphQL
 = require('graphql');

const Subject = new GraphQLObjectType(
  name: "Subject",
  sqlTable: 'subjects',
  uniqueKey: 'id',
  fields: () => (
    id: 
      type: GraphQLInt
    ,
    name: 
      type: GraphQLString
    ,
    idEncoded: 
      description: 'The ID base-64 encoded',
      type: GraphQLString,
      sqlColumn: 'id',
      // specifies SQL column and applies a custom resolver
      resolve: user => toBase64(user.idEncoded)
    ,
    teacher: 
      type: GraphQLString
    ,
    students: 
      type: new GraphQLList(Student),
      junction: 
        sqlTable: 'class',
        sqlJoins: [
          (subjectTable, junctionTable, args) => `$subjectTable.id = $junctionTable.subject_id`,
          (junctionTable, studentTable, args) => `$junctionTable.student_id = $studentTable.id`
        ]
      
    
  )
);

const Student = new GraphQLObjectType(
  name: "Student",
  sqlTable: 'students',
  uniqueKey: 'id',
  fields: () => (
    id: 
      type: GraphQLInt
    ,
    name: 
      type: GraphQLString
    ,
    idEncoded: 
      description: 'The ID base-64 encoded',
      type: GraphQLString,
      sqlColumn: 'id',
      resolve: user => toBase64(user.idEncoded)
    ,
    lastname: 
      type: GraphQLString
    ,
    subjects: 
      type: new GraphQLList(Subject),
      junction: 
        sqlTable: 'class',
        sqlJoins: [
          (studentTable, junctionTable, args) => `$studentTable.id = $junctionTable.student_id`,
          (junctionTable, subjectTable, args) => `$junctionTable.subject_id = $subjectTable.id`
        ]
      
    
  )
);

const QueryRoot = new GraphQLObjectType(
  name: 'Query',
  fields: () => (
    student: 
      type: Student,
      args: 
        id: 
          type: GraphQLInt
        
      ,
      where: (studentsTable, args, context) => 
        if (args.id) return `$studentsTable.id = $args.id`
      ,
      resolve: (parent, args, context, resolveInfo) => 
        return joinMonster(resolveInfo, , sql => 
          return knex.raw(sql)
        )
      
    ,

    subject: 
      type: Subject,
      args: 
        id: 
          type: GraphQLInt
        
      ,
      where: (subjectsTable, args, context) => 
        if (args.id) return `$subjectsTable.id = $args.id`
      ,
      resolve: (parent, args, context, resolveInfo) => 
        return joinMonster(resolveInfo, , sql => 
          return knex.raw(sql)
        )
      
    
  )
)


const schema = new GraphQLSchema(
  query: QueryRoot,
);

module.exports = schema;

function toBase64(clear) 
  return Buffer.from(String(clear)).toString('base64')

我已按照https://join-monster.readthedocs.io/ 的文档进行操作

谢谢

【问题讨论】:

你用 npm 安装包了吗? 是:npm install --save join-monster 【参考方案1】:

你也可以这样导入函数:

const joinMonster = require('join-monster').default;

【讨论】:

【参考方案2】:

问题在于,当使库对文件可用时,提供的是对象而不是函数。

const joinMonster = require('join-monster'); 
console.log(joinMonster)

// output
 default:  [Function: joinMonster] getNode: [Function: getNode], version: '2.0.16'  

我不知道为什么提供对象而不是函数。但 现在,我调用了 joinMonster.default,它可以工作了:

resolve: (parent, args, context, resolveInfo) =>  
        return joinMonster.default(resolveInfo, , sql => 
          return knex.raw(sql)
        )

【讨论】:

以上是关于joinmonster 不是一个函数 - GraphQL的主要内容,如果未能解决你的问题,请参考以下文章

delphi回调函数

自定义ImageView实现图片圆角效果

无法从 Win32 C++ GDI+ 中的函数返回图形对象

阅读《Android 从入门到精通》(30)——字体

从 DynamoDB 更改中反应应用程序更新数据

网站接入QQ登录注意事项