2017-09-01 1 views
0

Ich habe eine zyklische Definition für GraphQL zu lösen, aber aus irgendeinem Grund kann ich nicht lösen. Ive überprüfte mehrere Beiträge über Thunks und verzögertes Laden und hier ist mein aktueller Code:GraphQL zyklische Referenz mit Thunks - Fehler: Feldtyp muss Ausgabetyp sein, aber erhalten: undefined

Geschichte/types.js

const fields = { 
    id: { 
     type: new GraphQLNonNull(GraphQLID), 
     resolve: (obj) => dbIdToNodeId(obj._id, "History") 
    }, 
    timestamp: { 
     type: GraphQLLong 
    }, 
    objectId: { 
     type: GraphQLString 
    }, 
    user: { 
     type: require('../User/connections').default, 
     args: connectionArgs, 
     resolve: (source, args) => { 
      return UserModel.findOne({ id: source.id }).exec(); 
     } 
    }, 
    action: { 
     type: new GraphQLNonNull(GraphQLString) 
    } 
}; 

export const HistoryType = new GraphQLObjectType({ 
    name: 'History', 
    description: 'History', 
    interfaces:() => [NodeInterface], 
    isTypeOf: (value) => value instanceof HistoryModel, 
    fields:() => (fields) 
}); 

Geschichte/connections.js:

import { HistoryType } from './types'; 
const { connectionType: HistoryConnectionType } = connectionDefinitions({ nodeType: HistoryType }); 

export default HistoryConnectionType; 

Benutzer/types.js

const fields = { 
    id: { 
     type: new GraphQLNonNull(GraphQLID), 
     resolve: (obj) => dbIdToNodeId(obj._id, "User") 
    }, 
    email: { 
     type: GraphQLString 
    }, 
    history: { 
     type: require('../History/connections').default, 
     args: connectionArgs, 
     resolve: (source, args, context) => { 
      return HistoryModel.find({ deleted: false, objectId: source.id}).sort('timestamp').exec(); 
     } 
    } 
}; 

export const UserType = new GraphQLObjectType({ 
    name: 'User', 
    description: 'User', 
    interfaces:() => [NodeInterface], 
    isTypeOf: (value) => value instanceof UserModel, 
    fields:() => (fields) 
}); 

Benutzer/connections.js

import { UserType } from './types'; 

const { connectionType: UserConnectionType } = connectionDefinitions({ nodeType: UserType }); 

export default UserConnectionType; 

Firma/types.js

const fields = { 
    id: { 
     type: new GraphQLNonNull(GraphQLID), 
     resolve: (obj) => dbIdToNodeId(obj._id, "Company") 
    }, 
    name: { 
     type: GraphQLString 
    }, 
    users: { 
     type: require('../User/connections').default, 
     args: connectionArgs, 
     resolve: (source, args) => { 
      const { id } = source; 
      return UserModel.find({ companyId: id }).then((rows) => connectionFromArray(rows,args)); 
     } 
    }, 
    history: { 
     type: require('../History/connections').default, 
     args: connectionArgs, 
     resolve(source, args, context) { 
      return loaders.getHistory(source.id); 
     } 
    } 
}; 

export const CompanyType = new GraphQLObjectType({ 
    name: 'Company', 
    description: 'Company', 
    interfaces:() => [NodeInterface], 
    isTypeOf: (value) => value instanceof CompanyModel, 
    fields:() => (fields) 
}); 

Firma/connections.js

import { CompanyType } from './types'; 

const { connectionType: CompanyConnectionType } = connectionDefinitions({ nodeType: CompanyType }); 

export default CompanyConnectionType; 

Ich kann nicht Bring es zum Laufen. Wenn ich laufe, bekomme ich die folgende Ausgabe:

History.user field type must be Output Type but got: undefined 

Hilfe geschätzt, um das zu lösen.

+0

dies hilfreich sein könnte https://stackoverflow.com/questions/42498900/graphql-error-undefined-adding-a-relationship/42500391#42500391 –

Antwort

1

Ich denke, man es jetzt gelöst, aber für jedermann vorbei:
In history/types.js, das fields Objekt muss in der fields Funktion von HistoryType wird inlined, so wird es zur Laufzeit erstellt.

const HistoryType = new GraphQLObjectType({ 
    ... 
    fields:() => ({ ... }), // not a var, object is inlined 
    ... 
}); 
Verwandte Themen