Skip to content

Commit

Permalink
Fixes Weakky#30
Browse files Browse the repository at this point in the history
Fixes: Weakky#34
  • Loading branch information
macrozone committed Mar 29, 2019
1 parent 7a63113 commit 16a6e42
Showing 1 changed file with 115 additions and 39 deletions.
154 changes: 115 additions & 39 deletions src/buildVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
);

Expand All @@ -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 };
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 16a6e42

Please sign in to comment.