Apollo:服务器响应所需的形状?
Apollo: Required Shape of Server Response?
我刚刚让我的第一个关系数据库 schema/query/resolver 在 apollo/postgres/sequelize 中运行,直到解析器将 return 数据发送给客户端的时间点。显然我还没有正确形状的数据,因为它在客户端上显示为空。
查询
const CREATE_APPT_MUTATION = gql`
mutation createAPPT ($originatingUserID: String!, $apptWithUserID: String!, $apptDateTime: String!, $apptNotes: String!, $apptTitle: String!){
createAPPT(originatingUserID: $originatingUserID, apptWithUserID: $apptWithUserID, apptDateTime: $apptDateTime, apptNotes: $apptNotes, apptTitle: $apptTitle){
originatingUserID
apptWithUserID
apptDateTime
apptNotes
apptTitle
}
}
`;
解析器的当前响应形状
通过服务器上的console.log 运行,在发送给客户端之前:
{ data:
{ __typename: 'Mutation',
createAPPT:
{ id: '76',
originatingUserID: 'DsmkoaYPeAumREsqC',
apptWithUserID: '9W95z8A7Y6i34buk7',
apptDateTime: '2016-12-24T02:48:50.000Z',
apptTitle: 'Appointment with Benedict Sama',
apptNotes: 'asdf',
createdAt: Fri Dec 23 2016 10:49:12 GMT-0800 (PST),
updatedAt: Fri Dec 23 2016 10:49:12 GMT-0800 (PST),
UserData: [Object],
__typename: 'Appts'
}
}
}
它返回给客户端时在 CHROME 开发工具中的样子
mutationResult: Object
data: Object
createAPPT: Object
__typename: "Appts"
apptDateTime: null
apptNotes: null
apptTitle: null
apptWithUserID: null
originatingUserID: null
__proto__: Object
__proto__: Object
__proto__: Object
最终THEN
解析器中的块
.then(apptWithJoinedData => {
//package up the results in the way that the client is expecting
const apptDataValues = apptWithJoinedData[0].dataValues;
apptDataValues.__typename = "Appts";
var serverResponse = {};
serverResponse.data = {};
serverResponse.data.__typename = 'Mutation';
serverResponse.data.createAPPT = apptDataValues;
// publish subscription notification
debugger;
console.log(serverResponse);
pubsub.publish('APPTAdded', serverResponse);
return serverResponse;
})
有人可以指出我的方向,看看服务器响应的形状有什么问题吗?
通过将最后一个 then
块更改为:
,我将除 UserData 数组之外的所有数据都发送到了服务器
.then(apptWithJoinedData => {
// publish subscription notification
debugger;
console.log('createAPPT cp#2');
console.log(apptWithJoinedData);
pubsub.publish('APPTAdded', apptWithJoinedData);
return apptWithJoinedData;
})
我仍然需要将该 UserData 发送到服务器,但这是另一个线程的主题。
我刚刚让我的第一个关系数据库 schema/query/resolver 在 apollo/postgres/sequelize 中运行,直到解析器将 return 数据发送给客户端的时间点。显然我还没有正确形状的数据,因为它在客户端上显示为空。
查询
const CREATE_APPT_MUTATION = gql`
mutation createAPPT ($originatingUserID: String!, $apptWithUserID: String!, $apptDateTime: String!, $apptNotes: String!, $apptTitle: String!){
createAPPT(originatingUserID: $originatingUserID, apptWithUserID: $apptWithUserID, apptDateTime: $apptDateTime, apptNotes: $apptNotes, apptTitle: $apptTitle){
originatingUserID
apptWithUserID
apptDateTime
apptNotes
apptTitle
}
}
`;
解析器的当前响应形状
通过服务器上的console.log 运行,在发送给客户端之前:
{ data:
{ __typename: 'Mutation',
createAPPT:
{ id: '76',
originatingUserID: 'DsmkoaYPeAumREsqC',
apptWithUserID: '9W95z8A7Y6i34buk7',
apptDateTime: '2016-12-24T02:48:50.000Z',
apptTitle: 'Appointment with Benedict Sama',
apptNotes: 'asdf',
createdAt: Fri Dec 23 2016 10:49:12 GMT-0800 (PST),
updatedAt: Fri Dec 23 2016 10:49:12 GMT-0800 (PST),
UserData: [Object],
__typename: 'Appts'
}
}
}
它返回给客户端时在 CHROME 开发工具中的样子
mutationResult: Object
data: Object
createAPPT: Object
__typename: "Appts"
apptDateTime: null
apptNotes: null
apptTitle: null
apptWithUserID: null
originatingUserID: null
__proto__: Object
__proto__: Object
__proto__: Object
最终THEN
解析器中的块
.then(apptWithJoinedData => {
//package up the results in the way that the client is expecting
const apptDataValues = apptWithJoinedData[0].dataValues;
apptDataValues.__typename = "Appts";
var serverResponse = {};
serverResponse.data = {};
serverResponse.data.__typename = 'Mutation';
serverResponse.data.createAPPT = apptDataValues;
// publish subscription notification
debugger;
console.log(serverResponse);
pubsub.publish('APPTAdded', serverResponse);
return serverResponse;
})
有人可以指出我的方向,看看服务器响应的形状有什么问题吗?
通过将最后一个 then
块更改为:
.then(apptWithJoinedData => {
// publish subscription notification
debugger;
console.log('createAPPT cp#2');
console.log(apptWithJoinedData);
pubsub.publish('APPTAdded', apptWithJoinedData);
return apptWithJoinedData;
})
我仍然需要将该 UserData 发送到服务器,但这是另一个线程的主题。