@contember/client-content
Advanced tools
Comparing version 1.3.0-alpha.26 to 1.3.0-alpha.27
@@ -38,4 +38,12 @@ import { createListArgs } from "./utils/createListArgs.js"; | ||
const typedArgs = createListArgs(context.entity, args); | ||
const selectionSet = this.resolveSelectionSet(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selectionSet); | ||
const selection = this.resolveSelection(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selection.selectionSet, (it) => { | ||
const transformFn = selection.transformFn; | ||
if (transformFn) { | ||
return it.map((el) => transformFn(el, { | ||
rootValue: it | ||
})); | ||
} | ||
return it; | ||
}); | ||
} | ||
@@ -49,47 +57,63 @@ get(name, args, fields) { | ||
}); | ||
const selectionSet = this.resolveSelectionSet(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selectionSet); | ||
const selection = this.resolveSelection(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selection.selectionSet, (it) => { | ||
if (it && selection.transformFn) { | ||
return selection.transformFn(it, { | ||
rootValue: it | ||
}); | ||
} | ||
return it; | ||
}); | ||
} | ||
create(name, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `create${name}`; | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
data: `${context.entity.name}CreateInput!` | ||
data: `${entity.name}CreateInput!` | ||
}); | ||
const selectionSet = this.createMutationSelection("create", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "create", typedArgs, fields); | ||
} | ||
update(name, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `update${name}`; | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
data: `${context.entity.name}UpdateInput!`, | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where` | ||
data: `${entity.name}UpdateInput!`, | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where` | ||
}); | ||
const selectionSet = this.createMutationSelection("update", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "update", typedArgs, fields); | ||
} | ||
upsert(name, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `upsert${name}`; | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
update: `${context.entity.name}UpdateInput!`, | ||
create: `${context.entity.name}CreateInput!`, | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where` | ||
update: `${entity.name}UpdateInput!`, | ||
create: `${entity.name}CreateInput!`, | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where` | ||
}); | ||
const selectionSet = this.createMutationSelection("upsert", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "upsert", typedArgs, fields); | ||
} | ||
delete(name, args, fields) { | ||
const context = this.createContext(name); | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where` | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where` | ||
}); | ||
const fieldName = `delete${name}`; | ||
const selectionSet = this.createMutationSelection("delete", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "delete", typedArgs, fields); | ||
} | ||
createMutationOperation(name, operation, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `${operation}${name}`; | ||
const nodeSelection = fields ? this.resolveSelection(fields, context) : void 0; | ||
const selectionSet = this.createMutationSelection(operation, nodeSelection?.selectionSet); | ||
return new ContentOperation("mutation", fieldName, args, selectionSet, (it) => { | ||
if (!nodeSelection?.transformFn) { | ||
return it; | ||
} | ||
return { | ||
...it, | ||
node: it.node ? nodeSelection.transformFn(it.node, { | ||
rootValue: it.node | ||
}) : null | ||
}; | ||
}); | ||
} | ||
transaction(input, options = {}) { | ||
@@ -132,10 +156,7 @@ const combined = createMutationOperationSet(input); | ||
} | ||
resolveSelectionSet(fields, context) { | ||
return (typeof fields === "function" ? fields(new ContentEntitySelection(context, [])) : fields).selectionSet; | ||
resolveSelection(fields, context) { | ||
return typeof fields === "function" ? fields(new ContentEntitySelection(context, [])) : fields; | ||
} | ||
createContext(name) { | ||
const entity = this.schema.entities[name]; | ||
if (!entity) { | ||
throw new Error(`Entity ${name} not found`); | ||
} | ||
const entity = this.getEntity(name); | ||
return { | ||
@@ -146,2 +167,9 @@ entity, | ||
} | ||
getEntity(name) { | ||
const entity = this.schema.entities[name]; | ||
if (!entity) { | ||
throw new Error(`Entity ${name} not found`); | ||
} | ||
return entity; | ||
} | ||
} | ||
@@ -148,0 +176,0 @@ export { |
@@ -7,5 +7,6 @@ import { createListArgs } from "../utils/createListArgs.js"; | ||
*/ | ||
constructor(context, selectionSet) { | ||
constructor(context, selectionSet, transformFn) { | ||
this.context = context; | ||
this.selectionSet = selectionSet; | ||
this.transformFn = transformFn; | ||
} | ||
@@ -52,2 +53,7 @@ $(field, argsOrSelection, selectionIn) { | ||
} | ||
transform(transform) { | ||
return new ContentEntitySelection(this.context, this.selectionSet, !this.transformFn ? transform : (value, ctx) => { | ||
return transform(this.transformFn(value, ctx), ctx); | ||
}); | ||
} | ||
_column(name, args = {}) { | ||
@@ -86,3 +92,8 @@ let fieldInfo = this.context.entity.fields[name]; | ||
); | ||
return this.withField(newObjectField); | ||
const selectionWithField = this.withField(newObjectField); | ||
const nestedTransform = entitySelection.transformFn; | ||
if (!nestedTransform) { | ||
return selectionWithField; | ||
} | ||
return this.withFieldTransform(alias, (it) => it.map(nestedTransform)); | ||
} | ||
@@ -123,3 +134,8 @@ _manyBy(name, args, fields) { | ||
); | ||
return this.withField(newObjectField); | ||
const selectionWithField = this.withField(newObjectField); | ||
const nestedTransform = entitySelection.transformFn; | ||
if (!nestedTransform) { | ||
return selectionWithField; | ||
} | ||
return this.withFieldTransform(alias, (it, ctx) => it !== null ? nestedTransform(it, ctx) : null); | ||
} | ||
@@ -153,3 +169,8 @@ _one(name, args, fields) { | ||
); | ||
return this.withField(newObjectField); | ||
const selectionWithField = this.withField(newObjectField); | ||
const nestedTransform = entitySelection.transformFn; | ||
if (!nestedTransform) { | ||
return selectionWithField; | ||
} | ||
return this.withFieldTransform(alias, (it, ctx) => it !== null ? nestedTransform(it, ctx) : null); | ||
} | ||
@@ -160,4 +181,14 @@ withField(field) { | ||
field | ||
]); | ||
], this.transformFn); | ||
} | ||
withFieldTransform(alias, transform) { | ||
return new ContentEntitySelection(this.context, this.selectionSet, (value, ctx) => { | ||
const transformedValue = transform(value[alias], ctx); | ||
const newValue = { | ||
...value, | ||
[alias]: transformedValue | ||
}; | ||
return this.transformFn ? this.transformFn(newValue, ctx) : newValue; | ||
}); | ||
} | ||
} | ||
@@ -164,0 +195,0 @@ export { |
const createTypedArgs = (args, types) => { | ||
const typedArgs = {}; | ||
for (const key in args) { | ||
if (!types.hasOwnProperty(key)) { | ||
throw new Error(`Unknown argument ${key}`); | ||
} | ||
typedArgs[key] = { | ||
@@ -5,0 +8,0 @@ graphQlType: types[key], |
@@ -38,4 +38,12 @@ import { createListArgs } from "./utils/createListArgs.js"; | ||
const typedArgs = createListArgs(context.entity, args); | ||
const selectionSet = this.resolveSelectionSet(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selectionSet); | ||
const selection = this.resolveSelection(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selection.selectionSet, (it) => { | ||
const transformFn = selection.transformFn; | ||
if (transformFn) { | ||
return it.map((el) => transformFn(el, { | ||
rootValue: it | ||
})); | ||
} | ||
return it; | ||
}); | ||
} | ||
@@ -49,47 +57,63 @@ get(name, args, fields) { | ||
}); | ||
const selectionSet = this.resolveSelectionSet(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selectionSet); | ||
const selection = this.resolveSelection(fields, context); | ||
return new ContentOperation("query", fieldName, typedArgs, selection.selectionSet, (it) => { | ||
if (it && selection.transformFn) { | ||
return selection.transformFn(it, { | ||
rootValue: it | ||
}); | ||
} | ||
return it; | ||
}); | ||
} | ||
create(name, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `create${name}`; | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
data: `${context.entity.name}CreateInput!` | ||
data: `${entity.name}CreateInput!` | ||
}); | ||
const selectionSet = this.createMutationSelection("create", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "create", typedArgs, fields); | ||
} | ||
update(name, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `update${name}`; | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
data: `${context.entity.name}UpdateInput!`, | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where` | ||
data: `${entity.name}UpdateInput!`, | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where` | ||
}); | ||
const selectionSet = this.createMutationSelection("update", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "update", typedArgs, fields); | ||
} | ||
upsert(name, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `upsert${name}`; | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
update: `${context.entity.name}UpdateInput!`, | ||
create: `${context.entity.name}CreateInput!`, | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where` | ||
update: `${entity.name}UpdateInput!`, | ||
create: `${entity.name}CreateInput!`, | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where` | ||
}); | ||
const selectionSet = this.createMutationSelection("upsert", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "upsert", typedArgs, fields); | ||
} | ||
delete(name, args, fields) { | ||
const context = this.createContext(name); | ||
const entity = this.getEntity(name); | ||
const typedArgs = createTypedArgs(args, { | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where` | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where` | ||
}); | ||
const fieldName = `delete${name}`; | ||
const selectionSet = this.createMutationSelection("delete", fields ? this.resolveSelectionSet(fields, context) : void 0); | ||
return new ContentOperation("mutation", fieldName, typedArgs, selectionSet); | ||
return this.createMutationOperation(name, "delete", typedArgs, fields); | ||
} | ||
createMutationOperation(name, operation, args, fields) { | ||
const context = this.createContext(name); | ||
const fieldName = `${operation}${name}`; | ||
const nodeSelection = fields ? this.resolveSelection(fields, context) : void 0; | ||
const selectionSet = this.createMutationSelection(operation, nodeSelection?.selectionSet); | ||
return new ContentOperation("mutation", fieldName, args, selectionSet, (it) => { | ||
if (!nodeSelection?.transformFn) { | ||
return it; | ||
} | ||
return { | ||
...it, | ||
node: it.node ? nodeSelection.transformFn(it.node, { | ||
rootValue: it.node | ||
}) : null | ||
}; | ||
}); | ||
} | ||
transaction(input, options = {}) { | ||
@@ -132,10 +156,7 @@ const combined = createMutationOperationSet(input); | ||
} | ||
resolveSelectionSet(fields, context) { | ||
return (typeof fields === "function" ? fields(new ContentEntitySelection(context, [])) : fields).selectionSet; | ||
resolveSelection(fields, context) { | ||
return typeof fields === "function" ? fields(new ContentEntitySelection(context, [])) : fields; | ||
} | ||
createContext(name) { | ||
const entity = this.schema.entities[name]; | ||
if (!entity) { | ||
throw new Error(`Entity ${name} not found`); | ||
} | ||
const entity = this.getEntity(name); | ||
return { | ||
@@ -146,2 +167,9 @@ entity, | ||
} | ||
getEntity(name) { | ||
const entity = this.schema.entities[name]; | ||
if (!entity) { | ||
throw new Error(`Entity ${name} not found`); | ||
} | ||
return entity; | ||
} | ||
} | ||
@@ -148,0 +176,0 @@ export { |
@@ -7,5 +7,6 @@ import { createListArgs } from "../utils/createListArgs.js"; | ||
*/ | ||
constructor(context, selectionSet) { | ||
constructor(context, selectionSet, transformFn) { | ||
this.context = context; | ||
this.selectionSet = selectionSet; | ||
this.transformFn = transformFn; | ||
} | ||
@@ -52,2 +53,7 @@ $(field, argsOrSelection, selectionIn) { | ||
} | ||
transform(transform) { | ||
return new ContentEntitySelection(this.context, this.selectionSet, !this.transformFn ? transform : (value, ctx) => { | ||
return transform(this.transformFn(value, ctx), ctx); | ||
}); | ||
} | ||
_column(name, args = {}) { | ||
@@ -86,3 +92,8 @@ let fieldInfo = this.context.entity.fields[name]; | ||
); | ||
return this.withField(newObjectField); | ||
const selectionWithField = this.withField(newObjectField); | ||
const nestedTransform = entitySelection.transformFn; | ||
if (!nestedTransform) { | ||
return selectionWithField; | ||
} | ||
return this.withFieldTransform(alias, (it) => it.map(nestedTransform)); | ||
} | ||
@@ -123,3 +134,8 @@ _manyBy(name, args, fields) { | ||
); | ||
return this.withField(newObjectField); | ||
const selectionWithField = this.withField(newObjectField); | ||
const nestedTransform = entitySelection.transformFn; | ||
if (!nestedTransform) { | ||
return selectionWithField; | ||
} | ||
return this.withFieldTransform(alias, (it, ctx) => it !== null ? nestedTransform(it, ctx) : null); | ||
} | ||
@@ -153,3 +169,8 @@ _one(name, args, fields) { | ||
); | ||
return this.withField(newObjectField); | ||
const selectionWithField = this.withField(newObjectField); | ||
const nestedTransform = entitySelection.transformFn; | ||
if (!nestedTransform) { | ||
return selectionWithField; | ||
} | ||
return this.withFieldTransform(alias, (it, ctx) => it !== null ? nestedTransform(it, ctx) : null); | ||
} | ||
@@ -160,4 +181,14 @@ withField(field) { | ||
field | ||
]); | ||
], this.transformFn); | ||
} | ||
withFieldTransform(alias, transform) { | ||
return new ContentEntitySelection(this.context, this.selectionSet, (value, ctx) => { | ||
const transformedValue = transform(value[alias], ctx); | ||
const newValue = { | ||
...value, | ||
[alias]: transformedValue | ||
}; | ||
return this.transformFn ? this.transformFn(newValue, ctx) : newValue; | ||
}); | ||
} | ||
} | ||
@@ -164,0 +195,0 @@ export { |
const createTypedArgs = (args, types) => { | ||
const typedArgs = {}; | ||
for (const key in args) { | ||
if (!types.hasOwnProperty(key)) { | ||
throw new Error(`Unknown argument ${key}`); | ||
} | ||
typedArgs[key] = { | ||
@@ -5,0 +8,0 @@ graphQlType: types[key], |
@@ -20,7 +20,9 @@ import { ContentClientInput, MutationResult, SchemaNames, TransactionResult } from './types'; | ||
delete(name: string, args: Input.UniqueQueryInput, fields?: EntitySelectionOrCallback): ContentMutation<MutationResult>; | ||
private createMutationOperation; | ||
transaction(input: Record<string, ContentMutation<any> | ContentQuery<any>> | ContentMutation<any> | ContentMutation<any>[], options?: MutationTransactionOptions): ContentMutation<TransactionResult<any>>; | ||
private createMutationSelection; | ||
private resolveSelectionSet; | ||
private resolveSelection; | ||
private createContext; | ||
private getEntity; | ||
} | ||
//# sourceMappingURL=ContentQueryBuilder.d.ts.map |
@@ -26,2 +26,5 @@ import { ContentClientInput, SchemaEntityNames, SchemaNames } from '../types'; | ||
export type ContentEntitySelectionOrCallback = ContentEntitySelectionCallback | ContentEntitySelection; | ||
export type ContentTransformContext = { | ||
rootValue: unknown; | ||
}; | ||
export declare class ContentEntitySelection { | ||
@@ -32,2 +35,4 @@ /** @internal */ | ||
readonly selectionSet: GraphQlSelectionSet; | ||
/** @internal */ | ||
readonly transformFn?: ((value: any, ctx: ContentTransformContext) => any) | undefined; | ||
/** | ||
@@ -40,3 +45,5 @@ * @internal | ||
/** @internal */ | ||
selectionSet: GraphQlSelectionSet); | ||
selectionSet: GraphQlSelectionSet, | ||
/** @internal */ | ||
transformFn?: ((value: any, ctx: ContentTransformContext) => any) | undefined); | ||
$(field: string, args?: EntitySelectionColumnArgs): ContentEntitySelection; | ||
@@ -48,2 +55,3 @@ $(field: string, args: EntitySelectionManyArgs, selection: ContentEntitySelectionOrCallback): ContentEntitySelection; | ||
$$(): ContentEntitySelection; | ||
transform(transform: (value: any, context: ContentTransformContext) => any): ContentEntitySelection; | ||
private _column; | ||
@@ -54,3 +62,4 @@ private _many; | ||
private withField; | ||
private withFieldTransform; | ||
} | ||
//# sourceMappingURL=ContentEntitySelection.d.ts.map |
@@ -10,2 +10,3 @@ import { EntityTypeLike, SchemaTypeLike } from '../types'; | ||
}>; | ||
transform<T>(cb: (value: TValue) => T): TypedEntitySelection<TSchema, TEntityName, TEntity, T>; | ||
$<TNestedValue, TKey extends (keyof TEntity['columns'] | keyof TEntity['hasMany'] | keyof TEntity['hasManyBy'] | keyof TEntity['hasOne']) & string, TAlias extends string | null = null>(name: TKey, ...args: TypedEntitySelectionParams<TSchema, TEntity, TKey, TNestedValue, TAlias>): TypedEntitySelection<TSchema, TEntityName, TEntity, TValue & { | ||
@@ -12,0 +13,0 @@ [key in TAlias extends null ? TKey : TAlias]: TypedEntitySelectionResult<TEntity, TKey, TNestedValue>; |
{ | ||
"name": "@contember/client-content", | ||
"license": "Apache-2.0", | ||
"version": "1.3.0-alpha.26", | ||
"version": "1.3.0-alpha.27", | ||
"main": "./dist/production/index.js", | ||
@@ -38,8 +38,8 @@ "exports": { | ||
"dependencies": { | ||
"@contember/graphql-builder": "1.3.0-alpha.26", | ||
"@contember/graphql-client": "1.3.0-alpha.26", | ||
"@contember/graphql-builder": "1.3.0-alpha.27", | ||
"@contember/graphql-client": "1.3.0-alpha.27", | ||
"@contember/schema": "^1.3.7" | ||
}, | ||
"devDependencies": { | ||
"@contember/client-content-generator": "1.3.0-alpha.26" | ||
"@contember/client-content-generator": "1.3.0-alpha.27" | ||
}, | ||
@@ -46,0 +46,0 @@ "repository": { |
import { ContentClientInput, MutationResult, SchemaNames, TransactionResult } from './types' | ||
import { | ||
ContentEntitySelection, | ||
ContentEntitySelectionCallback, | ||
ContentEntitySelectionContext, | ||
ContentMutation, | ||
ContentOperation, | ||
ContentQuery, | ||
} from './nodes' | ||
import { ContentEntitySelection, ContentEntitySelectionCallback, ContentEntitySelectionContext, ContentMutation, ContentOperation, ContentQuery } from './nodes' | ||
import { createListArgs } from './utils/createListArgs' | ||
import { createTypedArgs } from './utils/createTypedArgs' | ||
import { Input } from '@contember/schema' | ||
import { GraphQlField, GraphQlFragmentSpread, GraphQlSelectionSet } from '@contember/graphql-builder' | ||
import { GraphQlField, GraphQlFieldTypedArgs, GraphQlFragmentSpread, GraphQlSelectionSet } from '@contember/graphql-builder' | ||
import { createMutationOperationSet } from './utils/createMutationOperationSet' | ||
@@ -25,2 +18,4 @@ | ||
type MutationOperation = 'create' | 'update' | 'delete' | 'upsert' | 'transaction'; | ||
export class ContentQueryBuilder { | ||
@@ -65,5 +60,13 @@ constructor(private readonly schema: SchemaNames) { | ||
const typedArgs = createListArgs(context.entity, args) | ||
const selectionSet = this.resolveSelectionSet(fields, context) | ||
const selection = this.resolveSelection(fields, context) | ||
return new ContentOperation('query', fieldName, typedArgs, selectionSet) | ||
return new ContentOperation('query', fieldName, typedArgs, selection.selectionSet, it => { | ||
const transformFn = selection.transformFn | ||
if (transformFn) { | ||
return it.map((el: any) => transformFn(el, { | ||
rootValue: it, | ||
})) | ||
} | ||
return it | ||
}) | ||
} | ||
@@ -79,5 +82,12 @@ | ||
}) | ||
const selectionSet = this.resolveSelectionSet(fields, context) | ||
const selection = this.resolveSelection(fields, context) | ||
return new ContentOperation('query', fieldName, typedArgs, selectionSet) | ||
return new ContentOperation('query', fieldName, typedArgs, selection.selectionSet, it => { | ||
if (it && selection.transformFn) { | ||
return selection.transformFn(it, { | ||
rootValue: it, | ||
}) | ||
} | ||
return it | ||
}) | ||
} | ||
@@ -88,25 +98,19 @@ | ||
const context = this.createContext(name) | ||
const fieldName = `create${name}` | ||
const entity = this.getEntity(name) | ||
const typedArgs = createTypedArgs(args, { | ||
data: `${context.entity.name}CreateInput!`, | ||
data: `${entity.name}CreateInput!`, | ||
}) | ||
const selectionSet = this.createMutationSelection('create', fields ? this.resolveSelectionSet(fields, context) : undefined) | ||
return new ContentOperation('mutation', fieldName, typedArgs, selectionSet) | ||
return this.createMutationOperation(name, 'create', typedArgs, fields) | ||
} | ||
public update(name: string, args: Input.UpdateInput, fields?: EntitySelectionOrCallback): ContentMutation<MutationResult> { | ||
const context = this.createContext(name) | ||
const fieldName = `update${name}` | ||
const entity = this.getEntity(name) | ||
const typedArgs = createTypedArgs(args, { | ||
data: `${context.entity.name}UpdateInput!`, | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where`, | ||
data: `${entity.name}UpdateInput!`, | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where`, | ||
}) | ||
const selectionSet = this.createMutationSelection('update', fields ? this.resolveSelectionSet(fields, context) : undefined) | ||
return new ContentOperation('mutation', fieldName, typedArgs, selectionSet) | ||
return this.createMutationOperation(name, 'update', typedArgs, fields) | ||
} | ||
@@ -117,28 +121,42 @@ | ||
const context = this.createContext(name) | ||
const fieldName = `upsert${name}` | ||
const entity = this.getEntity(name) | ||
const typedArgs = createTypedArgs(args, { | ||
update: `${context.entity.name}UpdateInput!`, | ||
create: `${context.entity.name}CreateInput!`, | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where`, | ||
update: `${entity.name}UpdateInput!`, | ||
create: `${entity.name}CreateInput!`, | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where`, | ||
}) | ||
return this.createMutationOperation(name, 'upsert', typedArgs, fields) | ||
} | ||
const selectionSet = this.createMutationSelection('upsert', fields ? this.resolveSelectionSet(fields, context) : undefined) | ||
return new ContentOperation('mutation', fieldName, typedArgs, selectionSet) | ||
public delete(name: string, args: Input.UniqueQueryInput, fields?: EntitySelectionOrCallback): ContentMutation<MutationResult> { | ||
const entity = this.getEntity(name) | ||
const typedArgs = createTypedArgs(args, { | ||
by: `${entity.name}UniqueWhere!`, | ||
filter: `${entity.name}Where`, | ||
}) | ||
return this.createMutationOperation(name, 'delete', typedArgs, fields) | ||
} | ||
public delete(name: string, args: Input.UniqueQueryInput, fields?: EntitySelectionOrCallback): ContentMutation<MutationResult> { | ||
private createMutationOperation(name: string, operation: MutationOperation, args: GraphQlFieldTypedArgs, fields?: EntitySelectionOrCallback): ContentMutation<MutationResult> { | ||
const context = this.createContext(name) | ||
const typedArgs = createTypedArgs(args, { | ||
by: `${context.entity.name}UniqueWhere!`, | ||
filter: `${context.entity.name}Where`, | ||
const fieldName = `${operation}${name}` | ||
const nodeSelection = fields ? this.resolveSelection(fields, context) : undefined | ||
const selectionSet = this.createMutationSelection(operation, nodeSelection?.selectionSet) | ||
return new ContentOperation('mutation', fieldName, args, selectionSet, it => { | ||
if (!nodeSelection?.transformFn) { | ||
return it | ||
} | ||
return { | ||
...it, | ||
node: it.node ? nodeSelection.transformFn(it.node, { | ||
rootValue: it.node, | ||
}) : null, | ||
} | ||
}) | ||
const fieldName = `delete${name}` | ||
const selectionSet = this.createMutationSelection('delete', fields ? this.resolveSelectionSet(fields, context) : undefined) | ||
return new ContentOperation('mutation', fieldName, typedArgs, selectionSet) | ||
} | ||
@@ -170,3 +188,3 @@ | ||
private createMutationSelection(operation: 'create' | 'update' | 'delete' | 'upsert' | 'transaction', selection?: GraphQlSelectionSet): GraphQlSelectionSet { | ||
private createMutationSelection(operation: MutationOperation, selection?: GraphQlSelectionSet): GraphQlSelectionSet { | ||
const items: GraphQlSelectionSet = [ | ||
@@ -191,7 +209,7 @@ new GraphQlField(null, 'ok'), | ||
private resolveSelectionSet( | ||
private resolveSelection( | ||
fields: EntitySelectionOrCallback, | ||
context: ContentEntitySelectionContext<string>, | ||
) { | ||
return (typeof fields === 'function' ? fields(new ContentEntitySelection(context, [])) : fields).selectionSet | ||
return (typeof fields === 'function' ? fields(new ContentEntitySelection(context, [])) : fields) | ||
} | ||
@@ -202,6 +220,3 @@ | ||
): ContentEntitySelectionContext<string> { | ||
const entity = this.schema.entities[name] | ||
if (!entity) { | ||
throw new Error(`Entity ${name} not found`) | ||
} | ||
const entity = this.getEntity(name) | ||
return { | ||
@@ -212,2 +227,10 @@ entity: entity, | ||
} | ||
private getEntity(name: string) { | ||
const entity = this.schema.entities[name] | ||
if (!entity) { | ||
throw new Error(`Entity ${name} not found`) | ||
} | ||
return entity | ||
} | ||
} |
@@ -31,2 +31,6 @@ import { createListArgs } from '../utils/createListArgs' | ||
export type ContentTransformContext = { | ||
rootValue: unknown | ||
} | ||
export class ContentEntitySelection { | ||
@@ -42,2 +46,4 @@ | ||
public readonly selectionSet: GraphQlSelectionSet, | ||
/** @internal */ | ||
public readonly transformFn?: (value: any, ctx: ContentTransformContext) => any, | ||
) { | ||
@@ -98,2 +104,9 @@ } | ||
transform(transform: (value: any, context: ContentTransformContext) => any): ContentEntitySelection { | ||
return new ContentEntitySelection(this.context, this.selectionSet, !this.transformFn ? transform : (value, ctx) => { | ||
return transform(this.transformFn!(value, ctx), ctx) | ||
}) | ||
} | ||
private _column( | ||
@@ -147,3 +160,8 @@ name: string, | ||
) | ||
return this.withField(newObjectField) | ||
const selectionWithField = this.withField(newObjectField) | ||
const nestedTransform = entitySelection.transformFn | ||
if (!nestedTransform) { | ||
return selectionWithField | ||
} | ||
return this.withFieldTransform(alias, it => it.map(nestedTransform)) | ||
} | ||
@@ -195,3 +213,8 @@ | ||
) | ||
return this.withField(newObjectField) | ||
const selectionWithField = this.withField(newObjectField) | ||
const nestedTransform = entitySelection.transformFn | ||
if (!nestedTransform) { | ||
return selectionWithField | ||
} | ||
return this.withFieldTransform(alias, (it, ctx) => it !== null ? nestedTransform(it, ctx) : null) | ||
} | ||
@@ -235,3 +258,8 @@ | ||
) | ||
return this.withField(newObjectField) | ||
const selectionWithField = this.withField(newObjectField) | ||
const nestedTransform = entitySelection.transformFn | ||
if (!nestedTransform) { | ||
return selectionWithField | ||
} | ||
return this.withFieldTransform(alias, (it, ctx) => it !== null ? nestedTransform(it, ctx) : null) | ||
} | ||
@@ -244,4 +272,15 @@ | ||
field, | ||
]) | ||
], this.transformFn) | ||
} | ||
private withFieldTransform(alias: string, transform: (value: any, ctx: ContentTransformContext) => any) { | ||
return new ContentEntitySelection(this.context, this.selectionSet, (value, ctx) => { | ||
const transformedValue = transform(value[alias], ctx) | ||
const newValue = { | ||
...value, | ||
[alias]: transformedValue, | ||
} | ||
return this.transformFn ? this.transformFn(newValue, ctx) : newValue | ||
}) | ||
} | ||
} |
@@ -15,2 +15,5 @@ import { EntityTypeLike, SchemaTypeLike } from '../types' | ||
transform<T>(cb: (value: TValue) => T): TypedEntitySelection<TSchema, TEntityName, TEntity, T> | ||
$< | ||
@@ -17,0 +20,0 @@ TNestedValue, |
@@ -9,2 +9,5 @@ import { GraphQlFieldTypedArgs } from '@contember/graphql-builder' | ||
for (const key in args) { | ||
if (!types.hasOwnProperty(key)) { | ||
throw new Error(`Unknown argument ${key}`) | ||
} | ||
typedArgs[key] = { | ||
@@ -11,0 +14,0 @@ graphQlType: types[key], |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
382411
4007
+ Added@contember/graphql-builder@1.3.0-alpha.27(transitive)
+ Added@contember/graphql-client@1.3.0-alpha.27(transitive)
- Removed@contember/graphql-builder@1.3.0-alpha.26(transitive)
- Removed@contember/graphql-client@1.3.0-alpha.26(transitive)