({
type,
provider,
entity,
schemaMap,
}: {
type: 'add' | 'update'
provider: string
entity: Entity
schemaMap: SchemaMap | undefined
})
| 12 | ((schemaMap && schemaMap[entity.name]) || entity.className) as string |
| 13 | |
| 14 | export function generateMutation({ |
| 15 | type, |
| 16 | provider, |
| 17 | entity, |
| 18 | schemaMap, |
| 19 | }: { |
| 20 | type: 'add' | 'update' |
| 21 | provider: string |
| 22 | entity: Entity |
| 23 | schemaMap: SchemaMap | undefined |
| 24 | }): string { |
| 25 | const service: string = getResourceNameForMutationGenerator(entity, schemaMap) |
| 26 | const capitalizedType = capitalizeString(type) |
| 27 | // cases: add(upsert), update(update) |
| 28 | // input names are different for upsert and update |
| 29 | const mutationVarName = type === 'add' ? '$input' : '$patch' |
| 30 | const providerServiceString = service.includes(provider) |
| 31 | ? service |
| 32 | : `${provider}${capitalizeString(service)}` |
| 33 | // We get the mutation type name for this mutation |
| 34 | const mutationInputTypeName = `${capitalizedType}${providerServiceString}Input` |
| 35 | // When upserting we insert several entities at a time |
| 36 | // Whereas updating we update edges for one entity at a time |
| 37 | const mutationInputType = |
| 38 | type === 'add' |
| 39 | ? `[${mutationInputTypeName}!]!` |
| 40 | : `${mutationInputTypeName}!` |
| 41 | // Add the upsert boolean if the mutation is an upsert |
| 42 | const mutationAdditionalArgs = type === 'add' ? ',upsert:true' : '' |
| 43 | // For upsert we tell the mutation to return the number of affected nodes |
| 44 | // For update we pass the query that filter the node in order to update its edges |
| 45 | const internalQuery = |
| 46 | type === 'add' ? 'numUids' : `${providerServiceString}{id}` |
| 47 | // And we put everything together |
| 48 | // eslint-disable-next-line max-len |
| 49 | const mutation = `mutation(${mutationVarName}:${mutationInputType}){${type}${providerServiceString}(input:${mutationVarName}${mutationAdditionalArgs}){${internalQuery}}}` |
| 50 | return mutation |
| 51 | } |
| 52 | |
| 53 | export const generateUpdateVarsObject = ( |
| 54 | service: any, |
no test coverage detected