0

I have a graphql endpoint, I provide an object from the server like this { parentId : 42, parrent: {}}

for my update I only accept this { parentId : 42 }

How do I strip that extra parent property from being sent to the server for mutations?

I'm using a generated set of classes, they are as follows for a more complicated example.

So manually mapping every property works but makes maintenance a pain in the hiney.

export type Folder = {
  id: Scalars["String"];
  name?: Maybe<Scalars["String"]>;
  parrentFolder?: Maybe<Folder>;
  parrentFolderId?: Maybe<Scalars["String"]>;
};

export type FolderInput = {
  name: Scalars["String"];
  parrentFolderId?: Maybe<Scalars["String"]>;
  id?: Maybe<Scalars["String"]>;
};

not removing the parrentFolder generates the following error: Unrecognized input fields 'parrentFolder'

I'd like to have a way to go from a Folder to a FolderInput, that is resilient to changes in either object. Errors for missing properties (present on FolderInput and not Folder) is neat but not required

Bas Hamer
  • 304
  • 3
  • 16
  • my best bet seems to be https://stackoverflow.com/questions/42631523/remove-unnecessary-fields-before-mutation-in-graphql but I'm hoping to avoid having to write all that. – Bas Hamer May 22 '19 at 02:16

1 Answers1

0

You could simply use the delete operator to remove the unneeded parrentFolder property after typecasting. Remember that the types go away after the typescript is compiled into javascript, so the type definitions aren't available at runtime, which makes automatic mapping hard to accomplish.

Example in typescript (I simplified your types):

export type Folder = {
  id: string;
  name?: string;
  parrentFolder?: any;
  parrentFolderId?: string;
};

export type FolderInput = {
  name: string;
  parrentFolderId?: string;
  id?: string;
};

const folder: Folder = {
  id: 'xxx',
  name: 'yyy',
  parrentFolder: {},
  parrentFolderId: 'zzz'
}

const folderInput = folder as FolderInput;
console.log(folderInput);

delete folderInput['parrentFolder'];
console.log(folderInput);

After compilation:

"use strict";
exports.__esModule = true;
var folder = {
    id: 'xxx',
    name: 'yyy',
    parrentFolder: {},
    parrentFolderId: 'zzz'
};
var folderInput = folder;
console.log(folderInput);
delete folderInput['parrentFolder'];
console.log(folderInput);

and here is the output:

{ id: 'xxx',
  name: 'yyy',
  parrentFolder: {},
  parrentFolderId: 'zzz' }

{ id: 'xxx', name: 'yyy', parrentFolderId: 'zzz' }

You may want to add a deep clone if you want to keep using the original folder object.

hayhorse
  • 2,652
  • 1
  • 10
  • 7
  • I was hoping to remove the parrentFolder trough reflection, aka discover that parrentFolder is an unmatched property and wipe it out. The reason for this is long term maintenance, what if proeprties get added to Folder that are not on FolderInput. – Bas Hamer May 23 '19 at 15:11
  • You’re going to have a hard time doing that’s as you don’t have access to type info at runtime. There may be an easy way but I’m not aware of it. – hayhorse May 23 '19 at 22:39