【问题标题】:Apollo: Required Shape of Server Response?Apollo:所需的服务器响应形状?
【发布时间】:2017-05-09 11:06:06
【问题描述】:

我刚刚在 apollo/postgres/sequelize 中使用了我的第一个关系数据库模式/查询/解析器,直到解析器将数据返回给客户端的时候。显然我还没有正确形状的数据,因为它在客户端上显示为空。

查询

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;
    })

有人可以指出我的方向,看看服务器响应的形状有什么问题吗?

【问题讨论】:

    标签: apollostack apollo-server


    【解决方案1】:

    通过将最后一个then 块更改为:

                .then(apptWithJoinedData => {
                     // publish subscription notification
                    debugger;
                    console.log('createAPPT cp#2');
                    console.log(apptWithJoinedData);
                    pubsub.publish('APPTAdded', apptWithJoinedData);
                    return apptWithJoinedData;
                })
    

    我仍然需要将该 UserData 发送到服务器,但这是另一个线程的主题。

    【讨论】:

      猜你喜欢
      • 2020-03-29
      • 2022-06-12
      • 2020-10-16
      • 2021-12-28
      • 2019-04-27
      • 2023-04-02
      • 2020-12-19
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多