Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PrismaTable): capitalize model names in gql requests #272

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions admin/src/PrismaTable/EditRecord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { queryDocument } from './QueryDocument';
import { TableContext } from './Context';
import { Option } from '../components/Select';
import { classNames } from '../components/css';
import { fLCapital } from './utils';

interface EditRecordProps {
model: string;
Expand Down Expand Up @@ -62,10 +63,11 @@ const EditRecord: React.FC<EditRecordProps> = ({
getRecord();
}

const record = data ? data[`findUnique${model}`] : {};
const modelUpper = fLCapital(model);
const record = data ? data[`findUnique${modelUpper}`] : {};

if (
(!loading && data && !data[`findUnique${model}`] && modelObject) ||
(!loading && data && !data[`findUnique${modelUpper}`] && modelObject) ||
(modelObject && !modelObject.update && !actions && !view) ||
(actions && !actions.includes('update') && !view)
)
Expand Down
27 changes: 16 additions & 11 deletions admin/src/PrismaTable/QueryDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SchemaModel } from '../types';
import tag from 'graphql-tag';
import { fLCapital } from './utils';

const getFields = (
models: SchemaModel[],
Expand Down Expand Up @@ -73,27 +74,29 @@ export const queryDocument = (
update = false,
) => {
const fields = getFields(models, modelName, update);
const modelUpper = fLCapital(modelName);

if (findUnique) {
return tag`
query findUnique${modelName}($where: ${modelName}WhereUniqueInput!) {
findUnique${modelName}(where: $where) {
query findUnique${modelUpper}($where: ${modelName}WhereUniqueInput!) {
findUnique${modelUpper}(where: $where) {
${fields}
}
}
`;
} else {
return tag`
query findMany${modelName}(
query findMany${modelUpper}(
$where: ${modelName}WhereInput
$orderBy: [${modelName}OrderByWithRelationInput!]
$cursor: ${modelName}WhereUniqueInput
$skip: Int
$take: Int
) {
findMany${modelName}(where: $where, orderBy: $orderBy, cursor: $cursor, skip: $skip, take: $take) {
findMany${modelUpper}(where: $where, orderBy: $orderBy, cursor: $cursor, skip: $skip, take: $take) {
${fields}
}
findMany${modelName}Count(where: $where)
findMany${modelUpper}Count(where: $where)
}
`;
}
Expand All @@ -105,23 +108,25 @@ export const mutationDocument = (
mutation: 'create' | 'update' | 'delete',
) => {
const fields = getFields(models, model, true);
const modelUpper = fLCapital(model);

const modelObject = models.find((item) => item.id === model);
switch (mutation) {
case 'create':
return tag`mutation createOne${model}($data: ${model}CreateInput!) {
createOne${model}(data: $data) {
return tag`mutation createOne${modelUpper}($data: ${model}CreateInput!) {
createOne${modelUpper}(data: $data) {
${modelObject?.idField || allScalar(modelObject)}
}
}`;
case 'delete':
return tag`mutation deleteOne${model} ($where: ${model}WhereUniqueInput!) {
deleteOne${model} (where: $where) {
return tag`mutation deleteOne${modelUpper} ($where: ${model}WhereUniqueInput!) {
deleteOne${modelUpper} (where: $where) {
${modelObject?.idField || allScalar(modelObject)}
}
}`;
case 'update':
return tag`mutation updateOne${model} ($where: ${model}WhereUniqueInput!, $data: ${model}UpdateInput!) {
updateOne${model} (where: $where, data: $data) {
return tag`mutation updateOne${modelUpper} ($where: ${model}WhereUniqueInput!, $data: ${model}UpdateInput!) {
updateOne${modelUpper} (where: $where, data: $data) {
${fields}
}
}`;
Expand Down
5 changes: 4 additions & 1 deletion admin/src/PrismaTable/dynamicTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TableContext } from './Context';
import EditRecord from './EditRecord';
import { mutationDocument, queryDocument } from './QueryDocument';
import { ContextProps, TableParentRecord } from '..';
import { fLCapital } from './utils';

export interface DynamicTableProps {
parent?: TableParentRecord;
Expand Down Expand Up @@ -179,7 +180,9 @@ const DynamicTable: React.FC<DynamicTableProps> = ({
const parentName = modelObject?.fields.find(
(item) => item.type === parent?.name,
)?.name;
const _data: any[] = data ? data[`findMany${model}`] : [];
const modelUpper = fLCapital(model);
const _data: any[] = data ? data[`findMany${modelUpper}`] : [];

return (
<>
{children &&
Expand Down
1 change: 1 addition & 0 deletions admin/src/PrismaTable/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const fLCapital = (s: string) => s.replace(/./, (c) => c.toUpperCase());