-
Notifications
You must be signed in to change notification settings - Fork 45
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
Fixes #30, Fixes #34, Fixes #36, #35
Open
macrozone
wants to merge
6
commits into
Weakky:master
Choose a base branch
from
panter:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16a6e42
Fixes #30
macrozone 7df730d
fixes: https://github.com/Weakky/ra-data-opencrud/issues/36
macrozone c087165
fix: disconnect when reference is empty
macrozone f319eb6
fix: update of lists of embedded objects
macrozone 049fabb
fix
macrozone 1a1b0de
not a fix
macrozone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }; | ||
|
@@ -191,17 +256,26 @@ const buildUpdateVariables = (introspectionResults: IntrospectionResult) => ( | |
) => { | ||
return Object.keys(params.data).reduce( | ||
(acc, key) => { | ||
if (Array.isArray(params.data[key])) { | ||
const inputType = findInputFieldForType( | ||
introspectionResults, | ||
`${resource.type.name}UpdateInput`, | ||
key | ||
); | ||
// Put id field in a where object | ||
if (key === 'id' && params.data[key]) { | ||
return { | ||
...acc, | ||
where: { | ||
id: params.data[key] | ||
} | ||
}; | ||
} | ||
const inputType = findInputFieldForType( | ||
introspectionResults, | ||
`${resource.type.name}UpdateInput`, | ||
key | ||
); | ||
|
||
if (!inputType) { | ||
return acc; | ||
} | ||
if (!inputType) { | ||
return acc; | ||
} | ||
|
||
if (Array.isArray(params.data[key])) { | ||
//TODO: Make connect, disconnect and update overridable | ||
//TODO: Make updates working | ||
const { | ||
|
@@ -226,37 +300,23 @@ const buildUpdateVariables = (introspectionResults: IntrospectionResult) => ( | |
} | ||
|
||
if (isObject(params.data[key])) { | ||
const fieldsToUpdate = buildReferenceField({ | ||
inputArg: params.data[key], | ||
introspectionResults, | ||
typeName: `${resource.type.name}UpdateInput`, | ||
field: key, | ||
mutationType: PRISMA_CONNECT | ||
}); | ||
if (inputType.kind !== 'SCALAR') { | ||
const typeName = `${resource.type.name}UpdateInput`; | ||
|
||
// If no fields in the object are valid, continue | ||
if (Object.keys(fieldsToUpdate).length === 0) { | ||
return acc; | ||
const data = buildObjectMutationData({ | ||
inputArg: params.data[key], | ||
introspectionResults, | ||
typeName, | ||
key | ||
}); | ||
return { | ||
...acc, | ||
data: { | ||
...acc.data, | ||
...data | ||
} | ||
}; | ||
} | ||
|
||
// Else, connect the nodes | ||
return { | ||
...acc, | ||
data: { | ||
...acc.data, | ||
[key]: { [PRISMA_CONNECT]: { ...fieldsToUpdate } } | ||
} | ||
}; | ||
} | ||
|
||
// Put id field in a where object | ||
if (key === 'id' && params.data[key]) { | ||
return { | ||
...acc, | ||
where: { | ||
id: params.data[key] | ||
} | ||
}; | ||
} | ||
|
||
const type = introspectionResults.types.find( | ||
|
@@ -291,17 +351,26 @@ const buildCreateVariables = (introspectionResults: IntrospectionResult) => ( | |
) => | ||
Object.keys(params.data).reduce( | ||
(acc, key) => { | ||
if (Array.isArray(params.data[key])) { | ||
if ( | ||
!inputFieldExistsForType( | ||
introspectionResults, | ||
`${resource.type.name}CreateInput`, | ||
key | ||
) | ||
) { | ||
return acc; | ||
} | ||
// Put id field in a where object | ||
if (key === 'id' && params.data[key]) { | ||
return { | ||
...acc, | ||
where: { | ||
id: params.data[key] | ||
} | ||
}; | ||
} | ||
|
||
const inputType = findInputFieldForType( | ||
introspectionResults, | ||
`${resource.type.name}UpdateInput`, | ||
key | ||
); | ||
|
||
if (!inputType) { | ||
return acc; | ||
} | ||
if (Array.isArray(params.data[key])) { | ||
return { | ||
...acc, | ||
data: { | ||
|
@@ -316,37 +385,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 } } | ||
} | ||
}; | ||
} | ||
|
||
// Put id field in a where object | ||
if (key === 'id' && params.data[key]) { | ||
return { | ||
...acc, | ||
where: { | ||
id: params.data[key] | ||
} | ||
}; | ||
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 | ||
} | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this part is mostly duplicated from buildUpdateVariables, we should generalize that later |
||
} | ||
|
||
const type = introspectionResults.types.find( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am not sure if there are some cases where we should add the property despite there is no input type.
when key is
id
is one exception, that's why i moved it on top of the function. But maybe there are other cases?