From 16a6e42c604fa10d22777e012950470681fbe36f Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 29 Mar 2019 14:12:07 +0100 Subject: [PATCH] Fixes #30 Fixes: #34 --- src/buildVariables.ts | 154 +++++++++++++++++++++++++++++++----------- 1 file changed, 115 insertions(+), 39 deletions(-) diff --git a/src/buildVariables.ts b/src/buildVariables.ts index 9008ac6..fe00de1 100644 --- a/src/buildVariables.ts +++ b/src/buildVariables.ts @@ -15,7 +15,8 @@ import { computeFieldsToAddRemoveUpdate } from './utils/computeAddRemoveUpdate'; import { PRISMA_CONNECT, PRISMA_DISCONNECT, - PRISMA_UPDATE + PRISMA_UPDATE, + PRISMA_CREATE } from './constants/mutations'; import { IntrospectionInputObjectType, @@ -143,6 +144,35 @@ const inputFieldExistsForType = ( return !!findInputFieldForType(introspectionResults, typeName, field); }; +const findMutationInputType = ( + introspectionResults: IntrospectionResult, + typeName: string, + field: string, + mutationType: string +) => { + const inputType = findInputFieldForType( + introspectionResults, + typeName, + field + ); + return findInputFieldForType( + introspectionResults, + inputType!.name, + mutationType + ); +}; + +const hasMutationInputType = ( + introspectionResults: IntrospectionResult, + typeName: string, + field: string, + mutationType: string +) => { + return Boolean( + findMutationInputType(introspectionResults, typeName, field, mutationType) + ); +}; + const buildReferenceField = ({ inputArg, introspectionResults, @@ -156,14 +186,10 @@ const buildReferenceField = ({ field: string; mutationType: string; }) => { - const inputType = findInputFieldForType( + const mutationInputType = findMutationInputType( introspectionResults, typeName, - field - ); - const mutationInputType = findInputFieldForType( - introspectionResults, - inputType!.name, + field, mutationType ); @@ -178,6 +204,45 @@ const buildReferenceField = ({ }, {}); }; +const buildObjectMutationData = ({ + inputArg, + introspectionResults, + typeName, + key +}: { + inputArg: { [key: string]: any }; + introspectionResults: IntrospectionResult; + typeName: string; + key: string; +}) => { + const hasConnect = hasMutationInputType( + introspectionResults, + typeName, + key, + PRISMA_CONNECT + ); + + const mutationType = hasConnect ? PRISMA_CONNECT : PRISMA_CREATE; + + const fields = buildReferenceField({ + inputArg, + introspectionResults, + typeName, + field: key, + mutationType + }); + + // If no fields in the object are valid, continue + if (Object.keys(fields).length === 0) { + return {}; + } + + // Else, connect the nodes + return { + [key]: { [mutationType]: { ...fields } } + }; +}; + interface UpdateParams { id: string; data: { [key: string]: any }; @@ -226,27 +291,33 @@ const buildUpdateVariables = (introspectionResults: IntrospectionResult) => ( } if (isObject(params.data[key])) { - const fieldsToUpdate = buildReferenceField({ - inputArg: params.data[key], + const inputType = findInputFieldForType( introspectionResults, - typeName: `${resource.type.name}UpdateInput`, - field: key, - mutationType: PRISMA_CONNECT - }); + `${resource.type.name}UpdateInput`, + key + ); - // If no fields in the object are valid, continue - if (Object.keys(fieldsToUpdate).length === 0) { + if (!inputType) { return acc; } - // Else, connect the nodes - return { - ...acc, - data: { - ...acc.data, - [key]: { [PRISMA_CONNECT]: { ...fieldsToUpdate } } - } - }; + if (inputType.kind !== 'SCALAR') { + const typeName = `${resource.type.name}UpdateInput`; + + const data = buildObjectMutationData({ + inputArg: params.data[key], + introspectionResults, + typeName, + key + }); + return { + ...acc, + data: { + ...acc.data, + ...data + } + }; + } } // Put id field in a where object @@ -316,27 +387,32 @@ const buildCreateVariables = (introspectionResults: IntrospectionResult) => ( } if (isObject(params.data[key])) { - const fieldsToConnect = buildReferenceField({ - inputArg: params.data[key], + const inputType = findInputFieldForType( introspectionResults, - typeName: `${resource.type.name}CreateInput`, - field: key, - mutationType: PRISMA_CONNECT - }); + `${resource.type.name}UpdateInput`, + key + ); - // If no fields in the object are valid, continue - if (Object.keys(fieldsToConnect).length === 0) { + if (!inputType) { return acc; } - // Else, connect the nodes - return { - ...acc, - data: { - ...acc.data, - [key]: { [PRISMA_CONNECT]: { ...fieldsToConnect } } - } - }; + if (inputType.kind !== 'SCALAR') { + const typeName = `${resource.type.name}CreateInput`; + const data = buildObjectMutationData({ + inputArg: params.data[key], + introspectionResults, + typeName, + key + }); + return { + ...acc, + data: { + ...acc.data, + ...data + } + }; + } } // Put id field in a where object