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.
- Loading branch information
Showing
5 changed files
with
85 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ | |
} | ||
} | ||
} | ||
} | ||
} |
74 changes: 43 additions & 31 deletions
74
packages/datastore/cli/src/generate-documents/createJsonSchema.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,49 +1,61 @@ | ||
import { ModelDefinition, getPrimaryKey } from "@graphback/core"; | ||
import { | ||
isNonNullType, | ||
GraphQLOutputType, | ||
getNullableType | ||
isNonNullType, | ||
GraphQLOutputType, | ||
getNullableType | ||
} from "graphql"; | ||
import { convertToTsType } from "../utils"; | ||
|
||
|
||
const getFieldParameters = (fieldName: string, type: GraphQLOutputType): any => { | ||
const options: any = {}; | ||
const options: any = {}; | ||
|
||
// TODO handle relationships | ||
// TODO handle relationships | ||
|
||
options.key = fieldName; | ||
options.key = fieldName; | ||
|
||
if (isNonNullType(type)) { | ||
type = getNullableType(type); | ||
options.isRequired = true; | ||
} | ||
if (isNonNullType(type)) { | ||
type = getNullableType(type); | ||
options.isRequired = true; | ||
} | ||
|
||
return { type: convertToTsType(type), ...options }; | ||
return { type: convertToTsType(type), ...options }; | ||
}; | ||
|
||
const getModelProperties = (model: ModelDefinition, primaryKey: string) => { | ||
const fieldMap = model.graphqlType.getFields(); | ||
|
||
return Object.keys(fieldMap) | ||
.map(fieldName => { | ||
const fieldOptions = getFieldParameters(fieldName, fieldMap[fieldName].type); | ||
if (fieldName === primaryKey) { | ||
fieldOptions.primary = true; | ||
} | ||
return { [fieldName]: fieldOptions }; | ||
}) | ||
.reduce((prev, current) => ({ ...prev, ...current }), {}); | ||
const fieldMap = model.graphqlType.getFields(); | ||
|
||
const generatedProperties = Object.keys(fieldMap) | ||
.map(fieldName => { | ||
const fieldOptions = getFieldParameters(fieldName, fieldMap[fieldName].type); | ||
if (fieldName === primaryKey) { | ||
fieldOptions.primary = true; | ||
} | ||
return { [fieldName]: fieldOptions }; | ||
}) | ||
.reduce((prev, current) => ({ ...prev, ...current }), {}); | ||
|
||
generatedProperties._version = { | ||
type: "string", | ||
key: "_version", | ||
isRequired: true | ||
}; | ||
generatedProperties._deleted = { | ||
type: "string", | ||
key: "_version", | ||
isRequired: true | ||
}; | ||
return generatedProperties; | ||
}; | ||
|
||
export const createJsonSchema = (model: ModelDefinition) => { | ||
const primaryKey = getPrimaryKey(model.graphqlType).name; | ||
|
||
return { | ||
name: model.graphqlType.name, | ||
version: 1, | ||
type: "object", | ||
primaryKey: primaryKey, | ||
properties: getModelProperties(model, primaryKey) | ||
}; | ||
const primaryKey = getPrimaryKey(model.graphqlType).name; | ||
|
||
return { | ||
name: model.graphqlType.name, | ||
version: 1, | ||
type: "object", | ||
primaryKey: primaryKey, | ||
properties: getModelProperties(model, primaryKey) | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
import * as util from 'util'; | ||
import { mkdirSync, rmdirSync, readFileSync } from "fs"; | ||
import { mkdirSync, rmdirSync, readFileSync, existsSync } from "fs"; | ||
import { join } from 'path'; | ||
|
||
beforeEach(() => mkdirSync("./output/")); | ||
beforeEach(() => { | ||
if (!existsSync("./output/")) | ||
mkdirSync("./output/") | ||
}); | ||
afterEach(() => rmdirSync("./output/", { recursive: true })); | ||
|
||
test("generate", async () => { | ||
const exec = util.promisify(require('child_process').exec); | ||
const cliExec = join(__dirname, '../dist/index.js'); | ||
const schemaPath = join(__dirname, './mock.graphql'); | ||
const exec = util.promisify(require('child_process').exec); | ||
const cliExec = join(__dirname, '../bin/offix.js'); | ||
const schemaPath = join(__dirname, './mock.graphql'); | ||
|
||
await exec(`node ${cliExec} generate --schema ${schemaPath} --outputPath output/`); | ||
const schema = readFileSync("./output/schema.json").toString(); | ||
const index = readFileSync("./output/index.ts").toString(); | ||
const types = readFileSync("./output/types.ts").toString(); | ||
await exec(`node ${cliExec} generate --schema ${schemaPath} --outputPath output/`); | ||
const schema = readFileSync("./output/schema.json").toString(); | ||
const index = readFileSync("./output/index.ts").toString(); | ||
const types = readFileSync("./output/types.ts").toString(); | ||
|
||
expect(types).toMatchSnapshot(); | ||
expect(schema).toMatchSnapshot(); | ||
expect(index).toMatchSnapshot(); | ||
expect(types).toMatchSnapshot(); | ||
expect(schema).toMatchSnapshot(); | ||
expect(index).toMatchSnapshot(); | ||
}); |
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