2017-11-20 8 views
0

私はGrpahQLで新しく、ユーザーとグループの多対多の関係をシミュレートしようとしています。私は自分のスキーマで定義されfollowinf種類があります。多対多graphqlスキーマエラー

// UserType.js 
const { 
    GraphQLObjectType, 
    GraphQLString, 
    GraphQLList, 
    GraphQLID } = require('graphql'); 

const { 
    GraphQLEmail } = require('graphql-custom-types'); 

const GroupType = require('./GroupType'); const AuthService = require('../../services/AuthService'); 

let authService = new AuthService(); 

const UserType = new GraphQLObjectType({ 
    name: "UserType", 
    fields:() => ({ 
     id: { type: GraphQLID }, 
     user: { type: GraphQLString }, 
     password: { type: GraphQLString }, 
     name: { type: GraphQLString }, 
     lastname: { type: GraphQLString }, 
     email: { type: GraphQLEmail }, 
     groups: { 
      type: new GraphQLList(GroupType), 
      resolve(parentValue) { 
       return authService.userGroups(userId); 
      } 
     } 
    }) }); 


module.exports = UserType; 

を、これは他のファイルです。何らかの理由で、私はこのエラーを得たので私のために動作しません。この例

// GroupType.js 
const { 
    GraphQLObjectType, 
    GraphQLString, 
    GraphQLID, 
    GraphQLList 
} = require('graphql'); 

const UserType = require('./UserType'); 
const AuthService = require('../../services/AuthService'); 

let authService = new AuthService(); 


const GroupType = new GraphQLObjectType({ 
    name: "GroupType", 
    fields:() => ({ 
     id: { type: GraphQLID }, 
     name: { type: GraphQLString }, 
     description: { type: GraphQLString }, 
     users: { 
      type: new GraphQLList(UserType), 
      resolve(parentArgs) { 
       return authService.userGroups(parentArgs.id); 
      } 
     } 
    }) 
}); 

module.exports = GroupType; 

Error: Can only create List of a GraphQLType but got: [object Object].

このエラーはGroupTypeに対してのみ発生し、両方が同等の場合にUserTypeに対しては発生しません。何が起きてる?私は間違って何をしていますか?

答えて

0

問題はUserTypeGroupTypeを必要とすることである、とGroupTypeUserTypeを必要とする:これは、円形の依存性として知られています。

何が起こるかというと、UserType.jsが必要とされることをである(これは標準のNode.jsモジュールの実行で)実行するように仕上げながら{}をエクスポートし、バックUserTypeを必要とし、バック、空のオブジェクトを取得した、GroupTypeを必要とし、正しいGraphQL GroupTypeエクスポートUserTypeにだからGroupTypeのリストだからGroupTypeのリストであるのでUserTypeが動作しますが、UserTypeの必要性のために空のオブジェクトがありません。これを回避するために

、あなたはランタイムを使用することができますGroupType.jsに必要:

// GroupType.js 
... 

// Remove the line which requires UserType at the top 
// const UserType = require('./UserType'); 
const AuthService = require('../../services/AuthService'); 

... 

const GroupType = new GraphQLObjectType({ 
    ... 
    fields:() => ({ 
     ... 
     users: { 
      type: new GraphQLList(require('./UserType')), // Require UserType at runtime 
      ... 
     } 
    }) 
}); 

... 
+0

whitep4nther @ありがとうございました。それは今働く。 – user3005919

+0

@ user3005919答えの横にある灰色の目盛りを使用して、質問に答えたことを示す;) – whitep4nther