diff --git a/src/index.ts b/src/index.ts index e6a01f0..f96c473 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ import { ServiceConnection, ProviderData, Entity, + EntityMutations, SchemaMap, } from './types' import { @@ -41,6 +42,7 @@ import { mergeSchemas, getSchemaFromFolder, generateSchemaMapDynamically, + generateEntityMutations, } from './utils/schema' // Export Utils @@ -52,6 +54,7 @@ export { mergeSchemas, getSchemaFromFolder, generateSchemaMapDynamically, + generateEntityMutations, } export { PluginModule, PluginType, Result, pluginMap } @@ -69,6 +72,7 @@ export type { JsRule, JsonRule, Entity, + EntityMutations, StorageEngineConnectionConfig, StorageEngineConfig, StorageEngine, diff --git a/src/types/index.ts b/src/types/index.ts index fd09e1e..21cfda9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -49,10 +49,17 @@ export interface Service { rawData: any }) => any } + +export interface EntityMutations { + query?: string + upsert: string + delete: string +} + export interface Entity { className?: string name: string - mutation: string + mutation: EntityMutations | string data: any[] | any } diff --git a/src/utils/schema.ts b/src/utils/schema.ts index 06aee73..bd6a652 100644 --- a/src/utils/schema.ts +++ b/src/utils/schema.ts @@ -3,6 +3,8 @@ import { loadFilesSync } from '@graphql-tools/load-files' import { print } from 'graphql' import path from 'path' +import { EntityMutations } from '../types' + export const mergeSchemas = (currSchema: string, additions: string[]) => { const s = mergeTypeDefs([currSchema, ...additions]) return print(s) @@ -36,3 +38,18 @@ export const generateSchemaMapDynamically = ( } return resourceTypeNamesToFieldsMap } + +const generateDeleteMutation = (schemaName: string): string => + `mutation delete${schemaName}($input: [String!]!){delete${schemaName}(filter: { id: { in: $input }}) { numUids } }` + +const generateUpsertMutation = (schemaName: string): string => + `mutation($input: [Add${schemaName}Input!]!) { add${schemaName}(input: $input, upsert: true) { numUids } }` + +export const generateEntityMutations = ( + schemaName: string +): EntityMutations => { + return { + upsert: generateUpsertMutation(schemaName), + delete: generateDeleteMutation(schemaName), + } +}