Skip to content

Commit

Permalink
Only skip immutable fields in partial mutations inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed Feb 7, 2024
1 parent 60e1584 commit 8634d8f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
32 changes: 19 additions & 13 deletions packages/devtools/src/formats/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type ExtractSchemaParams = {
ownName?: string
required?: boolean
localRef?: boolean
immutable?: boolean
}

type RuntimeModelDefinition = {
Expand Down Expand Up @@ -80,12 +81,13 @@ export class RuntimeModelBuilder {
this.#commonEmbeds = params.commonEmbeds ?? []
this.#modelName = params.name
this.#modelRelations = params.definition.relations ?? {}
if (params.definition.version !== "1.0") {
this.#immutableFields = params.definition.immutableFields?? []
}
this.#modelSchema = params.definition.schema
this.#modelViews = params.views
this.#modelIndices = params.indices
this.#immutableFields =
// TODO: remove @ts-ignore once model definition is updated
// @ts-ignore added in new version of the model definition
params.definition.version === '1.0' ? [] : params.definition.immutableFields ?? []
}

build(): RuntimeModelDefinition {
Expand Down Expand Up @@ -129,6 +131,7 @@ export class RuntimeModelBuilder {
ownName: propKey,
parentName: ownName,
required: requiredProps.includes(propKey),
immutable: this.#immutableFields.includes(propKey),
})
}
return fields
Expand All @@ -154,11 +157,16 @@ export class RuntimeModelBuilder {
}

const required = params.required ?? false
const immutable = params.ownName? this.#immutableFields.includes(params.ownName): false
const immutable = params.immutable ?? false
const items = schema.items as AnySchema

if (items.$ref != null) {
return { type: 'list', required, immutable, item: this._buildListReference(items.$ref, params) }
return {
type: 'list',
required,
immutable,
item: this._buildListReference(items.$ref, params),
}
}
if (items.type == null) {
throw new Error('Missing schema $ref or type for array items')
Expand Down Expand Up @@ -210,7 +218,7 @@ export class RuntimeModelBuilder {
refType: 'object',
refName: ownName,
required: params.required ?? false,
immutable: params.ownName? this.#immutableFields.includes(params.ownName): false
immutable: params.immutable ?? false,
}
}

Expand All @@ -227,7 +235,7 @@ export class RuntimeModelBuilder {
refType: 'enum',
refName: ownName,
required: params.required ?? false,
immutable: params.ownName? this.#immutableFields.includes(params.ownName): false
immutable: params.immutable ?? false,
}
}

Expand All @@ -253,7 +261,7 @@ export class RuntimeModelBuilder {
}

const required = params.required ?? false
const immutable = params.ownName? this.#immutableFields.includes(params.ownName): false
const immutable = params.immutable ?? false

switch (schema.type) {
case 'boolean':
Expand Down Expand Up @@ -322,16 +330,14 @@ export function createRuntimeDefinition(
for (const [modelID, modelDefinition] of Object.entries(definition.models)) {
const modelName = definition.aliases?.[modelID] ?? modelDefinition.name
const isV1 = modelDefinition.version === '1.0'
const interfaceDefinition =
isV1
? { interface: false, implements: [] }
: { interface: modelDefinition.interface, implements: modelDefinition.implements }
const interfaceDefinition = isV1
? { interface: false, implements: [] }
: { interface: modelDefinition.interface, implements: modelDefinition.implements }
// Add name to model metadata mapping
runtime.models[modelName] = {
...interfaceDefinition,
id: modelID,
accountRelation: modelDefinition.accountRelation,
immutableFields: !isV1? modelDefinition.immutableFields: [],
}

// Extract objects, enums, relations and views from model schema
Expand Down
4 changes: 3 additions & 1 deletion packages/devtools/src/schema/resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ function executeCreateFactory(
name: sourceDefinition.name,
description: sourceDefinition.description,
accountRelation: sourceDefinition.accountRelation,
immutableFields: !isV1 ? (sourceDefinition as ModelDefinitionV2).immutableFields: [],
interface: isV1 ? false : sourceDefinition.interface,
implements: implementIDs,
schema: sourceDefinition.schema,
// TODO: remove @ts-ignore once model definition is updated
// @ts-ignore added in new version of the model definition
immutableFields: isV1 ? [] : sourceDefinition.immutableFields,
relations,
views,
}
Expand Down
13 changes: 8 additions & 5 deletions packages/runtime/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,9 @@ class SchemaBuilder {
},
}
for (const [key, field] of Object.entries(fields)) {
// Don't show meta or immutable fields in schema
if (field.type == 'meta' || (field as any).immutable) continue

switch (field.type) {
case 'meta':
break
case 'reference':
config[key] = this._buildDocumentObjectReferenceField(key, field)
break
Expand Down Expand Up @@ -945,7 +944,12 @@ class SchemaBuilder {
const inputPrefix = isDocument || required ? '' : 'Partial'

for (const [key, field] of Object.entries(fields)) {
let type//TODO should it also go here?
if (!required && (field as RuntimeScalarCommon).immutable) {
// Skip immutable fields from partial inputs
continue
}

let type
switch (field.type) {
case 'meta':
case 'view':
Expand Down Expand Up @@ -1077,7 +1081,6 @@ class SchemaBuilder {
name: string,
model: RuntimeModel,
) {
console.trace("i came here")
switch (model.accountRelation.type) {
case 'list':
this.#mutations[`create${name}`] = mutationWithClientMutationId({
Expand Down
1 change: 0 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ export type RuntimeModel = {
interface: boolean
implements: Array<string>
accountRelation: ModelAccountRelationV2
immutableFields?: Array<string>
}

/**
Expand Down

0 comments on commit 8634d8f

Please sign in to comment.