This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #899 from aerogear/schema-relationships
WIP: generated relationship details for schema
- Loading branch information
Showing
7 changed files
with
769 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 20 additions & 27 deletions
47
packages/datastore/cli/src/generate-documents/createModelTypes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
`; | ||
`; | ||
}; |
Oops, something went wrong.