Initial commit
This commit is contained in:
24
api/db/schema.prisma
Normal file
24
api/db/schema.prisma
Normal file
@ -0,0 +1,24 @@
|
||||
// Don't forget to tell Prisma about your edits to this file using
|
||||
// `yarn rw prisma migrate dev` or `yarn rw prisma db push`.
|
||||
// `migrate` is like committing while `push` is for prototyping.
|
||||
// Read more about both here:
|
||||
// https://www.prisma.io/docs/orm/prisma-migrate
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
binaryTargets = "native"
|
||||
}
|
||||
|
||||
// Define your own datamodels here and run `yarn redwood prisma migrate dev`
|
||||
// to create migrations for them and apply to your dev DB.
|
||||
// TODO: Please remove the following example:
|
||||
model UserExample {
|
||||
id Int @id @default(autoincrement())
|
||||
email String @unique
|
||||
name String?
|
||||
}
|
8
api/jest.config.js
Normal file
8
api/jest.config.js
Normal file
@ -0,0 +1,8 @@
|
||||
// More info at https://redwoodjs.com/docs/project-configuration-dev-test-build
|
||||
|
||||
const config = {
|
||||
rootDir: '../',
|
||||
preset: '@redwoodjs/testing/config/jest/api',
|
||||
}
|
||||
|
||||
module.exports = config
|
9
api/package.json
Normal file
9
api/package.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "api",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@redwoodjs/api": "7.7.3",
|
||||
"@redwoodjs/graphql-server": "7.7.3"
|
||||
}
|
||||
}
|
18
api/src/directives/requireAuth/requireAuth.test.ts
Normal file
18
api/src/directives/requireAuth/requireAuth.test.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { mockRedwoodDirective, getDirectiveName } from '@redwoodjs/testing/api'
|
||||
|
||||
import requireAuth from './requireAuth'
|
||||
|
||||
describe('requireAuth directive', () => {
|
||||
it('declares the directive sdl as schema, with the correct name', () => {
|
||||
expect(requireAuth.schema).toBeTruthy()
|
||||
expect(getDirectiveName(requireAuth.schema)).toBe('requireAuth')
|
||||
})
|
||||
|
||||
it('requireAuth has stub implementation. Should not throw when current user', () => {
|
||||
// If you want to set values in context, pass it through e.g.
|
||||
// mockRedwoodDirective(requireAuth, { context: { currentUser: { id: 1, name: 'Lebron McGretzky' } }})
|
||||
const mockExecution = mockRedwoodDirective(requireAuth, { context: {} })
|
||||
|
||||
expect(mockExecution).not.toThrowError()
|
||||
})
|
||||
})
|
25
api/src/directives/requireAuth/requireAuth.ts
Normal file
25
api/src/directives/requireAuth/requireAuth.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import gql from 'graphql-tag'
|
||||
|
||||
import type { ValidatorDirectiveFunc } from '@redwoodjs/graphql-server'
|
||||
import { createValidatorDirective } from '@redwoodjs/graphql-server'
|
||||
|
||||
import { requireAuth as applicationRequireAuth } from 'src/lib/auth'
|
||||
|
||||
export const schema = gql`
|
||||
"""
|
||||
Use to check whether or not a user is authenticated and is associated
|
||||
with an optional set of roles.
|
||||
"""
|
||||
directive @requireAuth(roles: [String]) on FIELD_DEFINITION
|
||||
`
|
||||
|
||||
type RequireAuthValidate = ValidatorDirectiveFunc<{ roles?: string[] }>
|
||||
|
||||
const validate: RequireAuthValidate = ({ directiveArgs }) => {
|
||||
const { roles } = directiveArgs
|
||||
applicationRequireAuth({ roles })
|
||||
}
|
||||
|
||||
const requireAuth = createValidatorDirective(schema, validate)
|
||||
|
||||
export default requireAuth
|
10
api/src/directives/skipAuth/skipAuth.test.ts
Normal file
10
api/src/directives/skipAuth/skipAuth.test.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { getDirectiveName } from '@redwoodjs/testing/api'
|
||||
|
||||
import skipAuth from './skipAuth'
|
||||
|
||||
describe('skipAuth directive', () => {
|
||||
it('declares the directive sdl as schema, with the correct name', () => {
|
||||
expect(skipAuth.schema).toBeTruthy()
|
||||
expect(getDirectiveName(skipAuth.schema)).toBe('skipAuth')
|
||||
})
|
||||
})
|
16
api/src/directives/skipAuth/skipAuth.ts
Normal file
16
api/src/directives/skipAuth/skipAuth.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import gql from 'graphql-tag'
|
||||
|
||||
import { createValidatorDirective } from '@redwoodjs/graphql-server'
|
||||
|
||||
export const schema = gql`
|
||||
"""
|
||||
Use to skip authentication checks and allow public access.
|
||||
"""
|
||||
directive @skipAuth on FIELD_DEFINITION
|
||||
`
|
||||
|
||||
const skipAuth = createValidatorDirective(schema, () => {
|
||||
return
|
||||
})
|
||||
|
||||
export default skipAuth
|
19
api/src/functions/graphql.ts
Normal file
19
api/src/functions/graphql.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { createGraphQLHandler } from '@redwoodjs/graphql-server'
|
||||
|
||||
import directives from 'src/directives/**/*.{js,ts}'
|
||||
import sdls from 'src/graphql/**/*.sdl.{js,ts}'
|
||||
import services from 'src/services/**/*.{js,ts}'
|
||||
|
||||
import { db } from 'src/lib/db'
|
||||
import { logger } from 'src/lib/logger'
|
||||
|
||||
export const handler = createGraphQLHandler({
|
||||
loggerConfig: { logger, options: {} },
|
||||
directives,
|
||||
sdls,
|
||||
services,
|
||||
onException: () => {
|
||||
// Disconnect from your database with an unhandled exception.
|
||||
db.$disconnect()
|
||||
},
|
||||
})
|
0
api/src/graphql/.keep
Normal file
0
api/src/graphql/.keep
Normal file
32
api/src/lib/auth.ts
Normal file
32
api/src/lib/auth.ts
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Once you are ready to add authentication to your application
|
||||
* you'll build out requireAuth() with real functionality. For
|
||||
* now we just return `true` so that the calls in services
|
||||
* have something to check against, simulating a logged
|
||||
* in user that is allowed to access that service.
|
||||
*
|
||||
* See https://redwoodjs.com/docs/authentication for more info.
|
||||
*/
|
||||
export const isAuthenticated = () => {
|
||||
return true
|
||||
}
|
||||
|
||||
export const hasRole = ({ roles }) => {
|
||||
return roles !== undefined
|
||||
}
|
||||
|
||||
// This is used by the redwood directive
|
||||
// in ./api/src/directives/requireAuth
|
||||
|
||||
// Roles are passed in by the requireAuth directive if you have auth setup
|
||||
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
|
||||
export const requireAuth = ({ roles }) => {
|
||||
return isAuthenticated()
|
||||
}
|
||||
|
||||
export const getCurrentUser = async () => {
|
||||
throw new Error(
|
||||
'Auth is not set up yet. See https://redwoodjs.com/docs/authentication ' +
|
||||
'to get started'
|
||||
)
|
||||
}
|
21
api/src/lib/db.ts
Normal file
21
api/src/lib/db.ts
Normal file
@ -0,0 +1,21 @@
|
||||
// See https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/constructor
|
||||
// for options.
|
||||
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
import { emitLogLevels, handlePrismaLogging } from '@redwoodjs/api/logger'
|
||||
|
||||
import { logger } from './logger'
|
||||
|
||||
/*
|
||||
* Instance of the Prisma Client
|
||||
*/
|
||||
export const db = new PrismaClient({
|
||||
log: emitLogLevels(['info', 'warn', 'error']),
|
||||
})
|
||||
|
||||
handlePrismaLogging({
|
||||
db,
|
||||
logger,
|
||||
logLevels: ['info', 'warn', 'error'],
|
||||
})
|
17
api/src/lib/logger.ts
Normal file
17
api/src/lib/logger.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { createLogger } from '@redwoodjs/api/logger'
|
||||
|
||||
/**
|
||||
* Creates a logger with RedwoodLoggerOptions
|
||||
*
|
||||
* These extend and override default LoggerOptions,
|
||||
* can define a destination like a file or other supported pino log transport stream,
|
||||
* and sets whether or not to show the logger configuration settings (defaults to false)
|
||||
*
|
||||
* @param RedwoodLoggerOptions
|
||||
*
|
||||
* RedwoodLoggerOptions have
|
||||
* @param {options} LoggerOptions - defines how to log, such as redaction and format
|
||||
* @param {string | DestinationStream} destination - defines where to log, such as a transport stream or file
|
||||
* @param {boolean} showConfig - whether to display logger configuration on initialization
|
||||
*/
|
||||
export const logger = createLogger({})
|
0
api/src/services/.keep
Normal file
0
api/src/services/.keep
Normal file
35
api/tsconfig.json
Normal file
35
api/tsconfig.json
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"noEmit": true,
|
||||
"allowJs": true,
|
||||
"esModuleInterop": true,
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"skipLibCheck": false,
|
||||
"rootDirs": [
|
||||
"./src",
|
||||
"../.redwood/types/mirror/api/src"
|
||||
],
|
||||
"paths": {
|
||||
"src/*": [
|
||||
"./src/*",
|
||||
"../.redwood/types/mirror/api/src/*"
|
||||
],
|
||||
"types/*": ["./types/*", "../types/*"],
|
||||
"@redwoodjs/testing": ["../node_modules/@redwoodjs/testing/api"]
|
||||
},
|
||||
"typeRoots": [
|
||||
"../node_modules/@types",
|
||||
"./node_modules/@types"
|
||||
],
|
||||
"types": ["jest"],
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": [
|
||||
"src",
|
||||
"../.redwood/types/includes/all-*",
|
||||
"../.redwood/types/includes/api-*",
|
||||
"../types"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user