gpt4 book ai didi

node.js - Edge.node 字段类型必须是 Output Type 但得到 : undefined

转载 作者:搜寻专家 更新时间:2023-10-31 22:40:51 27 4
gpt4 key购买 nike

我正在尝试使用 Relay 兼容的 GraphQL 架构设置 node.js 服务器。

尝试验证或加载架构时出现以下错误:

EventEdge.node 字段类型必须是输出类型但得到:未定义。

这是由于在其他类型定义之一中具有用于事件类型的连接类型。

我不会发布整个架构,因为它非常冗长,但是当连接字段被注释掉时,架构会正确加载并且不会抛出任何错误。

我提供了一个导致相同问题的简化架构示例:

  const graphql       = require('graphql')
const relay = require('graphql-relay')

const GraphQLID = graphql.GraphQLID,
GraphQLInt = graphql.GraphQLInt,
GraphQLString = graphql.GraphQLString,
GraphQLList = graphql.GraphQLList,
GraphQLObjectType = graphql.GraphQLObjectType,
GraphQLSchema = graphql.GraphQLSchema,
GraphQLNonNull = graphql.GraphQLNonNull

const connectionArgs = relay.connectionArgs,
connectionDefinitions = relay.connectionDefinitions,
connectionFromArray = relay.connectionFromArray,
cursorForObjectInConnection = relay.cursorForObjectInConnection,
fromGlobalId = relay.fromGlobalId,
globalIdField = relay.globalIdField,
mutationWithClientMutationId = relay.mutationWithClientMutationId,
nodeDefinitions = relay.nodeDefinitions,
toGlobalId = relay.toGlobalId

// Models (ORM Mappings)
const models = require('../models')

// Model handlers
const handlers = require('../models/handlers')


/*
* Relay Node Definition:
* {nodeInterface, nodeField} = nodeDefinitions
*/

var nodeDefinition = nodeDefinitions(
(globalId) => {
// {type, id} = fromGlobalId(globalId)
const gid = fromGlobalId(globalId);

switch (gid.type) {
case 'User':
return handlers.getUser(id)

// case 'Event':
// return handlers.getEvent(id)
//
// case 'Club':
// return handlers.getClub(id)

default:
return null

}
},
(obj) => {
switch (true) {
case (obj instanceof models.User):
return UserType

// case (obj instanceof models.Club):
// return ClubType
//
// case (obj instanceof models.Event):
// return EventType

default:
return null
}
}
)

/**************************
**************************
* Relay Connections
*
* { connectionType, edgeType } = connectionDefinitions({nodeType: LoremType})
**************************
**************************/

// User Connection
// const usersConnection = connectionDefinitions({name: 'User', nodeType: UserType})

// Event Connection
const eventsConnection = connectionDefinitions({name: 'Event', nodeType: EventType})

// Club Connection
// const clubsConnection = connectionDefinitions({name: 'Club', nodeType: ClubType})


/**************************
**************************
* GraphQL Type Definitions
**************************
**************************/

/*
* User Type
*
* type User : Object {
* id: String!
* first_name: String
* last_name: String
* friends: [User]
* }
*/

var UserType = new GraphQLObjectType({
name: 'User',
description: 'A user of the app.',
fields: () => ({
id: globalIdField('User'),
events: {
type: eventsConnection.connectionType,
description: 'User\'s events.',
args: connectionArgs,
resolve: (user, args) => connectionFromArray(getEvents(), args)
}
}),
interfaces: [nodeDefinition.nodeInterface]
})


/*
** Event Type
*
* type Event : Object {
* id: String!
* title: String
* description: String
* datetime: Int
* location: [Int]
* managers: [User]
* club: Club
* members: [User]
* }
*/

var EventType = new GraphQLObjectType({
name: 'Event',
description: 'An event in the app.',
fields: () => ({
id: globalIdField('Event'),
name: {
type: GraphQLString,
description: 'Event\'s name.',
resolve: event => event.get('name')
}
}),
interfaces: [nodeDefinition.nodeInterface]
})


/****************************
****************************
* Relay Mutation Definitions
****************************
****************************/


/**************************
**************************
* Root Schema Definitions
**************************
**************************/

/*
** Root Query
*
* type Query {
* user(id: String!): User
* club(id: String!): Club
* event(id: String!): Event
* }
*/

var QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeDefinition.nodeField,
user: {
type: UserType,
resolve: () => handlers.getUser()
}
})
})


/*
** Root Schema
*
* type Schema {
* query: Query
* mutation: Mutation
* }
*/

var Schema = new GraphQLSchema({
query: QueryType
})


module.exports = Schema

最佳答案

在分配给它之前,您已经引用了 EventType。首先定义类型,然后在连接中使用它:

/**
* Event Type
*
* type Event : Object {
* id: String!
* title: String
* description: String
* datetime: Int
* location: [Int]
* managers: [User]
* club: Club
* members: [User]
* }
*/

var EventType = new GraphQLObjectType({
name: 'Event',
description: 'An event in the app.',
fields: () => ({
id: globalIdField('Event'),
name: {
type: GraphQLString,
description: 'Event\'s name.',
resolve: event => event.get('name')
},
}),
interfaces: [nodeDefinition.nodeInterface],
});

// Event Connection
const eventsConnection = connectionDefinitions({
name: 'Event',
nodeType: EventType,
});

关于node.js - Edge.node 字段类型必须是 Output Type 但得到 : undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35993661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com