Skip to content

Commit

Permalink
Feat/schema (#270)
Browse files Browse the repository at this point in the history
* ✨ NEW: feature schema

* ✏️ DOC: changeset

* 👌 FIX: lint

* 🚧 FIX: prettier

* 🚧 FIX: prettier
  • Loading branch information
jycouet committed Nov 14, 2022
1 parent 31c178a commit a62009a
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-dingos-kick.md
@@ -0,0 +1,5 @@
---
'@kitql/all-in': patch
---

add schema option to the handler if you don't want to work with modules
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -14,3 +14,4 @@ README.md

$houdini
$kitql
packages/all-in/src/lib/modules/_enums/**/*
20 changes: 20 additions & 0 deletions packages/all-in/prisma/schema.prisma
@@ -0,0 +1,20 @@
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model test {
id String @id @default(cuid())
name String
my_demo my_demo_enum
}

enum my_demo_enum {
ONE
TWO
FOUR
}
35 changes: 21 additions & 14 deletions packages/all-in/src/lib/hooks/graphql.ts
Expand Up @@ -23,6 +23,12 @@ export type KitQLHandleGraphQL<TUserContext, TServerContext extends Record<strin
*/
endpoint?: string

/**
* If you want to use your own schema, you can pass it here.
* let's have a look at the type only after Yoga v3 is out!
*/
schema: any

/**
* THE context.
*/
Expand All @@ -41,13 +47,26 @@ export function handleGraphql<TUserContext, TServerContext>(
options?: KitQLHandleGraphQL<TUserContext, TServerContext>
): Handle {
// set defaults
const { graphiQLPath, endpoint, plugins, context } = {
const { graphiQLPath, endpoint, plugins, context, schema } = {
graphiQLPath: undefined,
endpoint: '/api/graphql',
plugins: [],
context: () => {
return {} as TUserContext
},
schema: createSchema({
typeDefs: `
type Query {
_greetings: String
}
`,
resolvers: {
Query: {
_greetings: () =>
'Yes yoga is up and running! Now, to make it work with your own schema, you need to `useKitqlModules(modules)` via plugins',
},
},
}),
...options,
}

Expand All @@ -65,19 +84,7 @@ export function handleGraphql<TUserContext, TServerContext>(
const kitqlServer = createYoga<YogaInitialContext, TUserContext>({
logging: true,
// will be overwritten by modules
schema: createSchema({
typeDefs: `
type Query {
_greetings: String
}
`,
resolvers: {
Query: {
_greetings: () =>
'Yes yoga is up and running! Now, to make it work with your own schema, you need to `useKitqlModules(modules)` via plugins',
},
},
}),
schema,
context,
plugins: kitqlPlugins.concat(plugins || []),
graphqlEndpoint: endpoint,
Expand Down
1 change: 1 addition & 0 deletions packages/all-in/src/lib/index.ts
Expand Up @@ -30,3 +30,4 @@ export type { KitQLVite } from './vite/KitQLVite.js'

// graphql-yoga
export type { Plugin as YogaPlugin } from 'graphql-yoga'
export { createSchema } from 'graphql-yoga'
8 changes: 8 additions & 0 deletions packages/all-in/src/lib/modules/_enums/index.ts
@@ -0,0 +1,8 @@
import { createModule } from 'graphql-modules'

import { typeDefs } from './$kitql/typedefs'

export const _enumsModule = createModule({
id: 'enums-module',
typeDefs
})
@@ -0,0 +1,5 @@
enum MyDemoEnum {
ONE
TWO
FOUR
}
7 changes: 7 additions & 0 deletions packages/all-in/src/lib/modules/_enums/ui/lists/MyDemoList.ts
@@ -0,0 +1,7 @@
import { type MyDemoEnum } from '$graphql/$kitql/graphqlTypes'

export const MyDemoList: Record<MyDemoEnum, string> = {
ONE: 'One',
TWO: 'Two',
FOUR: 'Four',
}
11 changes: 6 additions & 5 deletions packages/all-in/src/lib/vite/KitQLVite.ts
@@ -1,7 +1,7 @@
export type TEnumsModuleConfig = {
prismaFile: string
enumsModuleFolder: string
}
// export type TEnumsModuleConfig = {
// prismaFile: string
// enumsModuleFolder: string
// }

export type KitQLVite = {
projectName?: string
Expand All @@ -11,7 +11,8 @@ export type KitQLVite = {
// importBaseTypesFrom?: string
// modules?: string[]

createEnumsModule?: TEnumsModuleConfig | false
prismaFileForEnums?: string | false
// createEnumsModule?: TEnumsModuleConfig | false
// mergeModuleTypedefs?: boolean
// mergeModuleResolvers?: boolean
// mergeContexts?: boolean
Expand Down
19 changes: 12 additions & 7 deletions packages/all-in/src/lib/vite/actionEnum.ts
Expand Up @@ -9,7 +9,8 @@ export function actionEnum(
enumsModuleFolder: string,
moduleOutputFolder: string,
importBaseTypesFrom: string,
enums: Record<string, string[]>
enums: Record<string, string[]>,
localDev: boolean
) {
// Typedefs
createFolderIfNotExists(join(enumsModuleFolder, '_enums'))
Expand Down Expand Up @@ -38,26 +39,30 @@ export function actionEnum(
const keyWOEnum = key.replace('Enum', '')
const enumFileData = []

enumFileData.push(`import { type ${key} } from '${importBaseTypesFrom}';`)
const line1 = `import { type ${key} } from '${importBaseTypesFrom}';`
enumFileData.push(line1)
enumFileData.push(``)
enumFileData.push(`export const ${keyWOEnum}List: Record<${key}, string> = {`)
const line2 = `export const ${keyWOEnum}List: Record<${key}, string> = `
enumFileData.push(`${line2} {`)
list.forEach((c, i) => {
const isLast = i === list.length - 1
enumFileData.push(`\t${c}: '${toPascalCase(c.toLowerCase())}'${isLast ? '' : ','}`)
})
enumFileData.push(`};`)
enumFileData.push(`}`)
enumFileData.push(``)

const filePath = join(enumsModuleFolder, '_enums', 'ui', 'lists', `${keyWOEnum}List.ts`)

// Write this file only if it doesn't exist!
// Like this, you can change the value with text that will be displayed in the UI!
if (!existsSync(join(enumsModuleFolder, '_enums', 'ui', 'lists', `${keyWOEnum}List.ts`))) {
write(join(enumsModuleFolder, '_enums', 'ui', 'lists', `${keyWOEnum}List.ts`), enumFileData)
if (!existsSync(filePath)) {
write(filePath, enumFileData)
}
}

// Index
const enumFileData = []
enumFileData.push(`import { createModule } from 'graphql-modules'`)
enumFileData.push(`import { createModule } from ${localDev ? `'graphql-modules'` : `'@kitql/all-in'`}`)
enumFileData.push(``)
enumFileData.push(`import { typeDefs } from './${moduleOutputFolder}/typedefs'`)
enumFileData.push(``)
Expand Down
23 changes: 10 additions & 13 deletions packages/all-in/src/lib/vite/generate.ts
Expand Up @@ -42,22 +42,19 @@ export function generate(log: Log, config?: KitQLVite) {
}

// Enums
if (config?.createEnumsModule) {
const { prismaFile, enumsModuleFolder } = {
prismaFile: '',
enumsModuleFolder: '',
...config?.createEnumsModule,
}

const prismaFilePath = getFullPath(prismaFile)
if (readLines(prismaFilePath).length === 0) {
const enums = getPrismaEnum(readLines(prismaFilePath))
const enumsKeys = actionEnum(enumsModuleFolder, moduleOutputFolder, importBaseTypesFrom, enums)
if (config?.prismaFileForEnums) {
const enumsModuleFolder = './src/lib/modules'
const prismaFilePath = getFullPath(config.prismaFileForEnums)
const prismaFileContent = readLines(prismaFilePath)

if (prismaFileContent.length !== 0) {
const enums = getPrismaEnum(prismaFileContent)
const enumsKeys = actionEnum(enumsModuleFolder, moduleOutputFolder, importBaseTypesFrom, enums, localDev)
meta.enums = enumsKeys.length
// log.info(`${logGreen('✔')} ${logGreen('Enums')} created [${enumsKeys.map(c => logGreen(c)).join(',')}]`)
} else {
log.error(`${'❌'} file ${logRed(prismaFilePath)} not found!`)
throw new Error(`file ${prismaFilePath} not found!`)
log.error(`file ${logRed(prismaFilePath)} not found!`)
// throw new Error(`file ${prismaFilePath} not found!`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/all-in/vite.config.ts
Expand Up @@ -6,7 +6,7 @@ import { kitql } from './src/lib/vite/plugin.js'
// import { kitql } from '@kitql/all-in'

const config: UserConfig = {
plugins: [kitql({ projectName: 'myPrj', localDev: true }), sveltekit()],
plugins: [kitql({ projectName: 'myPrj', localDev: true, prismaFileForEnums: './prisma/schema.prisma' }), sveltekit()],

optimizeDeps: {
include: ['safe-stable-stringify'],
Expand Down

1 comment on commit a62009a

@vercel
Copy link

@vercel vercel bot commented on a62009a Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kitql – ./

kitql.vercel.app
kitql-git-main-jycouet.vercel.app
kitql-jycouet.vercel.app
www.kitql.dev
kitql.dev

Please sign in to comment.