Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – Edge.node字段类型必须是输出类型,但得到:undefined大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用与Relay兼容的GraphQL架构设置node.js服务器.

尝试验证或加载架构时,我收到以下错误

EventEdge.node字段类型必须是输出类型,但得到:undefined.

这是由于在其他类型定义之一中为Event类型提供了连接类型.

我不会发布整个架构,因为它非常详细,但是当连接字段被注释掉时,架构被正确加载并且不会抛出任何错误.

我已经包含了导致相同问题的简化模式的示例:

const graphql       = require('graphql')
  const relay         = require('graphql-relay')

  const GraphQLID         = graphql.GraphQLID,GraphQLInt        = graphql.GraphQLInt,GraphQLString     = graphql.GraphQLString,GraphQLList       = graphql.GraphQLList,GraphQLObjectType = graphql.GraphQLObjectType,GraphQLscheR_651_11845@a     = graphql.GraphQLscheR_651_11845@a,GraphQLNonNull    = graphql.GraphQLNonNull

  const connectionArgs               = relay.connectionArgs,connectionDeFinitions        = relay.connectionDeFinitions,connectionFromArray          = relay.connectionFromArray,cursorForObjecTinConnection  = relay.cursorForObjecTinConnection,fromGlobalId                 = relay.fromGlobalId,globalIdField                = relay.globalIdField,mutationWithClientMutationId = relay.mutationWithClientMutationId,nodeDeFinitions              = relay.nodeDeFinitions,toGlobalId                   = relay.toGlobalId

  // Models (ORM Mappings)
  const models = require('../models')

  // Model handlers
  const handlers = require('../models/handlers')


  /*
   *  Relay Node DeFinition:
   *  {nodeInterface,nodeFielD} = nodeDeFinitions
   */

  var nodeDeFinition = nodeDeFinitions(
    (globalId) => {
      // {type,iD} = fromGlobalId(globalId)
      const gid = fromGlobalId(globalId);

      switch (gid.typE) {
      case 'User':
        return handlers.getUser(id)

      // case 'Event':
      //   return handlers.getEvent(id)
      //
      // case 'Club':
      //   return handlers.getClub(id)

      default:
        return null

      }
    },(obj) => {
      switch (true) {
        case (obj instanceof modelS.User):
          return UserType

        // case (obj instanceof models.Club):
        //   return ClubType
        //
        // case (obj instanceof models.Event):
        //   return EventType

        default:
          return null
      }
    }
  )

  /**************************
   **************************
   * Relay Connections
   *
   * { connectionType,edgeType } = connectionDeFinitions({nodeType: LoremTypE})
   **************************
   **************************/

   // User Connection
   // const usersConnection = connectionDeFinitions({name: 'User',nodeType: UserTypE})

   // Event Connection
   const eventsConnection = connectionDeFinitions({name: 'Event',nodeType: EventTypE})

   // Club Connection
   // const clubsConnection = connectionDeFinitions({name: 'Club',nodeType: ClubTypE})


   /**************************
    **************************
    * GraphQL Type DeFinitions
    **************************
    **************************/

    /*
    *   User Type
    *
    *   type User : Object {
    *     id: String!
    *     first_name: String
    *     last_name: String
    *     friends: [User]
    *   }
    */

   var UserType = new GraphQLObjectType({
     name: 'User',description: 'A user of the app.',fields: () => ({
       id: globalIdField('User'),events: {
         type: eventsConnection.connectionType,description: 'User\'s events.',args: connectionArgs,resolve: (user,args) => connectionFromArray(getEvents(),args)
       }
     }),interfaces: [nodeDeFinition.nodeInterface]
   })


   /*
   **  Event Type
   *
   *   type Event : Object {
   *     id: String!
   *     title: String
   *     description: String
   *     datetiR_651_11845@e: Int
   *     LOCATIOn: [Int]
   *     managers: [User]
   *     club: Club
   *     members: [User]
   *   }
   */

  var EventType = new GraphQLObjectType({
    name: 'Event',description: 'An event in the app.',fields: () => ({
      id: globalIdField('Event'),name: {
        type: GraphQLString,description: 'Event\'s name.',resolve: event => event.get('name')
      }
    }),interfaces: [nodeDeFinition.nodeInterface]
  })


  /****************************
   ****************************
   * Relay Mutation DeFinitions
   ****************************
   ****************************/


  /**************************
   **************************
   * Root scheR_651_11845@a DeFinitions
   **************************
   **************************/

  /*
  **  Root Query
  *
  *   type Query {
  *     user(id: String!): User
  *     club(id: String!): Club
  *     event(id: String!): Event
  *   }
  */

  var QueryType = new GraphQLObjectType({
    name: 'Query',fields: () => ({
      node: nodeDeFinition.nodeField,user: {
        type: UserType,resolve: () => handlers.getUser()
      }
    })
  })


  /*
  **  Root scheR_651_11845@a
  *
  *   type scheR_651_11845@a {
  *     query: Query
  *     mutation: Mutation
  *   }
  */

  var scheR_651_11845@a = new GraphQLscheR_651_11845@a({
    query: QueryType
  })


  module.exports = scheR_651_11845@a@H_489_18@

解决方法

在分配EventType之前,您已经引用了EventType.首先定义类型,然后在连接中使用它:

/**
 *   Event Type
 *
 *   type Event : Object {
 *     id: String!
 *     title: String
 *     description: String
 *     datetiR_651_11845@e: Int
 *     LOCATIOn: [Int]
 *     managers: [User]
 *     club: Club
 *     members: [User]
 *   }
 */

var EventType = new GraphQLObjectType({
  name: 'Event',fields: () => ({
    id: globalIdField('Event'),name: {
      type: GraphQLString,resolve: event => event.get('name')
    },}),interfaces: [nodeDeFinition.nodeInterface],});

// Event Connection
const eventsConnection = connectionDeFinitions({
  name: 'Event',nodeType: EventType,});@H_489_18@

大佬总结

以上是大佬教程为你收集整理的node.js – Edge.node字段类型必须是输出类型,但得到:undefined全部内容,希望文章能够帮你解决node.js – Edge.node字段类型必须是输出类型,但得到:undefined所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。