-
Notifications
You must be signed in to change notification settings - Fork 45
WIP: generated relationship details for schema #899
Changes from all commits
f077c3a
68fc50f
0f0e05f
0d47969
593eee5
d218ecc
bc236fa
78b13ec
d3e33eb
501705a
a3b8ef4
c5521c3
76b392b
2d0f1b1
a27ffa4
a8c9772
af7ad8a
c681070
7ad987b
7fbe7f8
2c851d6
015df2a
500bc29
fcef636
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,9 @@ import { | |
} from "graphql"; | ||
import { convertToTsType } from "../utils"; | ||
|
||
|
||
const getFieldParameters = (fieldName: string, type: GraphQLOutputType): any => { | ||
const options: any = {}; | ||
|
||
// TODO handle relationships | ||
|
||
options.key = fieldName; | ||
|
||
if (isNonNullType(type)) { | ||
|
@@ -24,22 +21,53 @@ const getFieldParameters = (fieldName: string, type: GraphQLOutputType): any => | |
|
||
const getModelProperties = (model: ModelDefinition, primaryKey: string) => { | ||
const fieldMap = model.graphqlType.getFields(); | ||
const keys = Object.keys(fieldMap); | ||
// get a list of relationships | ||
const relNames = model.relationships.map(r => r.ownerField.name); | ||
|
||
const generatedProperties = Object.keys(fieldMap) | ||
// loop through graphback relationship types | ||
const relationships = model.relationships | ||
// filter out oneToMany relationships | ||
.filter(r => r.kind !== "oneToMany") | ||
// generate key/value pair with foreign key as key | ||
.map(r => { | ||
const fieldOptions = getFieldParameters( | ||
r.relationForeignKey!, | ||
r.relationType | ||
); | ||
fieldOptions.relationship = r.relationType; | ||
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. @craicoverflow can you take look if this would work from your side. 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. I'm not entirely sure yet, I think this might generate extra fields which are not part of a model. A For example, in a 1:M relationship between Note.comments and Comment.note, the I think once we have a unit test in for this API, it will be easier to verify this. 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. Yes you're correct @craicoverflow I had to filter out the side of the relationship that I didn't need here. But the relationship info went both ways oneToMany and manyToOne 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. Is this the filtering you meant @kingsleyzissou ? From looking at that I had thought it was filtering out the relationship information that Offix already had and did not want to apply twice. 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. Yeah that's the filtering I meant @craicoverflow . As far as I can remember, we were having some issues with the comments array being included in our Datastore model and we needed to remove it. So it isn't actually duplicate data at all. We are only adding the foreign key information the the notes so we can filter those out based on the parent id, in this case the Task. 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. See #900 - should this test be passing or am I doing something wrong? 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. The test should be passing. I'm going to fix the linting and snapshot issues and then I can debug that |
||
return { [r.relationForeignKey!]: fieldOptions }; | ||
}); | ||
|
||
const generatedProperties = keys | ||
// filter out autogenerated relationship properties | ||
.filter(fieldName => !relNames.includes(fieldName)) | ||
.map(fieldName => { | ||
const fieldOptions = getFieldParameters(fieldName, fieldMap[fieldName].type); | ||
const fieldOptions = getFieldParameters( | ||
fieldName, | ||
fieldMap[fieldName].type | ||
); | ||
if (fieldName === primaryKey) { | ||
fieldOptions.primary = true; | ||
} | ||
return { [fieldName]: fieldOptions }; | ||
}) | ||
// add previously created relationship key/values | ||
.concat(relationships) | ||
.reduce((prev, current) => ({ ...prev, ...current }), {}); | ||
|
||
generatedProperties._version = { | ||
type: "string", | ||
key: "_version", | ||
isRequired: true | ||
}; | ||
|
||
generatedProperties._lastUpdatedAt = { | ||
type: "number", | ||
key: "_lastUpdatedAt", | ||
isRequired: true | ||
}; | ||
|
||
return generatedProperties; | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,28 @@ | ||
import { ModelDefinition, getPrimaryKey } from "@graphback/core"; | ||
import { GraphQLOutputType, isNonNullType, getNullableType } from "graphql"; | ||
import { convertToTsType } from "../utils"; | ||
import endent from "endent"; | ||
|
||
const getField = (fieldName: string, type: GraphQLOutputType) => { | ||
if (isNonNullType(type)) { | ||
type = getNullableType(type); | ||
} else { | ||
fieldName = `${fieldName}?`; | ||
} | ||
const getModelProperties = (schema: any) => { | ||
const fieldMap = schema.properties; | ||
const keys = Object.keys(fieldMap); | ||
|
||
return `${fieldName}: ${convertToTsType(type)}`; | ||
return keys | ||
.map(fieldName => { | ||
const s = fieldMap[fieldName]; | ||
const name = s.isRequired ? fieldName : `${fieldName}?`; | ||
return `${name}: ${s.type}`; | ||
}) | ||
.join(";\n"); | ||
}; | ||
|
||
const getModelProperties = (model: ModelDefinition) => { | ||
const fieldMap = model.graphqlType.getFields(); | ||
export const createModelType = (schema: any) => { | ||
const { name: modelName, primaryKey } = schema; | ||
|
||
return Object.keys(fieldMap) | ||
.map(fieldName => (getField(fieldName, fieldMap[fieldName].type))) | ||
.join(";\n "); | ||
}; | ||
|
||
export const createModelType = (model: ModelDefinition) => { | ||
const modelName = model.graphqlType.name; | ||
const primaryKey = getPrimaryKey(model.graphqlType).name; | ||
return endent` | ||
export interface ${modelName} { | ||
${getModelProperties(schema)} | ||
} | ||
|
||
return `export interface ${modelName} { | ||
${getModelProperties(model)} | ||
_version: number; | ||
} | ||
export type ${modelName}Create = ${primaryKey ? `Omit<${modelName}, "${primaryKey}">` : modelName}; | ||
export type ${modelName}Change = ${primaryKey ? `Pick<${modelName}, "${primaryKey}"> & ` : ""}Partial<${modelName}Create>; | ||
|
||
export type ${modelName}Create = ${primaryKey ? `Omit<${modelName}, "${primaryKey}">` : modelName}; | ||
export type ${modelName}Change = ${primaryKey ? `Pick<${modelName}, "${primaryKey}"> & ` : ""}Partial<${modelName}Create>; | ||
`; | ||
`; | ||
}; |
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.
This is amazing. We do not need to use delta type and getting all the information.