2016-03-14 65 views
5

我想設置一個與Relay兼容的GraphQL架構的node.js服務器。Edge.node字段類型必須是輸出類型,但得到:undefined

當試圖驗證或加載架構我得到以下錯誤:

EventEdge.node field type must be Output Type but got: undefined.

這是通過對在其他類型定義的一個事件類型的連接類型造成的。

我不會發布整個模式,因爲它非常詳細,但是當連接字段被註釋掉時,模式被正確加載並且不會引發錯誤。

我已經包含了簡化的架構,導致同一問題的例子:

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, 
     GraphQLSchema  = graphql.GraphQLSchema, 
     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 
    *  datetime: 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 Schema 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 Schema 
    * 
    * type Schema { 
    *  query: Query 
    *  mutation: Mutation 
    * } 
    */ 

    var Schema = new GraphQLSchema({ 
    query: QueryType 
    }) 


    module.exports = Schema 

回答

7

你分配給它之前做了一個參考EventType。首先定義類型,然後在連接中使用它:

/** 
* Event Type 
* 
* type Event : Object { 
*  id: String! 
*  title: String 
*  description: String 
*  datetime: 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], 
}); 

// Event Connection 
const eventsConnection = connectionDefinitions({ 
    name: 'Event', 
    nodeType: EventType, 
}); 
相關問題