2017-08-30 8 views
1

タイトルは私の問題では適切ではないかもしれませんが、私のシナリオを説明してください。 私はGraphql schema.Hereで働いている私の最初のschema.jsがhttps://github.com/sany2k8/graphql-udemy/blob/master/schema/schema.jsGraphqlはGraphProjectType内外のモジュールを必要としています

を提出して、それがうまく働いた後、私は別の小さなファイルに分割することを決めたroot_query_type.jsmutation.jsを例えばuser_type.jscompany_type.js。すべてのファイルはモジュールとしてエクスポートされ、循環的に必要です。例えば ​​- 私はこのようなファイルの先頭にconst CompanyType = require('./company_type');を使用

user_type.js

const graphql = require('graphql'); 
const axios = require('axios'); 
const { GraphQLObjectType, GraphQLString, GraphQLInt } = graphql; 
//const CompanyType = require('./company_type'); // *this line causing error* 


const UserType = new GraphQLObjectType({ 
    name: "User", 
    fields:() => ({ 
     id:{ type: GraphQLString}, 
     firstName:{ type: GraphQLString}, 
     age:{ type: GraphQLInt}, 
     company :{ 
      type: require('./company_type'), // *this line fix the error* 
      resolve(parentValue, args){ 
       return axios.get(`http://localhost:3000/companies/${parentValue.companyId}`) 
        .then(res => res.data) 
      } 
     } 
    }) 
}); 

module.exports = UserType; 

company_type.jsuser_type.jsで

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


const CompanyType = new GraphQLObjectType({ 
    name: "Company", 
    fields:()=> ({ 
     id: { type: GraphQLString}, 
     name: { type: GraphQLString}, 
     description: { type: GraphQLString}, 
     users:{ 
      type: new GraphQLList(UserType), 
      resolve(parentValue, args){ 
       return axios.get(`http://localhost:3000/companies/${parentValue.id}/users`) 
        .then(res => res.data) 
      } 
     } 
    }) 
}); 

module.exports = CompanyType; 

ファイルconst UserTypeエラーメッセージの下に私を表示しています

Error: User.company field type must be Output Type but got: [object Object].

私はその行をコメントアウトして直接入力すれば動作します。

company :{ 
       type: require('./company_type'), 
       resolve(parentValue, args){ 
        return axios.get(`http://localhost:3000/companies/${parentValue.companyId}`) 
         .then(res => res.data) 
       } 
      } 

だから、基本的に私の質問は、なぜそれがconst CompanyType = require('./company_type');と協力し、しかしtype: require('./company_type')で動作していませんさ。私は単純な論理的な問題になる可能性がありますが見つかりませんでした。私を助けてください。

答えて

3

あなたが見ている動作は、GraphQLに固有のものではなく、一般的なノードです。あなたのモジュールに循環的な依存関係があります。requireの文内でuser_type.jscompany_type.jsの不完全なコピーに解決されています。お互いを必要とする2つのモジュール(a.jsb.js)与えられた

According to the docs、:あなたの輸出定義の内部必要書類を移動

When main.js loads a.js , then a.js in turn loads b.js . At that point, b.js tries to load a.js . In order to prevent an infinite loop, an unfinished copy of the a.js exports object is returned to the b.js module. b.js then finishes loading, and its exports object is provided to the a.js module.

は一つの解決策です。また、輸出の定義をの上に移動して、を呼び出すことで、同じ効果を得ることができます。 This questionは、循環依存性をより深く見ており、いくつかの代替ソリューションも提供しています。

これは、GraphQLスキーマをプログラマチックに宣言することから離れることを推奨する理由の1つです。 graphql-toolsgenerate-schemaを使用して、GraphQL言語ドキュメントからスキーマを生成できます。これにより、潜在的なサイクルの依存関係を処理することができなくなり、より読みやすいスキーマが得られます。スキーマも簡単にモジュール化できます。型定義は単なる文字列であり、リゾルバは単なるオブジェクトであり、どちらも簡単に組み合わせることができます。

+0

はい私は今それを得ました。リンクリファレンスをありがとう。 –

関連する問題