2016-08-18 10 views
12

私の質問はJavascript circular dependency in GraphQL codeに似ていますが、私の問題は構造とデータベースレベルではなくjavascript(ES6)です。循環依存関係を持たずにGraphQLでスキーマを分割する方法は?

スキーマの定義が大きくなりすぎていますが、ファイルを分割する場所がわかりません。異なるオブジェクトタイプに基づいてカットする論理的のようですが、それは、この非常に単純化され、非実施例と同様に、円形の依存関係にもたらす:

// -- file A.js 

    import { bConnection, getBs } from 'B'; 

    export class A { /*...*/ }; 
    export var getA = (a) => { /*...*/ }; 
    export var getAs = (array_of_as) => { /*...*/ }; 

    export var aType = new GraphQLObjectType ({ 
     name: 'A', 
     fields:() => ({ 
     bs: { 
      type: bConnection, 
      /*...*/ 
     }, 
     resolve: (a, args) => connectionFromPromisedArray (
      getBs (a.bs) 
     ), 
     /*...*/ 
     }), 
     interfaces:() => [ require ('./nodeDefs').nodeInterface ], 
     /*...*/ 
    }) 

    export var { 
     connectionType: aConnection, 
     edgeType: aEdge 
     } = connectionDefinitions ({ 
     name: 'A', 
     nodeType: aType 
     }); 

    // -- file B.js 

    import { aConnection, getAs } from 'A'; 

    export class B { /*...*/ }; 
    export var getB = (b) => { /*...*/ }; 
    export var getBs = (array_of_bs) => { /*...*/ }; 

    export var bType = new GraphQLObjectType ({ 
     name: 'B', 
     fields:() => ({ 
     as: { 
      type: aConnection, 
      /*...*/ 
     }, 
     resolve: (b, args) => connectionFromPromisedArray (
      getAs (b.bs) 
     ), 
     /*...*/ 
     }), 
     interfaces:() => [ require ('./nodeDefs').nodeInterface ], 
     /*...*/ 
    }) 

    export var { 
     connectionType: bConnection, 
     edgeType: bEdge 
     } = connectionDefinitions ({ 
     name: 'B', 
     nodeType: bType 
     }); 

    // -- file nodeDefs.js 

    import { 
     fromGlobalId, 
     nodeDefinitions, 
    } from 'graphql-relay'; 

    import { A, getA, aType } from 'A' 
    import { B, getB, bType } from 'B' 

    export var {nodeInterface, nodeField} = nodeDefinitions (
     (globalId) => { 
     var {type, id} = fromGlobalId (globalId); 
     if (type === 'A') { 
      return getA (id); 
     } else if (type === 'B') { 
      return getB (id); 
     } 
     }, 
     (obj) => { 
     if (obj instanceof A) { 
      return aType; 
     } else if (obj instanceof B) { 
      return bType; 
     } 
     } 
    ) 

    // -- file schema.js 

    import { 
     GraphQLObjectType, 
     GraphQLSchema, 
    } from 'graphql'; 

    import { nodeField } from './nodeDefs'; 

    var queryType = new GraphQLObjectType ({ 
     name: 'Query', 
     fields:() => ({ 
     node: nodeField, 
     /*...*/ 
     }), 
    }); 

は、一般的な方法やそのためのベストプラクティスはありますか?

+1

はあなたを持っています他の解決策が見つかりましたか? –

答えて

4

私は同じ問題を抱えています。私はよりクリーンなソリューションは、gruntjs concatを使用していると思います。

grunt.initConfig({ 
    concat: { 
    js: { 
     src: ['lib/before.js', 'lib/*', 'lib/after.js'], 
     dest: 'schema.js', 
    } 
    } 
}); 

この設定では、スキーマを複数のファイルに分割して、最終的なschema.jsを作成することができます。

before.jsは、この方法かもしれない:

import { 
    GraphQLObjectType, 
    GraphQLInt, 
    GraphQLString, 
    GraphQLSchema, 
    GraphQLList, 
    GraphQLNonNull 
} from 'graphql'; 
import db from '../models/index.js'; 
import Auth from '../classes/auth'; 

after.jsは、この方法かもしれない:

const Schema = new GraphQLSchema({ 
    query: Query, 
    mutation: Mutation 
}) 
export default Schema; 

、他のファイルのようなオブジェクト含まれています:

const Funcionario = new GraphQLObjectType({ 
name: 'Funcionario', 
description: 'This represent a Funcionario', 
fields:() => { 
    return { 
     id: { 
      type: GraphQLInt, 
      resolve(funcionario, args) { 
       return funcionario.id; 
      } 
     }, 
     CPF: { 
      type: GraphQLString, 
      resolve(funcionario, args) { 
       return funcionario.CPF; 
      } 
     }, 
     nome: { 
      type: GraphQLString, 
      resolve(funcionario, args) { 
       return funcionario.nome; 
      } 
     }, 
     sobrenome: { 
      type: GraphQLString, 
      resolve(funcionario, args) { 
       return funcionario.sobrenome; 
      } 
     }, 
     sessions: { 
      type: new GraphQLList(Session), 
      resolve(funcionario, args) { 
       return funcionario.getSessions(); 
      } 
     } 
    } 
} 
}) 
+1

ありがとう、私はあなたの解決策を受け入れる、それは間違いなく私の問題に答える。私はまだこの質問のためにそれを使うのは残忍であるように見えるので、不快感を使う方法を見つけようとしています。しかし、それ以外の方法がなければ、私はこれを実装します。 –

+0

Apolloのgraphql-toolsにショットを付ける場合は、コードを単純化して読みやすくします。あなたが使っているなら、私はあなたの正確な問題を解決するschemaglue.jsというツールを書いています。私はここであなたのGraphQLコードをスケーリングし整理することについて書いています:https://hackernoon.com/graphql-schemas-easier-better-structure-31677a640d18これが役立つことを願っています。 –

関連する問題