无法在React admin(GraphQL、Mongoose、Apollo服务器)中显示ReferenceField



我是这里的新手,也是的初学者

我正试图使用graphql-mongoose和react-admin建立一个堆栈,经过一些挖掘,我设法让所有的后端和前端与apollo一起工作。

在用户/消息模型类型的关系中,我注意到(从各种教程中(消息的猫鼬模型有时用type: Schema.Types.ObjectId,定义,有时用String定义。我认为ObjectId更有意义。。。

转到Gql模式和解析器,这意味着我在typeDef中声明为这样的

type Message {
id: ID!
text: String
createdAt: String
createdBy: User <- and not String?
}
type User {
id: ID!
username: String
email: String
password: String
token: String
}

但到目前为止,它还没有以这种方式工作,这样做会导致后端错误

Error: The type of MessageInput.username must be Input Type but got: User

我不得不使用String返回,这意味着前端显示用户id,而不能显示用户名

知道我该怎么解决这个问题吗?

非常感谢

我的回购在这里:https://github.com/zovitch/ra-apollo-gql-mongoose.git

我不知道MongoDB方面的情况,但如果您的createdBy外键是字符串,react admin将能够使用<ReferenceField>:显示消息作者

const MessageList = () => (
<List>
<Datagrid>
<TextField source="text" />
<DateField source="createdAt" />
<ReferenceField source="createdBy" reference="users" />
</Datagrid>
</List>
);

更多详细信息,请访问https://marmelab.com/react-admin/ReferenceField.html

<Admin>中,不要忘记为用户Resource定义默认的recordRepresentation,让react管理员知道使用其用户名呈现用户:

<Resource name="users" recordRepresentation="username" />

更多详细信息,请访问https://marmelab.com/react-admin/Resource.html#recordrepresentation

最新更新