2016-08-18 14 views
12

Soruma benzer Javascript circular dependency in GraphQL code ama benim sorun yapısı ve veritabanı düzeyinde değil, javascript (ES6).Grafiksel olarak şemaya dairesel bağımlılıklar olmadan nasıl ayrılır?

Şema tanımım çok büyüyor ama dosyayı parçalara bölebildiğimi göremiyorum. Farklı nesne türlerine göre kesmek için mantıklı gibi görünüyor, ama bu benzer bu çok basitleştirilmiş, çalışmayan örnek dairesel bağımlılıklar getiriyor: Bunun için ortak bir yol ya da en iyi uygulama

// -- 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, 
     /*...*/ 
     }), 
    }); 

var mı?

+1

sizi var:

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

diğerleri dosyalarınız gibi Nesneleri içerir:

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

after.js bu yolu olabilir:

before.js bu yolu olabilir başka çözümler buldun mu? –

cevap

4

Aynı sorunu yaşıyorum. Temizleyici çözümlerin gruntjs concat kullanımı hakkında olduğunu düşünüyorum. Bu yapılandırmayla

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

olduğundan, son schema.js oluşturarak, çeşitli dosyalarda şema bölebilirsiniz.

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

Teşekkür ederim, sorunumu kesinlikle yanıtladığı için çözümünüzü kabul ediyorum. Ben hala bu soru yüzünden kullanmak için overkill gibi görünüyor homurdanı kullanmak için bir yol bulmaya çalışıyorum. Ama başka bir yol olmazsa, bunu uygulayacağım. –

+0

Apollo'nun graphql araçlarına bir çekim yapmak istiyorsanız, kodunuzu gerçekten basitleştirecek ve daha okunabilir hale getirecektir. Eğer kullanırsanız, tam problemini çözen schemaglue.js adında bir araç yazdım. GraphQL kodunuzu ölçeklendirmek ve düzenlemek için yazdım: https://hackernoon.com/graphql-schemas-easier-better-structure-31677a640d18 Bu yardımcı olur umarım. –

İlgili konular