Search code examples
apollostackapollo-server

Apollo: Required Shape of Server Response?


I’ve just gotten my first relational database schema/query/resolver working in apollo/postgres/sequelize, up to the point where it’s time for the resolver to return data to the client. Evidently I don’t have the data in the correct shape yet, as it comes up null on the client.

QUERY

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

CURRENT SHAPE OF RESPONSE FROM RESOLVER

Via console.log running on the server, before being sent to client:

{ 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' 
        } 
    } 
}

HOW IT LOOKS IN CHROME DEV TOOLS WHEN IT COMES BACK TO THE CLIENT

mutationResult: Object
    data: Object
        createAPPT: Object
            __typename: "Appts"
            apptDateTime: null
            apptNotes: null
            apptTitle: null
            apptWithUserID: null
            originatingUserID: null
        __proto__: Object
    __proto__: Object
__proto__: Object

FINAL THEN BLOCK IN RESOLVER

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

Can someone point me in the direction of seeing what's wrong with the shape of the server response?


Solution

  • I got all the data, except for the array of UserData, to the server by changing the last then block to:

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

    I still have to get that UserData to the server, but that's a topic for another thread.