import { DocumentNode } from 'graphql' ; import { ensureMetadata , readMetadata } from './metadata' ; import { Metadata } from '../types/metadata' ; import { MutationDef , MutationResolveFn } from '../types/mutation' ; import { getNameOfMutation } from '../mutation' ; export function setMutationMetadata ( proto , propName , mutation ) { const constructor = proto . constructor ; const meta = ensureMetadata ( constructor ) ; meta . mutations . push ( { propName , mutation , } ) ; } export function hasMutation ( target , propName ) { const meta = readMetadata ( target ) ; if ( meta ) { return meta . mutations . some ( def => { return def . propName === propName ; } ) ; } return false ; } export function transformMutations ( instance , meta , transformFn = resolver => resolver ) { return meta . mutations . map ( ( { propName , mutation } ) => ( { mutation : typeof mutation === 'string' ? mutation : getNameOfMutation ( mutation ) , resolve : transformFn ( instance [ propName ] . bind ( instance ) ) , } ) ) ; }