【发布时间】:2019-11-25 13:19:17
【问题描述】:
我有 2 个模型,公司和用户。从数据库的角度来看,一家公司有很多用户。创建单个用户时,我想通过返回与用户关联的公司来利用 graphQL 的强大功能。但是,这仅在执行查询时有效。尝试突变时,对象发生突变,但请求的关系数据始终返回 null
在模型中,我们声明一个 -> 多关系,并在我们的用户模型架构中包含公司模型架构以访问数据
用户模型架构
type User {
clients: [Client!]
company: Company <------- Company Relation
companyId: UUID
confirmed: Boolean
defaultPortfolioSize: Int
email: String!
firstName: String!
lastLogin: String!
lastName: String!
id: UUID!
isActive: Boolean
isStaff: Boolean
isSuperuser: Boolean
password: String
phoneNumber: String
priceNotification: Boolean
priceThreshold: Float
sentimentNotification: Boolean
sentimentThreshold: Float
token: String
clientCount: Int
notificationCount: Int
portfolioCount: Int
stockAverageCount: Float
totalValue: Float
stockList: [PortfolioStock!]
}
在用户突变中,我们传递了一个公司 ID,用于将用户连接到关联的公司对象
用户变异
user(
companyId: UUID <---- Company ID for relation
confirmed: Boolean
defaultPortfolioSize: Int
delete: Boolean
email: String
firstName: String
lastName: String
id: UUID
isActive: Boolean
isStaff: Boolean
isSuperuser: Boolean
password: String
phoneNumber: String
priceNotification: Boolean
priceThreshold: Float
sentimentNotification: Boolean
sentimentThreshold: Float
username: String
): User!
解析器非常简单。我们验证授权,然后继续请求。
用户变异解析器
user: async (_, params, { user }) => {
if (params.id) {
await authorize(user, Permission.MODIFY_USER, { userId: params.id });
} else {
// Anyone can register
}
return await userDataLoader.upsertUser(user, params);
},
数据加载器是魔法发生的地方。我们调用 upsertUser 来创建、更新和删除任何对象。这里我们成功创建了一个用户,并且可以在数据库中验证创建。
用户数据加载器
upsertUser: async (user, params) => {
...
/* Register */
if (!params.companyId) {
throw new UserInputError("Missing 'companyId' parameter");
}
if (!params.password) {
throw new UserInputError("Missing 'password' parameter");
}
let newUser = new User({
billingAddressId: 0,
dateJoined: new Date(),
defaultPortfolioSize: 45,
isActive: true,
isStaff: false,
isSuperuser: false,
lastLogin: new Date(),
phoneNumber: '',
priceNotification: false,
priceThreshold: 0,
sentimentNotification: false,
sentimentThreshold: 0,
subscriptionStatus: false,
...params,
});
newUser = await newUser.save();
newUser.token = getJWT(newUser.email, newUser.id);
EmailManager(
EmailTemplate.CONFIRM_ACCOUNT,
`${config.emailBaseUrl}authentication/account-confirmation/?key=${
newUser.token
}`,
newUser.email
);
return newUser;
},
// Including the users query dataloader for reference
users: async params => {
return await User.findAll(get({ ...defaultParams(), ...params }));
},
这是一个示例突变,我们创建一个用户对象并请求具有嵌套公司关系的响应。
突变示例
mutation {
user(
companyId: "16a94e71-d023-4332-8263-3feacf1ad4dc",
firstName: "Test"
lastName: "User"
email: "test@gmail.com"
password: "PleaseWork"
) {
id
company {
id
name
}
email
firstName
lastName
}
}
但是,当请求将关系包含在响应对象中时,api 返回 null 而不是对象。
示例响应
ACTUAL:
{
"data": {
"user": {
"id": "16a94e71-d023-4332-8263-3feacf1ad4dc",
"company": null,
"email": "test@gmail.com",
"firstName": "Test",
"lastName": "User"
}
}
}
EXPECTED:
{
"data": {
"user": {
"id": "16a94e71-d023-4332-8263-3feacf1ad4dc",
"company": {
"id": "16a94e71-d023-4332-8263-3feacf1ad4dc",
"name": "Test Company",
},
"email": "test@gmail.com",
"firstName": "Test",
"lastName": "User"
}
}
}
我想我有点困惑,为什么 graphQL 不能在突变期间绘制我的嵌套对象,但可以通过查询来做到这一点。
【问题讨论】:
-
Daniel Reardan 可能会用他的解释链接来回答这个问题:stackoverflow.com/questions/56319137/…。我了解到 GraphQL 消息非常笼统,问题可能是众多问题之一。最好从 Daniel 的解决方案开始。
-
@Preston 感谢您的回复,我会检查一下
-
您说您在查询时成功检索了公司信息。您如何检索给定用户的公司数据?如果您使用标准 DB 连接进行此操作,那么您的
newUser数据可能不完整并且不包含company信息。理想情况下,您希望为company类型的company字段提供一个特定的解析器,并以这种方式实现您的关联;这将保证在您返回类型User时解析器始终运行(当然,如果查询了company字段)。
标签: graphql sequelize.js apollo