简体   繁体   中英

Cannot read property 'kind' of undefined at isDocumentNode while export makeExecutableSchema using Apollo Graphql on express js

I have created modular of schema like this: User.js

const gqTools = require('graphql-tools');

const user = `
  type User {
    emailId: String
    name: String
    type: Int
    created_on: String
  }
`;

module.exports = user

and this is the UserPayload that has related by the user type schema

const gqTools = require('graphql-tools');
const User = require('./User')

const userPayload = `
  enum ErrorCodes {
    UNAUTHORIZED
    INTERNAL_ERROR
  }
  type Error {
    code: ErrorCodes
    msg: String
  }
  type UserPayload {
    user: [User]
    error: Error
  }
`;

module.exports = () => [userPayload, User]

this is the Type Query of user that i created

const gqTools = require('graphql-tools');
const UserPayload = require('../UserPayload')

const query = `
  type Query {
    checkAuthorized: [UserPayload]

  }
`;

module.exports = () => [query, UserPayload]

this is the resolver i created

const checkAuthorizedResolver = {
  async checkAuthorized(root, data, context) {
    if (context.user.length === 0) {
        return {
            user: null,
            error: {
                code: 'UNAUTHORIZED',
                msg: 'UNAUTHORIZED'
            }
        }
    }

    let s = `
        select email, name, usertype_id, created_at
        from users
        where id = ${context.user[0].id}
    `

    try {
        let result = await context.db.run(s)
        return {
            user: {
                emailId: result[0].email,
                name: result[0].name,
                type: result[0].usertype_id,
                created_on: result[0].created_at
            },
            error: null
        }
    } catch (err) {
        console.log(err)
        return {
            user: null,
            error: {
                code: 'UNAUTHORIZED',
                msg: 'UNAUTHORIZED'
            }
        }
    }
  }
}

module.exports = checkAuthorizedResolver

after it i create the main.js on schema folder that will be import on server.js

const gqTools = require('graphql-tools');
const jwt = require('jsonwebtoken')
const { makeExecutableSchema } = require('graphql-tools')
const moment = require('moment')

const {
  checkAuthorized,
} = require('../schema/type_Query/type_Query')

const checkAuthorizedResolver = require ('../Controller/checkAuthorized')

module.exports = makeExecutableSchema({
  typeDefs: [checkAuthorized],
  resolvers:{
      checkAuthorizedResolver
  }
})

after i run the program it show the error msg, i dont understand what is mean of "kind" on the error msg because i never declare variable "kind", and i know it is from node module library. may everyone help me what is mean of that error and how to write the syntax on "makeExecutableSchema", because i have follow the documentation from GrahQL Tools for modularization

this is the error msg:

> TypeError: Cannot read property 'kind' of undefined
>     at isDocumentNode (/home/hari/Documents/kerjaan/nest8gg/kpi/node_modules/graphql-tools/src/schemaGener
> ator.ts:134:42)
>     at /home/hari/Documents/kerjaan/nest8gg/kpi/node_modules/graphql-tools/src/schemaGenerator.ts:151:9
>     at Array.forEach (<anonymous>)
>     at concatenateTypeDefs (/home/hari/Documents/kerjaan/nest8gg/kpi/node_modules/graphql-tools/src/schema
> Generator.ts:150:22)
>     at buildSchemaFromTypeDefinitions (/home/hari/Documents/kerjaan/nest8gg/kpi/node_modules/graphql-tools
> /src/schemaGenerator.ts:190:21)
>     at _generateSchema (/home/hari/Documents/kerjaan/nest8gg/kpi/node_modules/graphql-tools/src/schemaGene
> rator.ts:80:18)
>     at makeExecutableSchema (/home/hari/Documents/kerjaan/nest8gg/kpi/node_modules/graphql-tools/src/schem
> aGenerator.ts:109:20)
>     at Object.<anonymous> (/home/hari/Documents/kerjaan/nest8gg/kpi/server/schema/main.js:41:18)
>     at Module._compile (module.js:643:30)
>     at Module._compile (/home/hari/Documents/kerjaan/nest8gg/kpi/node_modules/source-map-support/source-ma
> p-support.js:492:25)

Most likely you pass a undefined as type, for example, User or UserPayload :

module.exports = () => [userPayload, User]

module.exports = () => [query, UserPayload]

You can log them out and make sure they have valid values:

console.log(User);
module.exports = () => [userPayload, User]

console.log(UserPayload);
module.exports = () => [query, UserPayload]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM