title: How to properly parse and type graphql results?
body: <p>Q: How do I parse and type a server response in a generic way when one of the parent keys in the response will constantly change?</p>
  
  <p>As a preface: I'm still new to TS and parsing server responses - so maybe this is obvious - but after a day I've not found a good solution. </p>
  
  <p>Background: I'm using Typescript and AWS Amplify. I have a graphql mutation called CreateGroup. It requires input of a code-generated type CreateGroupInput defined as such:</p>
  
  <pre><code>export type CreateGroupInput = {
    id?: string | null,
    name: string,
    createdAt?: string | null,
    updatedAt?: string | null,
  };
  
  //I'm using this as input to create a new db entry
  const groupInfo: CreateGroupInput = {
    name: "new group",
    createdAt: time,
    updatedAt: time
  };
  </code></pre>
  
  <p>I want to parse and type the result of the mutation so that I can access the id that gets created during the mutation process. IDEALLY this is what I would like to do:</p>
  
  <pre><code>const data = API.graphql(graphqlOperation(mutations.createGroup, {input: groupInfo}) as CreateGroupInput;
  
  </code></pre>
  
  <p>and have the data variable be of type CreateGroupInput with the server response all in the correct place. However the result I get back doesn't parse nicely because the structure is a bit different:</p>
  
  <pre><code>{
      "data": {
          "createGroup": {
              "id": "1d720f68-56bd-46a9-82ef-e060a5b07177",
              "name": "new group",
              "createdAt": "1580278766358",
              "updatedAt": "1580278766358"
          }
      }
  }
  </code></pre>
  
  <p>I tried doing different combinations of JSON.Parse and JSON.stringify - but none of them work and honestly it feels like I am doing something really wrong. This function will eventually handle many different mutations - and I expect the name "createGroup" in the response to change for each of these - so I can't just parse "result.data.createGroup" either. </p>
  
  <p>Which leads me to posting this question. How do I parse and type the server response in a generic way when the key "createGroup" in the response will constantly change?</p>