2016-03-27 36 views
0

我試圖從json配置動態生成graphql方案。但我無法爲自己創建一個GraphQLList。創建它時無法引用GraphQLObjectType

JSON:

{ 
    "label": "user", 
    "properties": [ 
    { 
     "key": "name", 
     "type": "string" 
    }, 
    { 
     "key": "id", 
     "type": "id" 
    }, 
    { 
     "key": "birthday", 
     "type": "date" 
    }, 
    { 
     "key": "gender", 
     "type": "string" 
    }, 
    { 
     key: 'friends', 
     type: 'string' 
    } 
    ] 
} 

的JavaScript代碼生成:

graphSchemes.forEach(function (graphScheme) { 
graphQLObjects[graphScheme.label] = new graphql.GraphQLObjectType({ 
    name: graphScheme.label, 
    fields: graphScheme.properties.reduce((fields, property) => { 
    if (property.key === 'friends') { 
     fields[property.key] = { 
     type: new graphql.GraphQLList(graphQLObjects[graphScheme.label]) 
     }; 
     return fields; 
    } 
    fields[property.key] = { 
     type: TYPES[property.type] 
    }; 
    return fields; 
    }, {}) 
}); 

});這裏

的問題是:

type: new graphql.GraphQLList(graphQLObjects[graphScheme.label]) 

沒有 「graphQLObjects [graphScheme.label]」

我如何去解決這個?有什麼建議麼?

回答

0

字段可能通過將字段放入包裝函數中來引用類型本身。

一個例子:

var routeType = new GraphQLObjectType({ 
    name: 'MessageRoute', 
    fields: function() { 
    return { 
     name: { 
     type: GraphQLString 
     }, 
     routes: { 
     type: new GraphQLList(routeType), 
     resolve: (route) => { 
      return route.routes; 
     } 
     } 
    }; 
    } 
});