From ff5496bd42783f2ba93d4756952896f820efdca2 Mon Sep 17 00:00:00 2001 From: Rob Cameron Date: Thu, 1 Dec 2022 15:41:47 -0800 Subject: [PATCH] Adds service caching demo app --- 2022-12-01-service-caching/README.md | 121 + 2022-12-01-service-caching/api/db/dev.db | Bin 0 -> 20480 bytes .../20221201173837_create_post/migration.sql | 8 + .../api/db/migrations/migration_lock.toml | 3 + .../api/db/schema.prisma | 17 + .../directives/requireAuth/requireAuth.js | 92 + .../directives/requireAuth/requireAuth.js.map | 7 + .../api/dist/directives/skipAuth/skipAuth.js | 66 + .../dist/directives/skipAuth/skipAuth.js.map | 7 + .../api/dist/functions/graphql.js | 59 + .../api/dist/functions/graphql.js.map | 7 + .../api/dist/graphql/posts.sdl.js | 64 + .../api/dist/graphql/posts.sdl.js.map | 7 + .../api/dist/lib/auth.js | 44 + .../api/dist/lib/auth.js.map | 7 + .../api/dist/lib/cache.js | 55 + .../api/dist/lib/cache.js.map | 7 + 2022-12-01-service-caching/api/dist/lib/db.js | 38 + .../api/dist/lib/db.js.map | 7 + .../api/dist/lib/logger.js | 29 + .../api/dist/lib/logger.js.map | 7 + .../api/dist/services/posts/posts.js | 90 + .../api/dist/services/posts/posts.js.map | 7 + 2022-12-01-service-caching/api/jest.config.js | 8 + 2022-12-01-service-caching/api/jsconfig.json | 34 + 2022-12-01-service-caching/api/package.json | 10 + .../api/server.config.js | 52 + .../src/directives/requireAuth/requireAuth.js | 22 + .../requireAuth/requireAuth.test.js | 18 + .../api/src/directives/skipAuth/skipAuth.js | 16 + .../src/directives/skipAuth/skipAuth.test.js | 10 + .../api/src/functions/graphql.js | 19 + .../api/src/graphql/.keep | 0 .../api/src/graphql/posts.sdl.js | 30 + .../api/src/lib/auth.js | 25 + .../api/src/lib/cache.ts | 30 + 2022-12-01-service-caching/api/src/lib/db.js | 21 + .../api/src/lib/logger.js | 17 + .../api/src/services/.keep | 0 .../api/src/services/posts/posts.js | 38 + .../api/src/services/posts/posts.scenarios.js | 18 + .../api/src/services/posts/posts.test.js | 52 + .../api/types/graphql.d.ts | 314 + 2022-12-01-service-caching/graphql.config.js | 5 + 2022-12-01-service-caching/jest.config.js | 8 + 2022-12-01-service-caching/package.json | 25 + 2022-12-01-service-caching/prettier.config.js | 18 + 2022-12-01-service-caching/redwood.toml | 16 + 2022-12-01-service-caching/scripts/.keep | 0 .../scripts/jsconfig.json | 42 + 2022-12-01-service-caching/scripts/seed.js | 62 + 2022-12-01-service-caching/web/.DS_Store | Bin 0 -> 6148 bytes 2022-12-01-service-caching/web/jest.config.js | 8 + 2022-12-01-service-caching/web/jsconfig.json | 38 + 2022-12-01-service-caching/web/package.json | 24 + .../web/public/README.md | 36 + .../web/public/favicon.png | Bin 0 -> 1741 bytes .../web/public/robots.txt | 2 + 2022-12-01-service-caching/web/src/App.js | 20 + 2022-12-01-service-caching/web/src/Routes.js | 30 + .../web/src/components/.keep | 0 .../src/components/ArticleCell/ArticleCell.js | 27 + .../ArticleCell/ArticleCell.mock.js | 6 + .../ArticleCell/ArticleCell.stories.js | 20 + .../ArticleCell/ArticleCell.test.js | 41 + .../components/ArticlesCell/ArticlesCell.js | 38 + .../ArticlesCell/ArticlesCell.mock.js | 4 + .../ArticlesCell/ArticlesCell.stories.js | 20 + .../ArticlesCell/ArticlesCell.test.js | 41 + .../Post/EditPostCell/EditPostCell.js | 64 + .../src/components/Post/NewPost/NewPost.js | 42 + .../web/src/components/Post/Post/Post.js | 84 + .../src/components/Post/PostCell/PostCell.js | 25 + .../src/components/Post/PostForm/PostForm.js | 71 + .../web/src/components/Post/Posts/Posts.js | 92 + .../components/Post/PostsCell/PostsCell.js | 36 + 2022-12-01-service-caching/web/src/index.css | 0 2022-12-01-service-caching/web/src/index.html | 17 + .../web/src/layouts/.keep | 0 .../layouts/ScaffoldLayout/ScaffoldLayout.js | 29 + .../web/src/lib/formatters.js | 58 + .../web/src/lib/formatters.test.js | 192 + .../web/src/pages/ArticlePage/ArticlePage.js | 19 + .../pages/ArticlePage/ArticlePage.stories.js | 10 + .../src/pages/ArticlePage/ArticlePage.test.js | 14 + .../pages/FatalErrorPage/FatalErrorPage.js | 62 + .../web/src/pages/HomePage/HomePage.js | 16 + .../src/pages/HomePage/HomePage.stories.js | 10 + .../web/src/pages/HomePage/HomePage.test.js | 14 + .../src/pages/NotFoundPage/NotFoundPage.js | 45 + .../pages/Post/EditPostPage/EditPostPage.js | 7 + .../src/pages/Post/NewPostPage/NewPostPage.js | 7 + .../web/src/pages/Post/PostPage/PostPage.js | 7 + .../web/src/pages/Post/PostsPage/PostsPage.js | 7 + .../web/src/scaffold.css | 397 + .../web/types/graphql.d.ts | 145 + 2022-12-01-service-caching/yarn.lock | 24261 ++++++++++++++++ 97 files changed, 27745 insertions(+) create mode 100644 2022-12-01-service-caching/README.md create mode 100644 2022-12-01-service-caching/api/db/dev.db create mode 100644 2022-12-01-service-caching/api/db/migrations/20221201173837_create_post/migration.sql create mode 100644 2022-12-01-service-caching/api/db/migrations/migration_lock.toml create mode 100644 2022-12-01-service-caching/api/db/schema.prisma create mode 100644 2022-12-01-service-caching/api/dist/directives/requireAuth/requireAuth.js create mode 100644 2022-12-01-service-caching/api/dist/directives/requireAuth/requireAuth.js.map create mode 100644 2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js create mode 100644 2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js.map create mode 100644 2022-12-01-service-caching/api/dist/functions/graphql.js create mode 100644 2022-12-01-service-caching/api/dist/functions/graphql.js.map create mode 100644 2022-12-01-service-caching/api/dist/graphql/posts.sdl.js create mode 100644 2022-12-01-service-caching/api/dist/graphql/posts.sdl.js.map create mode 100644 2022-12-01-service-caching/api/dist/lib/auth.js create mode 100644 2022-12-01-service-caching/api/dist/lib/auth.js.map create mode 100644 2022-12-01-service-caching/api/dist/lib/cache.js create mode 100644 2022-12-01-service-caching/api/dist/lib/cache.js.map create mode 100644 2022-12-01-service-caching/api/dist/lib/db.js create mode 100644 2022-12-01-service-caching/api/dist/lib/db.js.map create mode 100644 2022-12-01-service-caching/api/dist/lib/logger.js create mode 100644 2022-12-01-service-caching/api/dist/lib/logger.js.map create mode 100644 2022-12-01-service-caching/api/dist/services/posts/posts.js create mode 100644 2022-12-01-service-caching/api/dist/services/posts/posts.js.map create mode 100644 2022-12-01-service-caching/api/jest.config.js create mode 100644 2022-12-01-service-caching/api/jsconfig.json create mode 100644 2022-12-01-service-caching/api/package.json create mode 100644 2022-12-01-service-caching/api/server.config.js create mode 100644 2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.js create mode 100644 2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.test.js create mode 100644 2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.js create mode 100644 2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.test.js create mode 100644 2022-12-01-service-caching/api/src/functions/graphql.js create mode 100644 2022-12-01-service-caching/api/src/graphql/.keep create mode 100644 2022-12-01-service-caching/api/src/graphql/posts.sdl.js create mode 100644 2022-12-01-service-caching/api/src/lib/auth.js create mode 100644 2022-12-01-service-caching/api/src/lib/cache.ts create mode 100644 2022-12-01-service-caching/api/src/lib/db.js create mode 100644 2022-12-01-service-caching/api/src/lib/logger.js create mode 100644 2022-12-01-service-caching/api/src/services/.keep create mode 100644 2022-12-01-service-caching/api/src/services/posts/posts.js create mode 100644 2022-12-01-service-caching/api/src/services/posts/posts.scenarios.js create mode 100644 2022-12-01-service-caching/api/src/services/posts/posts.test.js create mode 100644 2022-12-01-service-caching/api/types/graphql.d.ts create mode 100644 2022-12-01-service-caching/graphql.config.js create mode 100644 2022-12-01-service-caching/jest.config.js create mode 100644 2022-12-01-service-caching/package.json create mode 100644 2022-12-01-service-caching/prettier.config.js create mode 100644 2022-12-01-service-caching/redwood.toml create mode 100644 2022-12-01-service-caching/scripts/.keep create mode 100644 2022-12-01-service-caching/scripts/jsconfig.json create mode 100644 2022-12-01-service-caching/scripts/seed.js create mode 100644 2022-12-01-service-caching/web/.DS_Store create mode 100644 2022-12-01-service-caching/web/jest.config.js create mode 100644 2022-12-01-service-caching/web/jsconfig.json create mode 100644 2022-12-01-service-caching/web/package.json create mode 100644 2022-12-01-service-caching/web/public/README.md create mode 100644 2022-12-01-service-caching/web/public/favicon.png create mode 100644 2022-12-01-service-caching/web/public/robots.txt create mode 100644 2022-12-01-service-caching/web/src/App.js create mode 100644 2022-12-01-service-caching/web/src/Routes.js create mode 100644 2022-12-01-service-caching/web/src/components/.keep create mode 100644 2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.js create mode 100644 2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.mock.js create mode 100644 2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.stories.js create mode 100644 2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.test.js create mode 100644 2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.js create mode 100644 2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.mock.js create mode 100644 2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.stories.js create mode 100644 2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.test.js create mode 100644 2022-12-01-service-caching/web/src/components/Post/EditPostCell/EditPostCell.js create mode 100644 2022-12-01-service-caching/web/src/components/Post/NewPost/NewPost.js create mode 100644 2022-12-01-service-caching/web/src/components/Post/Post/Post.js create mode 100644 2022-12-01-service-caching/web/src/components/Post/PostCell/PostCell.js create mode 100644 2022-12-01-service-caching/web/src/components/Post/PostForm/PostForm.js create mode 100644 2022-12-01-service-caching/web/src/components/Post/Posts/Posts.js create mode 100644 2022-12-01-service-caching/web/src/components/Post/PostsCell/PostsCell.js create mode 100644 2022-12-01-service-caching/web/src/index.css create mode 100644 2022-12-01-service-caching/web/src/index.html create mode 100644 2022-12-01-service-caching/web/src/layouts/.keep create mode 100644 2022-12-01-service-caching/web/src/layouts/ScaffoldLayout/ScaffoldLayout.js create mode 100644 2022-12-01-service-caching/web/src/lib/formatters.js create mode 100644 2022-12-01-service-caching/web/src/lib/formatters.test.js create mode 100644 2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.js create mode 100644 2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.stories.js create mode 100644 2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.test.js create mode 100644 2022-12-01-service-caching/web/src/pages/FatalErrorPage/FatalErrorPage.js create mode 100644 2022-12-01-service-caching/web/src/pages/HomePage/HomePage.js create mode 100644 2022-12-01-service-caching/web/src/pages/HomePage/HomePage.stories.js create mode 100644 2022-12-01-service-caching/web/src/pages/HomePage/HomePage.test.js create mode 100644 2022-12-01-service-caching/web/src/pages/NotFoundPage/NotFoundPage.js create mode 100644 2022-12-01-service-caching/web/src/pages/Post/EditPostPage/EditPostPage.js create mode 100644 2022-12-01-service-caching/web/src/pages/Post/NewPostPage/NewPostPage.js create mode 100644 2022-12-01-service-caching/web/src/pages/Post/PostPage/PostPage.js create mode 100644 2022-12-01-service-caching/web/src/pages/Post/PostsPage/PostsPage.js create mode 100644 2022-12-01-service-caching/web/src/scaffold.css create mode 100644 2022-12-01-service-caching/web/types/graphql.d.ts create mode 100644 2022-12-01-service-caching/yarn.lock diff --git a/2022-12-01-service-caching/README.md b/2022-12-01-service-caching/README.md new file mode 100644 index 00000000..f471d9ee --- /dev/null +++ b/2022-12-01-service-caching/README.md @@ -0,0 +1,121 @@ +# README + +Welcome to [RedwoodJS](https://redwoodjs.com)! + +> **Prerequisites** +> +> - Redwood requires [Node.js](https://nodejs.org/en/) (>=14.19.x <=16.x) and [Yarn](https://yarnpkg.com/) (>=1.15) +> - Are you on Windows? For best results, follow our [Windows development setup](https://redwoodjs.com/docs/how-to/windows-development-setup) guide + +Start by installing dependencies: + +``` +yarn install +``` + +Then change into that directory and start the development server: + +``` +cd my-redwood-project +yarn redwood dev +``` + +Your browser should automatically open to http://localhost:8910 where you'll see the Welcome Page, which links out to a ton of great resources. + +> **The Redwood CLI** +> +> Congratulations on running your first Redwood CLI command! +> From dev to deploy, the CLI is with you the whole way. +> And there's quite a few commands at your disposal: +> ``` +> yarn redwood --help +> ``` +> For all the details, see the [CLI reference](https://redwoodjs.com/docs/cli-commands). + +## Prisma and the database + +Redwood wouldn't be a full-stack framework without a database. It all starts with the schema. Open the [`schema.prisma`](api/db/schema.prisma) file in `api/db` and replace the `UserExample` model with the following `Post` model: + +``` +model Post { + id Int @id @default(autoincrement()) + title String + body String + createdAt DateTime @default(now()) +} +``` + +Redwood uses [Prisma](https://www.prisma.io/), a next-gen Node.js and TypeScript ORM, to talk to the database. Prisma's schema offers a declarative way of defining your app's data models. And Prisma [Migrate](https://www.prisma.io/migrate) uses that schema to make database migrations hassle-free: + +``` +yarn rw prisma migrate dev + +# ... + +? Enter a name for the new migration: › create posts +``` + +> `rw` is short for `redwood` + +You'll be prompted for the name of your migration. `create posts` will do. + +Now let's generate everything we need to perform all the CRUD (Create, Retrieve, Update, Delete) actions on our `Post` model: + +``` +yarn redwood g scaffold post +``` + +Navigate to http://localhost:8910/posts/new, fill in the title and body, and click "Save": + +Did we just create a post in the database? Yup! With `yarn rw g scaffold `, Redwood created all the pages, components, and services necessary to perform all CRUD actions on our posts table. + +## Frontend first with Storybook + +Don't know what your data models look like? +That's more than ok—Redwood integrates Storybook so that you can work on design without worrying about data. +Mockup, build, and verify your React components, even in complete isolation from the backend: + +``` +yarn rw storybook +``` + +Before you start, see if the CLI's `setup ui` command has your favorite styling library: + +``` +yarn rw setup ui --help +``` + +## Testing with Jest + +It'd be hard to scale from side project to startup without a few tests. +Redwood fully integrates Jest with the front and the backends and makes it easy to keep your whole app covered by generating test files with all your components and services: + +``` +yarn rw test +``` + +To make the integration even more seamless, Redwood augments Jest with database [scenarios](https://redwoodjs.com/docs/testing.md#scenarios) and [GraphQL mocking](https://redwoodjs.com/docs/testing.md#mocking-graphql-calls). + +## Ship it + +Redwood is designed for both serverless deploy targets like Netlify and Vercel and serverful deploy targets like Render and AWS: + +``` +yarn rw setup deploy --help +``` + +Don't go live without auth! +Lock down your front and backends with Redwood's built-in, database-backed authentication system ([dbAuth](https://redwoodjs.com/docs/authentication#self-hosted-auth-installation-and-setup)), or integrate with nearly a dozen third party auth providers: + +``` +yarn rw setup auth --help +``` + +## Next Steps + +The best way to learn Redwood is by going through the comprehensive [tutorial](https://redwoodjs.com/docs/tutorial/foreword) and joining the community (via the [Discourse forum](https://community.redwoodjs.com) or the [Discord server](https://discord.gg/redwoodjs)). + +## Quick Links + +- Stay updated: read [Forum announcements](https://community.redwoodjs.com/c/announcements/5), follow us on [Twitter](https://twitter.com/redwoodjs), and subscribe to the [newsletter](https://redwoodjs.com/newsletter) +- [Learn how to contribute](https://redwoodjs.com/docs/contributing) diff --git a/2022-12-01-service-caching/api/db/dev.db b/2022-12-01-service-caching/api/db/dev.db new file mode 100644 index 0000000000000000000000000000000000000000..88095f51c2047e18f1afb5c866442ee8c003c2f3 GIT binary patch literal 20480 zcmeI4O>Y~=8OKS>j1tv~ilBypo}Q{G99JYEQq~&^6p<}DRjRj26lxbJh}qp)?wGqf z^vtZNHHrd`oA}ZXkaI3Q7D2y4Q{>d%+MfCWl0$DskNwZ`MU82^oIbSt1m0}k6K&p7PYtT?Y8KZwr{5_;Z^j!^DLX0y>;_u`T1U9^H6GI zAJ;GRUbL4M>Fqhfu_%H?+SzZn-fbPx{z04ePj+|d@Mvdm>*x{vy!D8-PTB`M`>?Rr z+HY5fos2LsU!->HAv%mYjc$1|$gjTRE6xlLwlMZKd};6OwI-cuyY*n}WVcQCPmYc- zPRHIoZg1@!j?8BL;7dEt-Fg12^6affqx@l~@VnDjLMN<~h^}Hr$W&jxa_u6M%MC6N zvZ!7;3g@Rmy#m6IxZl%RvUsuIRm?(>3LWvF!@%bJ7*iS{n0V8%@t!oi%3v(JdeY$B zplv!breqvrK#%#o=}})9QX8favgsDq+PKx_@cEfiJT)C75{}MEe>5EH_hSJ=+VH;a z_%cfiPshSL*+1TScfYlL+2Hcro%`2byYC79joQ%MYgm=3l?P}^r5|i+xG*O+l zIq%acd2z;Z$zT^GX+Ofw&8Jm56=@!m=5Z*vrc>6JYQPl7Fv!o22*kZ7%GbNRNWl&L|)Vv4iG z<4W_h%z{YZyO*W#B_e@-Is9Znp0I#}XRmk!Lc`yfEBMWP3CkHa?updyNhKMEEUr__ zU}#wH2?K8@E}Fd02MW7Z73RhgPl27sH7vt?%tTPfXYQZ=;kP({{{4SHPb#yer58V} zR33;BG4rrVv54wDAp;QOm_0*)B%-51KIS!v=meYp2+qYE!*pr;cngZ7C=)X%4iSpsah;C2Fd!EwkwQvc z8(7|j))F4zhTTs}U2I0}SuKDTX_F;=BL{#z^d2S#m}JOOAn!wam}?x=`ig7K15CvD z5x#-ggH(qq2E9b0S(dOwpr4NHAnc6Oz{2b*&Qp4v2_L#-DrCdRDjHpJAd(u(VF^;p|y2`dRl}Z4)+_f0c3t3Yn_OQ-spUOk-;(O$h@xVHaZygeQk|w4txw+karsGU z>4P6tD(y%p=wO^w56(4k9(BbA6!26e(?yNAA4#YbqaX)QSr-*cq2A(@S4Z_V5og9m zLnD#M3^-1n&EWGYn4>23a;6sp_npWKA%1*C&7uKAK%rHn%BYG1tI#$vM@z|+aA|wz z5x$w6DGIPV8wJNmrlFTZ=nwf}XPVfPRvpToe@@wk9Q4obnnE5cJ7{g%t;BJq~%qS&ZcEV)=s_Y*`zxPj*GF*<}U! zGpQ%zwKo))_&s4ugFe5DY_R&mPIiW}b1nmV?_lp>RN4xl4-Xz5K!T_faDU< { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var requireAuth_exports = {}; +__export(requireAuth_exports, { + default: () => requireAuth_default, + schema: () => schema +}); +module.exports = __toCommonJS(requireAuth_exports); +var import_graphql_server = require("@redwoodjs/graphql-server"); +var import_auth = require("../../lib/auth"); +const schema = { + "kind": "Document", + "definitions": [{ + "kind": "DirectiveDefinition", + "description": { + "kind": "StringValue", + "value": "Use to check whether or not a user is authenticated and is associated\nwith an optional set of roles.", + "block": true + }, + "name": { + "kind": "Name", + "value": "requireAuth" + }, + "arguments": [{ + "kind": "InputValueDefinition", + "name": { + "kind": "Name", + "value": "roles" + }, + "type": { + "kind": "ListType", + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + }, + "directives": [] + }], + "repeatable": false, + "locations": [{ + "kind": "Name", + "value": "FIELD_DEFINITION" + }] + }], + "loc": { + "start": 0, + "end": 180, + "source": { + "body": '\n """\n Use to check whether or not a user is authenticated and is associated\n with an optional set of roles.\n """\n directive @requireAuth(roles: [String]) on FIELD_DEFINITION\n', + "name": "GraphQL request", + "locationOffset": { + "line": 1, + "column": 1 + } + } + } +}; +const validate = ({ + directiveArgs +}) => { + const { + roles + } = directiveArgs; + (0, import_auth.requireAuth)({ + roles + }); +}; +const requireAuth = (0, import_graphql_server.createValidatorDirective)(schema, validate); +var requireAuth_default = requireAuth; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + schema +}); +//# sourceMappingURL=requireAuth.js.map diff --git a/2022-12-01-service-caching/api/dist/directives/requireAuth/requireAuth.js.map b/2022-12-01-service-caching/api/dist/directives/requireAuth/requireAuth.js.map new file mode 100644 index 00000000..c551500f --- /dev/null +++ b/2022-12-01-service-caching/api/dist/directives/requireAuth/requireAuth.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../../src/directives/requireAuth/requireAuth.js"], + "sourcesContent": ["import gql from 'graphql-tag'\n\nimport { createValidatorDirective } from '@redwoodjs/graphql-server'\n\nimport { requireAuth as applicationRequireAuth } from 'src/lib/auth'\n\nexport const schema = gql`\n \"\"\"\n Use to check whether or not a user is authenticated and is associated\n with an optional set of roles.\n \"\"\"\n directive @requireAuth(roles: [String]) on FIELD_DEFINITION\n`\n\nconst validate = ({ directiveArgs }) => {\n const { roles } = directiveArgs\n applicationRequireAuth({ roles })\n}\n\nconst requireAuth = createValidatorDirective(schema, validate)\n\nexport default requireAuth\n"], + "mappings": ";;;;;;;;;;;;;;;;;AAEA;;;;;;4BAAyC;AAEzC,kBAA8C;AAEvC,MAAMA,SAAM;EAAA,QAAA;EAAA,eAAA,CAAA;IAAA,QAAA;IAAA,eAAA;MAAA,QAAA;MAAA,SAAA;MAAA,SAAA;IAAA;IAAA,QAAA;MAAA,QAAA;MAAA,SAAA;IAAA;IAAA,aAAA,CAAA;MAAA,QAAA;MAAA,QAAA;QAAA,QAAA;QAAA,SAAA;MAAA;MAAA,QAAA;QAAA,QAAA;QAAA,QAAA;UAAA,QAAA;UAAA,QAAA;YAAA,QAAA;YAAA,SAAA;UAAA;QAAA;MAAA;MAAA,cAAA,CAAA;IAAA,CAAA;IAAA,cAAA;IAAA,aAAA,CAAA;MAAA,QAAA;MAAA,SAAA;IAAA,CAAA;EAAA,CAAA;EAAA,OAAA;IAAA,SAAA;IAAA,OAAA;IAAA,UAAA;MAAA,QAAA;MAAA,QAAA;MAAA,kBAAA;QAAA,QAAA;QAAA,UAAA;MAAA;IAAA;EAAA;AAAA;AAQnB,MAAMC,WAAW,CAAC;EAAEC;AAAc,MAAM;AACtC,QAAM;IAAEC;EAAM,IAAID;AAClBE,kBAAAA,aAAuB;IAAED;EAAM,CAAC;AAClC;AAEA,MAAME,kBAAcC,gDAAyBN,QAAQC,QAAQ;AAE7D,IAAA,sBAAeI;", + "names": ["schema", "validate", "directiveArgs", "roles", "applicationRequireAuth", "requireAuth", "createValidatorDirective"] +} diff --git a/2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js b/2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js new file mode 100644 index 00000000..c30bef6a --- /dev/null +++ b/2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js @@ -0,0 +1,66 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var skipAuth_exports = {}; +__export(skipAuth_exports, { + default: () => skipAuth_default, + schema: () => schema +}); +module.exports = __toCommonJS(skipAuth_exports); +var import_graphql_server = require("@redwoodjs/graphql-server"); +const schema = { + "kind": "Document", + "definitions": [{ + "kind": "DirectiveDefinition", + "description": { + "kind": "StringValue", + "value": "Use to skip authentication checks and allow public access.", + "block": true + }, + "name": { + "kind": "Name", + "value": "skipAuth" + }, + "arguments": [], + "repeatable": false, + "locations": [{ + "kind": "Name", + "value": "FIELD_DEFINITION" + }] + }], + "loc": { + "start": 0, + "end": 116, + "source": { + "body": '\n """\n Use to skip authentication checks and allow public access.\n """\n directive @skipAuth on FIELD_DEFINITION\n', + "name": "GraphQL request", + "locationOffset": { + "line": 1, + "column": 1 + } + } + } +}; +const skipAuth = (0, import_graphql_server.createValidatorDirective)(schema, () => { + return; +}); +var skipAuth_default = skipAuth; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + schema +}); +//# sourceMappingURL=skipAuth.js.map diff --git a/2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js.map b/2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js.map new file mode 100644 index 00000000..757b1a66 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/directives/skipAuth/skipAuth.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../../src/directives/skipAuth/skipAuth.js"], + "sourcesContent": ["import gql from 'graphql-tag'\n\nimport { createValidatorDirective } from '@redwoodjs/graphql-server'\n\nexport const schema = gql`\n \"\"\"\n Use to skip authentication checks and allow public access.\n \"\"\"\n directive @skipAuth on FIELD_DEFINITION\n`\n\nconst skipAuth = createValidatorDirective(schema, () => {\n return\n})\n\nexport default skipAuth\n"], + "mappings": ";;;;;;;;;;;;;;;;;AAEA;;;;;;4BAAyC;AAElC,MAAMA,SAAM;EAAA,QAAA;EAAA,eAAA,CAAA;IAAA,QAAA;IAAA,eAAA;MAAA,QAAA;MAAA,SAAA;MAAA,SAAA;IAAA;IAAA,QAAA;MAAA,QAAA;MAAA,SAAA;IAAA;IAAA,aAAA,CAAA;IAAA,cAAA;IAAA,aAAA,CAAA;MAAA,QAAA;MAAA,SAAA;IAAA,CAAA;EAAA,CAAA;EAAA,OAAA;IAAA,SAAA;IAAA,OAAA;IAAA,UAAA;MAAA,QAAA;MAAA,QAAA;MAAA,kBAAA;QAAA,QAAA;QAAA,UAAA;MAAA;IAAA;EAAA;AAAA;AAOnB,MAAMC,eAAWC,gDAAyBF,QAAQ,MAAM;AACtD;AACF,CAAC;AAED,IAAA,mBAAeC;", + "names": ["schema", "skipAuth", "createValidatorDirective"] +} diff --git a/2022-12-01-service-caching/api/dist/functions/graphql.js b/2022-12-01-service-caching/api/dist/functions/graphql.js new file mode 100644 index 00000000..747e4e11 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/functions/graphql.js @@ -0,0 +1,59 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var graphql_exports = {}; +__export(graphql_exports, { + handler: () => handler +}); +module.exports = __toCommonJS(graphql_exports); +var import_graphql_server = require("@redwoodjs/graphql-server"); +var directives_requireAuth_requireAuth = __toESM(require("../directives/requireAuth/requireAuth")); +var directives_skipAuth_skipAuth = __toESM(require("../directives/skipAuth/skipAuth")); +var sdls_posts_sdl = __toESM(require("../graphql/posts.sdl")); +var services_posts_posts = __toESM(require("../services/posts/posts")); +var import_db = require("../lib/db"); +var import_logger = require("../lib/logger"); +let directives = {}; +directives.requireAuth_requireAuth = directives_requireAuth_requireAuth; +directives.skipAuth_skipAuth = directives_skipAuth_skipAuth; +let sdls = {}; +sdls.posts_sdl = sdls_posts_sdl; +let services = {}; +services.posts_posts = services_posts_posts; +const handler = (0, import_graphql_server.createGraphQLHandler)({ + loggerConfig: { + logger: import_logger.logger, + options: {} + }, + directives, + sdls, + services, + onException: () => { + import_db.db.$disconnect(); + } +}); +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + handler +}); +//# sourceMappingURL=graphql.js.map diff --git a/2022-12-01-service-caching/api/dist/functions/graphql.js.map b/2022-12-01-service-caching/api/dist/functions/graphql.js.map new file mode 100644 index 00000000..f458f523 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/functions/graphql.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../src/functions/graphql.js"], + "sourcesContent": ["import { createGraphQLHandler } from '@redwoodjs/graphql-server'\n\nimport directives from 'src/directives/**/*.{js,ts}'\nimport sdls from 'src/graphql/**/*.sdl.{js,ts}'\nimport services from 'src/services/**/*.{js,ts}'\n\nimport { db } from 'src/lib/db'\nimport { logger } from 'src/lib/logger'\n\nexport const handler = createGraphQLHandler({\n loggerConfig: { logger, options: {} },\n directives,\n sdls,\n services,\n onException: () => {\n // Disconnect from your database with an unhandled exception.\n db.$disconnect()\n },\n})\n"], + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;4BAAqC;AAA2B,yCAAA;AAAA,mCAAA;AAAA,qBAAA;AAAA,2BAAA;AAMhE,gBAAW;AACX,oBAAe;AAPiD,IAAA,aAAA,CAAA;AAAA,WAAA,0BAAA;AAAA,WAAA,oBAAA;AAAA,IAAA,OAAA,CAAA;AAAA,KAAA,YAAA;AAAA,IAAA,WAAA,CAAA;AAAA,SAAA,cAAA;AASzD,MAAMA,cAAUC,4CAAqB;EAC1CC,cAAc;IAAEC;IAAQC,SAAS,CAAC;EAAE;EACpCC;EACAC;EACAC;EACAC,aAAa,MAAM;AAEjBC,iBAAGC,YAAW;EAChB;AACF,CAAC;", + "names": ["handler", "createGraphQLHandler", "loggerConfig", "logger", "options", "directives", "sdls", "services", "onException", "db", "$disconnect"] +} diff --git a/2022-12-01-service-caching/api/dist/graphql/posts.sdl.js b/2022-12-01-service-caching/api/dist/graphql/posts.sdl.js new file mode 100644 index 00000000..be96c54a --- /dev/null +++ b/2022-12-01-service-caching/api/dist/graphql/posts.sdl.js @@ -0,0 +1,64 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var posts_sdl_exports = {}; +__export(posts_sdl_exports, { + schema: () => schema +}); +module.exports = __toCommonJS(posts_sdl_exports); +var import_graphql_tag = __toESM(require("graphql-tag")); +const schema = import_graphql_tag.default` + type Post { + id: Int! + title: String! + body: String! + createdAt: DateTime! + updatedAt: DateTime! + } + + type Query { + posts: [Post!]! @requireAuth + post(id: Int!): Post @requireAuth + } + + input CreatePostInput { + title: String! + body: String! + } + + input UpdatePostInput { + title: String + body: String + } + + type Mutation { + createPost(input: CreatePostInput!): Post! @requireAuth + updatePost(id: Int!, input: UpdatePostInput!): Post! @requireAuth + deletePost(id: Int!): Post! @requireAuth + } +`; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + schema +}); +//# sourceMappingURL=posts.sdl.js.map diff --git a/2022-12-01-service-caching/api/dist/graphql/posts.sdl.js.map b/2022-12-01-service-caching/api/dist/graphql/posts.sdl.js.map new file mode 100644 index 00000000..f5fabed0 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/graphql/posts.sdl.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../src/graphql/posts.sdl.js"], + "sourcesContent": ["export const schema = gql`\n type Post {\n id: Int!\n title: String!\n body: String!\n createdAt: DateTime!\n updatedAt: DateTime!\n }\n\n type Query {\n posts: [Post!]! @requireAuth\n post(id: Int!): Post @requireAuth\n }\n\n input CreatePostInput {\n title: String!\n body: String!\n }\n\n input UpdatePostInput {\n title: String\n body: String\n }\n\n type Mutation {\n createPost(input: CreatePostInput!): Post! @requireAuth\n updatePost(id: Int!, input: UpdatePostInput!): Post! @requireAuth\n deletePost(id: Int!): Post! @requireAuth\n }\n`\n"], + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;yBAAyB;AAAlB,MAAMA,SAASC,mBAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;", + "names": ["schema", "gql"] +} diff --git a/2022-12-01-service-caching/api/dist/lib/auth.js b/2022-12-01-service-caching/api/dist/lib/auth.js new file mode 100644 index 00000000..7b6dfbd1 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/auth.js @@ -0,0 +1,44 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var auth_exports = {}; +__export(auth_exports, { + hasRole: () => hasRole, + isAuthenticated: () => isAuthenticated, + requireAuth: () => requireAuth +}); +module.exports = __toCommonJS(auth_exports); +const isAuthenticated = () => { + return true; +}; +const hasRole = ({ + roles +}) => { + return roles !== void 0; +}; +const requireAuth = ({ + roles +}) => { + return isAuthenticated(); +}; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + hasRole, + isAuthenticated, + requireAuth +}); +//# sourceMappingURL=auth.js.map diff --git a/2022-12-01-service-caching/api/dist/lib/auth.js.map b/2022-12-01-service-caching/api/dist/lib/auth.js.map new file mode 100644 index 00000000..211016bb --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/auth.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../src/lib/auth.js"], + "sourcesContent": ["/**\n * Once you are ready to add authentication to your application\n * you'll build out requireAuth() with real functionality. For\n * now we just return `true` so that the calls in services\n * have something to check against, simulating a logged\n * in user that is allowed to access that service.\n *\n * See https://redwoodjs.com/docs/authentication for more info.\n */\nexport const isAuthenticated = () => {\n return true\n}\n\nexport const hasRole = ({ roles }) => {\n return roles !== undefined\n}\n\n// This is used by the redwood directive\n// in ./api/src/directives/requireAuth\n\n// Roles are passed in by the requireAuth directive if you have auth setup\n// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars\nexport const requireAuth = ({ roles }) => {\n return isAuthenticated()\n}\n"], + "mappings": ";;;;;;;;;;;;;;;;;AAAA;;;;;;;AASO,MAAMA,kBAAkB,MAAM;AACnC,SAAO;AACT;AAEO,MAAMC,UAAU,CAAC;EAAEC;AAAM,MAAM;AACpC,SAAOA,UAAUC;AACnB;AAOO,MAAMC,cAAc,CAAC;EAAEF;AAAM,MAAM;AACxC,SAAOF,gBAAe;AACxB;", + "names": ["isAuthenticated", "hasRole", "roles", "undefined", "requireAuth"] +} diff --git a/2022-12-01-service-caching/api/dist/lib/cache.js b/2022-12-01-service-caching/api/dist/lib/cache.js new file mode 100644 index 00000000..fb6198f2 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/cache.js @@ -0,0 +1,55 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var cache_exports = {}; +__export(cache_exports, { + cache: () => cache, + cacheFindMany: () => cacheFindMany, + client: () => client +}); +module.exports = __toCommonJS(cache_exports); +var import_cache = require("@redwoodjs/api/cache"); +var import_logger = require("./logger"); +const memJsFormattedLogger = { + log: (msg) => import_logger.logger.error(msg) +}; +let client; +if (process.env.NODE_ENV === "test") { + client = new import_cache.InMemoryClient(); +} else { + try { + client = new import_cache.MemcachedClient(process.env.CACHE_HOST, { + logger: memJsFormattedLogger + }); + } catch (e) { + import_logger.logger.error(`Could not connect to cache: ${e.message}`); + } +} +const { + cache, + cacheFindMany +} = (0, import_cache.createCache)(client, { + logger: import_logger.logger, + timeout: 500 +}); +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + cache, + cacheFindMany, + client +}); +//# sourceMappingURL=cache.js.map diff --git a/2022-12-01-service-caching/api/dist/lib/cache.js.map b/2022-12-01-service-caching/api/dist/lib/cache.js.map new file mode 100644 index 00000000..929eca65 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/cache.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../src/lib/cache.ts"], + "sourcesContent": ["import {\n createCache,\n InMemoryClient,\n MemcachedClient,\n} from '@redwoodjs/api/cache'\n\nimport { logger } from './logger'\n\nconst memJsFormattedLogger = {\n log: (msg: string) => logger.error(msg),\n}\n\nexport let client: InMemoryClient | MemcachedClient\n\nif (process.env.NODE_ENV === 'test') {\n client = new InMemoryClient()\n} else {\n try {\n client = new MemcachedClient(process.env.CACHE_HOST, {\n logger: memJsFormattedLogger,\n })\n } catch (e) {\n logger.error(`Could not connect to cache: ${e.message}`)\n }\n}\n\nexport const { cache, cacheFindMany } = createCache(client, {\n logger,\n timeout: 500,\n})\n"], + "mappings": ";;;;;;;;;;;;;;;;;AAAA;;;;;;;mBAIO;AAEP,oBAAuB;AAEvB,MAAMA,uBAAuB;EAC3BC,KAAMC,SAAgBC,qBAAOC,MAAMF,GAAG;AACxC;AAEO,IAAIG;AAEX,IAAIC,QAAQC,IAAIC,aAAa,QAAQ;AACnCH,WAAS,IAAII,4BAAc;AAC7B,OAAO;AACL,MAAI;AACFJ,aAAS,IAAIK,6BAAgBJ,QAAQC,IAAII,YAAY;MACnDR,QAAQH;IACV,CAAC;EACH,SAASY,GAAP;AACAT,yBAAOC,MAAO,+BAA8BQ,EAAEC,SAAS;EACzD;AACF;AAEO,MAAM;EAAEC;EAAOC;AAAc,QAAIC,0BAAYX,QAAQ;EAC1DF;EACAc,SAAS;AACX,CAAC;", + "names": ["memJsFormattedLogger", "log", "msg", "logger", "error", "client", "process", "env", "NODE_ENV", "InMemoryClient", "MemcachedClient", "CACHE_HOST", "e", "message", "cache", "cacheFindMany", "createCache", "timeout"] +} diff --git a/2022-12-01-service-caching/api/dist/lib/db.js b/2022-12-01-service-caching/api/dist/lib/db.js new file mode 100644 index 00000000..c79f0676 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/db.js @@ -0,0 +1,38 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var db_exports = {}; +__export(db_exports, { + db: () => db +}); +module.exports = __toCommonJS(db_exports); +var import_client = require("@prisma/client"); +var import_logger = require("@redwoodjs/api/logger"); +var import_logger2 = require("./logger"); +const db = new import_client.PrismaClient({ + log: (0, import_logger.emitLogLevels)(["info", "warn", "error"]) +}); +(0, import_logger.handlePrismaLogging)({ + db, + logger: import_logger2.logger, + logLevels: ["info", "warn", "error"] +}); +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + db +}); +//# sourceMappingURL=db.js.map diff --git a/2022-12-01-service-caching/api/dist/lib/db.js.map b/2022-12-01-service-caching/api/dist/lib/db.js.map new file mode 100644 index 00000000..26a02389 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/db.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../src/lib/db.js"], + "sourcesContent": ["// See https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/constructor\n// for options.\n\nimport { PrismaClient } from '@prisma/client'\n\nimport { emitLogLevels, handlePrismaLogging } from '@redwoodjs/api/logger'\n\nimport { logger } from './logger'\n\n/*\n * Instance of the Prisma Client\n */\nexport const db = new PrismaClient({\n log: emitLogLevels(['info', 'warn', 'error']),\n})\n\nhandlePrismaLogging({\n db,\n logger,\n logLevels: ['info', 'warn', 'error'],\n})\n"], + "mappings": ";;;;;;;;;;;;;;;;;AAAA;;;;;AAGA,oBAA6B;AAE7B,oBAAmD;AAEnD,IAAAA,iBAAuB;AAKhB,MAAMC,KAAK,IAAIC,2BAAa;EACjCC,SAAKC,6BAAc,CAAC,QAAQ,QAAQ,OAAO,CAAC;AAC9C,CAAC;IAEDC,mCAAoB;EAClBJ;EACAK;EACAC,WAAW,CAAC,QAAQ,QAAQ,OAAO;AACrC,CAAC;", + "names": ["import_logger", "db", "PrismaClient", "log", "emitLogLevels", "handlePrismaLogging", "logger", "logLevels"] +} diff --git a/2022-12-01-service-caching/api/dist/lib/logger.js b/2022-12-01-service-caching/api/dist/lib/logger.js new file mode 100644 index 00000000..0b97a044 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/logger.js @@ -0,0 +1,29 @@ +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var logger_exports = {}; +__export(logger_exports, { + logger: () => logger +}); +module.exports = __toCommonJS(logger_exports); +var import_logger = require("@redwoodjs/api/logger"); +const logger = (0, import_logger.createLogger)({}); +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + logger +}); +//# sourceMappingURL=logger.js.map diff --git a/2022-12-01-service-caching/api/dist/lib/logger.js.map b/2022-12-01-service-caching/api/dist/lib/logger.js.map new file mode 100644 index 00000000..5731bd84 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/lib/logger.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../src/lib/logger.js"], + "sourcesContent": ["import { createLogger } from '@redwoodjs/api/logger'\n\n/**\n * Creates a logger with RedwoodLoggerOptions\n *\n * These extend and override default LoggerOptions,\n * can define a destination like a file or other supported pino log transport stream,\n * and sets whether or not to show the logger configuration settings (defaults to false)\n *\n * @param RedwoodLoggerOptions\n *\n * RedwoodLoggerOptions have\n * @param {options} LoggerOptions - defines how to log, such as redaction and format\n * @param {string | DestinationStream} destination - defines where to log, such as a transport stream or file\n * @param {boolean} showConfig - whether to display logger configuration on initialization\n */\nexport const logger = createLogger({})\n"], + "mappings": ";;;;;;;;;;;;;;;;;AAAA;;;;;oBAA6B;AAgBtB,MAAMA,aAASC,4BAAa,CAAC,CAAC;", + "names": ["logger", "createLogger"] +} diff --git a/2022-12-01-service-caching/api/dist/services/posts/posts.js b/2022-12-01-service-caching/api/dist/services/posts/posts.js new file mode 100644 index 00000000..a672dfdc --- /dev/null +++ b/2022-12-01-service-caching/api/dist/services/posts/posts.js @@ -0,0 +1,90 @@ +var __create = Object.create; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var posts_exports = {}; +__export(posts_exports, { + createPost: () => createPost, + deletePost: () => deletePost, + post: () => post, + posts: () => posts, + updatePost: () => updatePost +}); +module.exports = __toCommonJS(posts_exports); +var import_promise = __toESM(require("@babel/runtime-corejs3/core-js/promise")); +var import_set_timeout = __toESM(require("@babel/runtime-corejs3/core-js/set-timeout")); +var import_cache = require("../../lib/cache"); +var import_db = require("../../lib/db"); +function delay(ms) { + return new import_promise.default((resolve) => (0, import_set_timeout.default)(resolve, ms)); +} +const posts = () => { + return (0, import_cache.cacheFindMany)(`posts`, import_db.db.post); +}; +const post = async ({ + id +}) => { + return (0, import_cache.cache)(["post", id], async () => { + await delay(2e3); + return import_db.db.post.findUnique({ + where: { + id + } + }); + }); +}; +const createPost = ({ + input +}) => { + return import_db.db.post.create({ + data: input + }); +}; +const updatePost = ({ + id, + input +}) => { + return import_db.db.post.update({ + data: input, + where: { + id + } + }); +}; +const deletePost = ({ + id +}) => { + return import_db.db.post.delete({ + where: { + id + } + }); +}; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + createPost, + deletePost, + post, + posts, + updatePost +}); +//# sourceMappingURL=posts.js.map diff --git a/2022-12-01-service-caching/api/dist/services/posts/posts.js.map b/2022-12-01-service-caching/api/dist/services/posts/posts.js.map new file mode 100644 index 00000000..2f562104 --- /dev/null +++ b/2022-12-01-service-caching/api/dist/services/posts/posts.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["../../../src/services/posts/posts.js"], + "sourcesContent": ["import { cache, cacheFindMany } from 'src/lib/cache'\nimport { db } from 'src/lib/db'\n\nfunction delay(ms) {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\nexport const posts = () => {\n return cacheFindMany(`posts`, db.post)\n}\n\nexport const post = async ({ id }) => {\n return cache(['post', id], async () => {\n await delay(2000)\n return db.post.findUnique({\n where: { id },\n })\n })\n}\n\nexport const createPost = ({ input }) => {\n return db.post.create({\n data: input,\n })\n}\n\nexport const updatePost = ({ id, input }) => {\n return db.post.update({\n data: input,\n where: { id },\n })\n}\n\nexport const deletePost = ({ id }) => {\n return db.post.delete({\n where: { id },\n })\n}\n"], + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mBAA6B;AAC7B,gBAAW;AAEX,SAASA,MAAMC,IAAI;AACjB,SAAO,IAAA,eAAAC,QAAaC,iBAAY,mBAAAC,SAAWD,SAASF,EAAE,CAAC;AACzD;AAEO,MAAMI,QAAQ,MAAM;AACzB,aAAOC,4BAAe,SAAQC,aAAGC,IAAI;AACvC;AAEO,MAAMA,OAAO,OAAO;EAAEC;AAAG,MAAM;AACpC,aAAOC,oBAAM,CAAC,QAAQD,EAAE,GAAG,YAAY;AACrC,UAAMT,MAAM,GAAI;AAChB,WAAOO,aAAGC,KAAKG,WAAW;MACxBC,OAAO;QAAEH;MAAG;IACd,CAAC;EACH,CAAC;AACH;AAEO,MAAMI,aAAa,CAAC;EAAEC;AAAM,MAAM;AACvC,SAAOP,aAAGC,KAAKO,OAAO;IACpBC,MAAMF;EACR,CAAC;AACH;AAEO,MAAMG,aAAa,CAAC;EAAER;EAAIK;AAAM,MAAM;AAC3C,SAAOP,aAAGC,KAAKU,OAAO;IACpBF,MAAMF;IACNF,OAAO;MAAEH;IAAG;EACd,CAAC;AACH;AAEO,MAAMU,aAAa,CAAC;EAAEV;AAAG,MAAM;AACpC,SAAOF,aAAGC,KAAKY,OAAO;IACpBR,OAAO;MAAEH;IAAG;EACd,CAAC;AACH;", + "names": ["delay", "ms", "_Promise", "resolve", "_setTimeout", "posts", "cacheFindMany", "db", "post", "id", "cache", "findUnique", "where", "createPost", "input", "create", "data", "updatePost", "update", "deletePost", "delete"] +} diff --git a/2022-12-01-service-caching/api/jest.config.js b/2022-12-01-service-caching/api/jest.config.js new file mode 100644 index 00000000..932fc82d --- /dev/null +++ b/2022-12-01-service-caching/api/jest.config.js @@ -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 diff --git a/2022-12-01-service-caching/api/jsconfig.json b/2022-12-01-service-caching/api/jsconfig.json new file mode 100644 index 00000000..8dd67d0a --- /dev/null +++ b/2022-12-01-service-caching/api/jsconfig.json @@ -0,0 +1,34 @@ +{ + "compilerOptions": { + "noEmit": true, + "allowJs": true, + "esModuleInterop": true, + "target": "esnext", + "module": "esnext", + "moduleResolution": "node", + "baseUrl": "./", + "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"], + }, + "include": [ + "src", + "../.redwood/types/includes/all-*", + "../.redwood/types/includes/api-*", + "../types" + ] +} diff --git a/2022-12-01-service-caching/api/package.json b/2022-12-01-service-caching/api/package.json new file mode 100644 index 00000000..33c1776c --- /dev/null +++ b/2022-12-01-service-caching/api/package.json @@ -0,0 +1,10 @@ +{ + "name": "api", + "version": "0.0.0", + "private": true, + "dependencies": { + "@redwoodjs/api": "3.5.0", + "@redwoodjs/graphql-server": "3.5.0", + "memjs": "^1.3.0" + } +} diff --git a/2022-12-01-service-caching/api/server.config.js b/2022-12-01-service-caching/api/server.config.js new file mode 100644 index 00000000..b012191d --- /dev/null +++ b/2022-12-01-service-caching/api/server.config.js @@ -0,0 +1,52 @@ +/** + * This file allows you to configure the Fastify Server settings + * used by the RedwoodJS dev server. + * + * It also applies when running RedwoodJS with `yarn rw serve`. + * + * For the Fastify server options that you can set, see: + * https://www.fastify.io/docs/latest/Reference/Server/#factory + * + * Examples include: logger settings, timeouts, maximum payload limits, and more. + * + * Note: This configuration does not apply in a serverless deploy. + */ + +/** @type {import('fastify').FastifyServerOptions} */ +const config = { + requestTimeout: 15_000, + logger: { + // Note: If running locally using `yarn rw serve` you may want to adust + // the default non-development level to `info` + level: process.env.NODE_ENV === 'development' ? 'debug' : 'warn', + }, +} + +/** + * You can also register Fastify plugins and additional routes for the API and Web sides + * in the configureFastify function. + * + * This function has access to the Fastify instance and options, such as the side + * (web, api, or proxy) that is being configured and other settings like the apiRootPath + * of the functions endpoint. + * + * Note: This configuration does not apply in a serverless deploy. + */ + +/** @type {import('@redwoodjs/api-server/dist/fastify').FastifySideConfigFn} */ +const configureFastify = async (fastify, options) => { + if (options.side === 'api') { + fastify.log.info({ custom: { options } }, 'Configuring api side') + } + + if (options.side === 'web') { + fastify.log.info({ custom: { options } }, 'Configuring web side') + } + + return fastify +} + +module.exports = { + config, + configureFastify, +} diff --git a/2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.js b/2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.js new file mode 100644 index 00000000..df6083b1 --- /dev/null +++ b/2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.js @@ -0,0 +1,22 @@ +import gql from 'graphql-tag' + +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 +` + +const validate = ({ directiveArgs }) => { + const { roles } = directiveArgs + applicationRequireAuth({ roles }) +} + +const requireAuth = createValidatorDirective(schema, validate) + +export default requireAuth diff --git a/2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.test.js b/2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.test.js new file mode 100644 index 00000000..0f01aa36 --- /dev/null +++ b/2022-12-01-service-caching/api/src/directives/requireAuth/requireAuth.test.js @@ -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() + }) +}) diff --git a/2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.js b/2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.js new file mode 100644 index 00000000..e85b94ae --- /dev/null +++ b/2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.js @@ -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 diff --git a/2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.test.js b/2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.test.js new file mode 100644 index 00000000..88d99a56 --- /dev/null +++ b/2022-12-01-service-caching/api/src/directives/skipAuth/skipAuth.test.js @@ -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') + }) +}) diff --git a/2022-12-01-service-caching/api/src/functions/graphql.js b/2022-12-01-service-caching/api/src/functions/graphql.js new file mode 100644 index 00000000..f395c3b0 --- /dev/null +++ b/2022-12-01-service-caching/api/src/functions/graphql.js @@ -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() + }, +}) diff --git a/2022-12-01-service-caching/api/src/graphql/.keep b/2022-12-01-service-caching/api/src/graphql/.keep new file mode 100644 index 00000000..e69de29b diff --git a/2022-12-01-service-caching/api/src/graphql/posts.sdl.js b/2022-12-01-service-caching/api/src/graphql/posts.sdl.js new file mode 100644 index 00000000..e492b3bd --- /dev/null +++ b/2022-12-01-service-caching/api/src/graphql/posts.sdl.js @@ -0,0 +1,30 @@ +export const schema = gql` + type Post { + id: Int! + title: String! + body: String! + createdAt: DateTime! + updatedAt: DateTime! + } + + type Query { + posts: [Post!]! @requireAuth + post(id: Int!): Post @requireAuth + } + + input CreatePostInput { + title: String! + body: String! + } + + input UpdatePostInput { + title: String + body: String + } + + type Mutation { + createPost(input: CreatePostInput!): Post! @requireAuth + updatePost(id: Int!, input: UpdatePostInput!): Post! @requireAuth + deletePost(id: Int!): Post! @requireAuth + } +` diff --git a/2022-12-01-service-caching/api/src/lib/auth.js b/2022-12-01-service-caching/api/src/lib/auth.js new file mode 100644 index 00000000..f98fe93a --- /dev/null +++ b/2022-12-01-service-caching/api/src/lib/auth.js @@ -0,0 +1,25 @@ +/** + * 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() +} diff --git a/2022-12-01-service-caching/api/src/lib/cache.ts b/2022-12-01-service-caching/api/src/lib/cache.ts new file mode 100644 index 00000000..adbf786e --- /dev/null +++ b/2022-12-01-service-caching/api/src/lib/cache.ts @@ -0,0 +1,30 @@ +import { + createCache, + InMemoryClient, + MemcachedClient, +} from '@redwoodjs/api/cache' + +import { logger } from './logger' + +const memJsFormattedLogger = { + log: (msg: string) => logger.error(msg), +} + +export let client: InMemoryClient | MemcachedClient + +if (process.env.NODE_ENV === 'test') { + client = new InMemoryClient() +} else { + try { + client = new MemcachedClient(process.env.CACHE_HOST, { + logger: memJsFormattedLogger, + }) + } catch (e) { + logger.error(`Could not connect to cache: ${e.message}`) + } +} + +export const { cache, cacheFindMany } = createCache(client, { + logger, + timeout: 500, +}) diff --git a/2022-12-01-service-caching/api/src/lib/db.js b/2022-12-01-service-caching/api/src/lib/db.js new file mode 100644 index 00000000..5006d00a --- /dev/null +++ b/2022-12-01-service-caching/api/src/lib/db.js @@ -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'], +}) diff --git a/2022-12-01-service-caching/api/src/lib/logger.js b/2022-12-01-service-caching/api/src/lib/logger.js new file mode 100644 index 00000000..150a3097 --- /dev/null +++ b/2022-12-01-service-caching/api/src/lib/logger.js @@ -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({}) diff --git a/2022-12-01-service-caching/api/src/services/.keep b/2022-12-01-service-caching/api/src/services/.keep new file mode 100644 index 00000000..e69de29b diff --git a/2022-12-01-service-caching/api/src/services/posts/posts.js b/2022-12-01-service-caching/api/src/services/posts/posts.js new file mode 100644 index 00000000..3a9f71a7 --- /dev/null +++ b/2022-12-01-service-caching/api/src/services/posts/posts.js @@ -0,0 +1,38 @@ +import { cache, cacheFindMany } from 'src/lib/cache' +import { db } from 'src/lib/db' + +function delay(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +export const posts = () => { + return cacheFindMany(`posts`, db.post) +} + +export const post = async ({ id }) => { + return cache(['post', id], async () => { + await delay(2000) + return db.post.findUnique({ + where: { id }, + }) + }) +} + +export const createPost = ({ input }) => { + return db.post.create({ + data: input, + }) +} + +export const updatePost = ({ id, input }) => { + return db.post.update({ + data: input, + where: { id }, + }) +} + +export const deletePost = ({ id }) => { + return db.post.delete({ + where: { id }, + }) +} diff --git a/2022-12-01-service-caching/api/src/services/posts/posts.scenarios.js b/2022-12-01-service-caching/api/src/services/posts/posts.scenarios.js new file mode 100644 index 00000000..caedfef5 --- /dev/null +++ b/2022-12-01-service-caching/api/src/services/posts/posts.scenarios.js @@ -0,0 +1,18 @@ +export const standard = defineScenario({ + post: { + one: { + data: { + title: 'String', + body: 'String', + updatedAt: '2022-12-01T17:38:47.194Z', + }, + }, + two: { + data: { + title: 'String', + body: 'String', + updatedAt: '2022-12-01T17:38:47.194Z', + }, + }, + }, +}) diff --git a/2022-12-01-service-caching/api/src/services/posts/posts.test.js b/2022-12-01-service-caching/api/src/services/posts/posts.test.js new file mode 100644 index 00000000..821b656c --- /dev/null +++ b/2022-12-01-service-caching/api/src/services/posts/posts.test.js @@ -0,0 +1,52 @@ +import { posts, post, createPost, updatePost, deletePost } from './posts' + +// Generated boilerplate tests do not account for all circumstances +// and can fail without adjustments, e.g. Float. +// Please refer to the RedwoodJS Testing Docs: +// https://redwoodjs.com/docs/testing#testing-services +// https://redwoodjs.com/docs/testing#jest-expect-type-considerations + +describe('posts', () => { + scenario('returns all posts', async (scenario) => { + const result = await posts() + + expect(result.length).toEqual(Object.keys(scenario.post).length) + }) + + scenario('returns a single post', async (scenario) => { + const result = await post({ id: scenario.post.one.id }) + + expect(result).toEqual(scenario.post.one) + }) + + scenario('creates a post', async () => { + const result = await createPost({ + input: { + title: 'String', + body: 'String', + updatedAt: '2022-12-01T17:38:47.188Z', + }, + }) + + expect(result.title).toEqual('String') + expect(result.body).toEqual('String') + expect(result.updatedAt).toEqual(new Date('2022-12-01T17:38:47.188Z')) + }) + + scenario('updates a post', async (scenario) => { + const original = await post({ id: scenario.post.one.id }) + const result = await updatePost({ + id: original.id, + input: { title: 'String2' }, + }) + + expect(result.title).toEqual('String2') + }) + + scenario('deletes a post', async (scenario) => { + const original = await deletePost({ id: scenario.post.one.id }) + const result = await post({ id: original.id }) + + expect(result).toEqual(null) + }) +}) diff --git a/2022-12-01-service-caching/api/types/graphql.d.ts b/2022-12-01-service-caching/api/types/graphql.d.ts new file mode 100644 index 00000000..7b885af1 --- /dev/null +++ b/2022-12-01-service-caching/api/types/graphql.d.ts @@ -0,0 +1,314 @@ +import { Prisma } from "@prisma/client" +import { MergePrismaWithSdlTypes, MakeRelationsOptional } from '@redwoodjs/api' +import { Post as PrismaPost } from '@prisma/client' +import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql'; +import { RedwoodGraphQLContext } from '@redwoodjs/graphql-server/dist/functions/types'; +export type Maybe = T | null; +export type InputMaybe = Maybe; +export type Exact = { [K in keyof T]: T[K] }; +export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; +export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; +export type ResolverFn = ( + args?: TArgs, + obj?: { root: TParent; context: TContext; info: GraphQLResolveInfo } + ) => TResult | Promise +export type RequireFields = Omit & { [P in K]-?: NonNullable }; +export type OptArgsResolverFn = ( + args?: TArgs, + obj?: { root: TParent; context: TContext; info: GraphQLResolveInfo } + ) => TResult | Promise + + export type RequiredResolverFn = ( + args: TArgs, + obj: { root: TParent; context: TContext; info: GraphQLResolveInfo } + ) => TResult | Promise +/** All built-in and custom scalars, mapped to their actual values */ +export type Scalars = { + ID: string; + String: string; + Boolean: boolean; + Int: number; + Float: number; + BigInt: number; + Date: Date | string; + DateTime: Date | string; + JSON: Prisma.JsonValue; + JSONObject: Prisma.JsonObject; + Time: Date | string; +}; + +export type CreatePostInput = { + body: Scalars['String']; + title: Scalars['String']; +}; + +export type Mutation = { + __typename?: 'Mutation'; + createPost: Post; + deletePost: Post; + updatePost: Post; +}; + + +export type MutationcreatePostArgs = { + input: CreatePostInput; +}; + + +export type MutationdeletePostArgs = { + id: Scalars['Int']; +}; + + +export type MutationupdatePostArgs = { + id: Scalars['Int']; + input: UpdatePostInput; +}; + +export type Post = { + __typename?: 'Post'; + body: Scalars['String']; + createdAt: Scalars['DateTime']; + id: Scalars['Int']; + title: Scalars['String']; + updatedAt: Scalars['DateTime']; +}; + +/** About the Redwood queries. */ +export type Query = { + __typename?: 'Query'; + post?: Maybe; + posts: Array; + /** Fetches the Redwood root schema. */ + redwood?: Maybe; +}; + + +/** About the Redwood queries. */ +export type QuerypostArgs = { + id: Scalars['Int']; +}; + +/** + * The RedwoodJS Root Schema + * + * Defines details about RedwoodJS such as the current user and version information. + */ +export type Redwood = { + __typename?: 'Redwood'; + /** The current user. */ + currentUser?: Maybe; + /** The version of Prisma. */ + prismaVersion?: Maybe; + /** The version of Redwood. */ + version?: Maybe; +}; + +export type UpdatePostInput = { + body?: InputMaybe; + title?: InputMaybe; +}; + +type MaybeOrArrayOfMaybe = T | Maybe | Maybe[]; +type AllMappedModels = MaybeOrArrayOfMaybe + + +export type ResolverTypeWrapper = Promise | T; + +export type Resolver = ResolverFn; + +export type SubscriptionSubscribeFn = ( + parent: TParent, + args: TArgs, + context: TContext, + info: GraphQLResolveInfo +) => AsyncIterable | Promise>; + +export type SubscriptionResolveFn = ( + parent: TParent, + args: TArgs, + context: TContext, + info: GraphQLResolveInfo +) => TResult | Promise; + +export interface SubscriptionSubscriberObject { + subscribe: SubscriptionSubscribeFn<{ [key in TKey]: TResult }, TParent, TContext, TArgs>; + resolve?: SubscriptionResolveFn; +} + +export interface SubscriptionResolverObject { + subscribe: SubscriptionSubscribeFn; + resolve: SubscriptionResolveFn; +} + +export type SubscriptionObject = + | SubscriptionSubscriberObject + | SubscriptionResolverObject; + +export type SubscriptionResolver = + | ((...args: any[]) => SubscriptionObject) + | SubscriptionObject; + +export type TypeResolveFn = ( + parent: TParent, + context: TContext, + info: GraphQLResolveInfo +) => Maybe | Promise>; + +export type IsTypeOfResolverFn = (obj: T, context: TContext, info: GraphQLResolveInfo) => boolean | Promise; + +export type NextResolverFn = () => Promise; + +export type DirectiveResolverFn = ( + next: NextResolverFn, + parent: TParent, + args: TArgs, + context: TContext, + info: GraphQLResolveInfo +) => TResult | Promise; + +/** Mapping between all available schema types and the resolvers types */ +export type ResolversTypes = { + BigInt: ResolverTypeWrapper; + Boolean: ResolverTypeWrapper; + CreatePostInput: CreatePostInput; + Date: ResolverTypeWrapper; + DateTime: ResolverTypeWrapper; + Int: ResolverTypeWrapper; + JSON: ResolverTypeWrapper; + JSONObject: ResolverTypeWrapper; + Mutation: ResolverTypeWrapper<{}>; + Post: ResolverTypeWrapper, AllMappedModels>>; + Query: ResolverTypeWrapper<{}>; + Redwood: ResolverTypeWrapper; + String: ResolverTypeWrapper; + Time: ResolverTypeWrapper; + UpdatePostInput: UpdatePostInput; +}; + +/** Mapping between all available schema types and the resolvers parents */ +export type ResolversParentTypes = { + BigInt: Scalars['BigInt']; + Boolean: Scalars['Boolean']; + CreatePostInput: CreatePostInput; + Date: Scalars['Date']; + DateTime: Scalars['DateTime']; + Int: Scalars['Int']; + JSON: Scalars['JSON']; + JSONObject: Scalars['JSONObject']; + Mutation: {}; + Post: MergePrismaWithSdlTypes, AllMappedModels>; + Query: {}; + Redwood: Redwood; + String: Scalars['String']; + Time: Scalars['Time']; + UpdatePostInput: UpdatePostInput; +}; + +export type requireAuthDirectiveArgs = { + roles?: Maybe>>; +}; + +export type requireAuthDirectiveResolver = DirectiveResolverFn; + +export type skipAuthDirectiveArgs = { }; + +export type skipAuthDirectiveResolver = DirectiveResolverFn; + +export interface BigIntScalarConfig extends GraphQLScalarTypeConfig { + name: 'BigInt'; +} + +export interface DateScalarConfig extends GraphQLScalarTypeConfig { + name: 'Date'; +} + +export interface DateTimeScalarConfig extends GraphQLScalarTypeConfig { + name: 'DateTime'; +} + +export interface JSONScalarConfig extends GraphQLScalarTypeConfig { + name: 'JSON'; +} + +export interface JSONObjectScalarConfig extends GraphQLScalarTypeConfig { + name: 'JSONObject'; +} + +export type MutationResolvers = { + createPost: Resolver>; + deletePost: Resolver>; + updatePost: Resolver>; +}; + +export type MutationRelationResolvers = { + createPost?: RequiredResolverFn>; + deletePost?: RequiredResolverFn>; + updatePost?: RequiredResolverFn>; +}; + +export type PostResolvers = { + body: OptArgsResolverFn; + createdAt: OptArgsResolverFn; + id: OptArgsResolverFn; + title: OptArgsResolverFn; + updatedAt: OptArgsResolverFn; + __isTypeOf?: IsTypeOfResolverFn; +}; + +export type PostRelationResolvers = { + body?: RequiredResolverFn; + createdAt?: RequiredResolverFn; + id?: RequiredResolverFn; + title?: RequiredResolverFn; + updatedAt?: RequiredResolverFn; + __isTypeOf?: IsTypeOfResolverFn; +}; + +export type QueryResolvers = { + post: Resolver, ParentType, ContextType, RequireFields>; + posts: OptArgsResolverFn, ParentType, ContextType>; + redwood: OptArgsResolverFn, ParentType, ContextType>; +}; + +export type QueryRelationResolvers = { + post?: RequiredResolverFn, ParentType, ContextType, RequireFields>; + posts?: RequiredResolverFn, ParentType, ContextType>; + redwood?: RequiredResolverFn, ParentType, ContextType>; +}; + +export type RedwoodResolvers = { + currentUser: OptArgsResolverFn, ParentType, ContextType>; + prismaVersion: OptArgsResolverFn, ParentType, ContextType>; + version: OptArgsResolverFn, ParentType, ContextType>; + __isTypeOf?: IsTypeOfResolverFn; +}; + +export type RedwoodRelationResolvers = { + currentUser?: RequiredResolverFn, ParentType, ContextType>; + prismaVersion?: RequiredResolverFn, ParentType, ContextType>; + version?: RequiredResolverFn, ParentType, ContextType>; + __isTypeOf?: IsTypeOfResolverFn; +}; + +export interface TimeScalarConfig extends GraphQLScalarTypeConfig { + name: 'Time'; +} + +export type Resolvers = { + BigInt: GraphQLScalarType; + Date: GraphQLScalarType; + DateTime: GraphQLScalarType; + JSON: GraphQLScalarType; + JSONObject: GraphQLScalarType; + Mutation: MutationResolvers; + Post: PostResolvers; + Query: QueryResolvers; + Redwood: RedwoodResolvers; + Time: GraphQLScalarType; +}; + +export type DirectiveResolvers = { + requireAuth: requireAuthDirectiveResolver; + skipAuth: skipAuthDirectiveResolver; +}; diff --git a/2022-12-01-service-caching/graphql.config.js b/2022-12-01-service-caching/graphql.config.js new file mode 100644 index 00000000..2da7862f --- /dev/null +++ b/2022-12-01-service-caching/graphql.config.js @@ -0,0 +1,5 @@ +const { getPaths } = require('@redwoodjs/internal') + +module.exports = { + schema: getPaths().generated.schema, +} diff --git a/2022-12-01-service-caching/jest.config.js b/2022-12-01-service-caching/jest.config.js new file mode 100644 index 00000000..c6b395cb --- /dev/null +++ b/2022-12-01-service-caching/jest.config.js @@ -0,0 +1,8 @@ +// This the Redwood root jest config +// Each side, e.g. ./web/ and ./api/ has specific config that references this root +// More info at https://redwoodjs.com/docs/project-configuration-dev-test-build + +module.exports = { + rootDir: '.', + projects: ['/{*,!(node_modules)/**/}/jest.config.js'], +} diff --git a/2022-12-01-service-caching/package.json b/2022-12-01-service-caching/package.json new file mode 100644 index 00000000..1736ddd8 --- /dev/null +++ b/2022-12-01-service-caching/package.json @@ -0,0 +1,25 @@ +{ + "private": true, + "workspaces": { + "packages": [ + "api", + "web", + "packages/*" + ] + }, + "devDependencies": { + "@redwoodjs/core": "3.5.0" + }, + "eslintConfig": { + "extends": "@redwoodjs/eslint-config", + "root": true + }, + "engines": { + "node": ">=14.19 <=16.x", + "yarn": ">=1.15" + }, + "prisma": { + "seed": "yarn rw exec seed" + }, + "packageManager": "yarn@3.2.4" +} diff --git a/2022-12-01-service-caching/prettier.config.js b/2022-12-01-service-caching/prettier.config.js new file mode 100644 index 00000000..45058f7a --- /dev/null +++ b/2022-12-01-service-caching/prettier.config.js @@ -0,0 +1,18 @@ +// https://prettier.io/docs/en/options.html +/** @type {import('prettier').RequiredOptions} */ +module.exports = { + trailingComma: 'es5', + bracketSpacing: true, + tabWidth: 2, + semi: false, + singleQuote: true, + arrowParens: 'always', + overrides: [ + { + files: 'Routes.*', + options: { + printWidth: 999, + }, + }, + ], +} diff --git a/2022-12-01-service-caching/redwood.toml b/2022-12-01-service-caching/redwood.toml new file mode 100644 index 00000000..11270a20 --- /dev/null +++ b/2022-12-01-service-caching/redwood.toml @@ -0,0 +1,16 @@ +# This file contains the configuration settings for your Redwood app. +# This file is also what makes your Redwood app a Redwood app. +# If you remove it and try to run `yarn rw dev`, you'll get an error. +# +# For the full list of options, see the "App Configuration: redwood.toml" doc: +# https://redwoodjs.com/docs/app-configuration-redwood-toml + +[web] + title = "Redwood App" + port = 8910 + apiUrl = "/.redwood/functions" # you can customise graphql and dbauth urls individually too: see https://redwoodjs.com/docs/app-configuration-redwood-toml#api-paths + includeEnvironmentVariables = [] # any ENV vars that should be available to the web side, see https://redwoodjs.com/docs/environment-variables#web +[api] + port = 8911 +[browser] + open = true diff --git a/2022-12-01-service-caching/scripts/.keep b/2022-12-01-service-caching/scripts/.keep new file mode 100644 index 00000000..e69de29b diff --git a/2022-12-01-service-caching/scripts/jsconfig.json b/2022-12-01-service-caching/scripts/jsconfig.json new file mode 100644 index 00000000..a2a4293a --- /dev/null +++ b/2022-12-01-service-caching/scripts/jsconfig.json @@ -0,0 +1,42 @@ +{ + "compilerOptions": { + "noEmit": true, + "allowJs": true, + "esModuleInterop": true, + "target": "esnext", + "module": "esnext", + "moduleResolution": "node", + "baseUrl": "./", + "paths": { + "$api/*": [ + "../api/*", + ], + "api/*": [ + "../api/*", + ], + "$web/*": [ + "../web/*", + ], + "web/*": [ + "../web/*", + ], + "$web/src/*": [ + "../web/src/*", + "../.redwood/types/mirror/web/src/*" + ], + "web/src/*": [ + "../web/src/*", + "../.redwood/types/mirror/web/src/*" + ], + "types/*": ["../types/*", "../web/types/*", "../api/types/*"], + }, + "typeRoots": ["../node_modules/@types"], + "jsx": "preserve", + }, + "include": [ + ".", + "../.redwood/types/includes/all-*", + "../.redwood/types/includes/web-*", + "../types" + ] +} diff --git a/2022-12-01-service-caching/scripts/seed.js b/2022-12-01-service-caching/scripts/seed.js new file mode 100644 index 00000000..6c6e9485 --- /dev/null +++ b/2022-12-01-service-caching/scripts/seed.js @@ -0,0 +1,62 @@ +import { db } from 'api/src/lib/db' + +export default async () => { + try { + // + // Manually seed via `yarn rw prisma db seed` + // Seeds automatically with `yarn rw prisma migrate dev` and `yarn rw prisma migrate reset` + // + // Update "const data = []" to match your data model and seeding needs + // + const data = [ + // To try this example data with the UserExample model in schema.prisma, + // uncomment the lines below and run 'yarn rw prisma migrate dev' + // + // { name: 'alice', email: 'alice@example.com' }, + // { name: 'mark', email: 'mark@example.com' }, + // { name: 'jackie', email: 'jackie@example.com' }, + // { name: 'bob', email: 'bob@example.com' }, + ] + console.log( + "\nUsing the default './scripts/seed.{js,ts}' template\nEdit the file to add seed data\n" + ) + + // Note: if using PostgreSQL, using `createMany` to insert multiple records is much faster + // @see: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#createmany + Promise.all( + // + // Change to match your data model and seeding needs + // + data.map(async (data) => { + const record = await db.userExample.create({ data }) + console.log(record) + }) + ) + + // If using dbAuth and seeding users, you'll need to add a `hashedPassword` + // and associated `salt` to their record. Here's how to create them using + // the same algorithm that dbAuth uses internally: + // + // import { hashPassword } from '@redwoodjs/api' + // + // const users = [ + // { name: 'john', email: 'john@example.com', password: 'secret1' }, + // { name: 'jane', email: 'jane@example.com', password: 'secret2' } + // ] + // + // for (user of users) { + // const [hashedPassword, salt] = hashPassword(user.password) + // await db.user.create({ + // data: { + // name: user.name, + // email: user.email, + // hashedPassword, + // salt + // } + // }) + // } + } catch (error) { + console.warn('Please define your seed data.') + console.error(error) + } +} diff --git a/2022-12-01-service-caching/web/.DS_Store b/2022-12-01-service-caching/web/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..06dcdf7010727c54b6c3de570ffa4b6370294bef GIT binary patch literal 6148 zcmeHKJ5Iwu5S?`bG6F$_ghaVL4Y`4dOplZcL{1_=vSpKyf&%1%8VXu^E^@ zBD~pIC3YM{2MEnbvu{10o%ng<@e+~g%|>0K4iSY=#$W@(3SmEMLjrHv0vfl*IStd( zZhx3(MI%}be~|%xcI#Boj1o$z@%*;0m;G5D$4Qzeu!k3ugO9t%*P~|4=-Zg$th!GQ zu>`efN|WUs^z^pyRBPYvzMs5EZJ(Fri>gg;tzIzTdTVq^SGe~vP3yaM=b`Sc*1dVQ zd-qbel3%U8I-*wJ{onW}s3-%%z%If;uV#%0QigmRxqY{~v#S{;wzLl`^0V{3`}bJMPCl+>-CDjho|MYoP-u3&)j$ k^At>6D@H80;(e$S*ey?hk;hUH7KnZbI2v?N27Z)*Pb=bP5&!@I literal 0 HcmV?d00001 diff --git a/2022-12-01-service-caching/web/jest.config.js b/2022-12-01-service-caching/web/jest.config.js new file mode 100644 index 00000000..0e54869e --- /dev/null +++ b/2022-12-01-service-caching/web/jest.config.js @@ -0,0 +1,8 @@ +// More info at https://redwoodjs.com/docs/project-configuration-dev-test-build + +const config = { + rootDir: '../', + preset: '@redwoodjs/testing/config/jest/web', +} + +module.exports = config diff --git a/2022-12-01-service-caching/web/jsconfig.json b/2022-12-01-service-caching/web/jsconfig.json new file mode 100644 index 00000000..4f491a12 --- /dev/null +++ b/2022-12-01-service-caching/web/jsconfig.json @@ -0,0 +1,38 @@ +{ + "compilerOptions": { + "noEmit": true, + "allowJs": true, + "esModuleInterop": true, + "target": "esnext", + "module": "esnext", + "moduleResolution": "node", + "baseUrl": "./", + "rootDirs": [ + "./src", + "../.redwood/types/mirror/web/src", + "../api/src", + "../.redwood/types/mirror/api/src" + ], + "paths": { + "src/*": [ + "./src/*", + "../.redwood/types/mirror/web/src/*", + "../api/src/*", + "../.redwood/types/mirror/api/src/*" + ], + "$api/*": [ "../api/*" ], + "types/*": ["./types/*", "../types/*"], + "@redwoodjs/testing": ["../node_modules/@redwoodjs/testing/web"] + }, + "typeRoots": ["../node_modules/@types", "./node_modules/@types"], + "types": ["jest", "@testing-library/jest-dom"], + "jsx": "preserve", + }, + "include": [ + "src", + "../.redwood/types/includes/all-*", + "../.redwood/types/includes/web-*", + "../types", + "./types" + ] +} diff --git a/2022-12-01-service-caching/web/package.json b/2022-12-01-service-caching/web/package.json new file mode 100644 index 00000000..c88728a9 --- /dev/null +++ b/2022-12-01-service-caching/web/package.json @@ -0,0 +1,24 @@ +{ + "name": "web", + "version": "0.0.0", + "private": true, + "browserslist": { + "development": [ + "last 1 version" + ], + "production": [ + "defaults", + "not IE 11", + "not IE_Mob 11" + ] + }, + "dependencies": { + "@redwoodjs/forms": "3.5.0", + "@redwoodjs/router": "3.5.0", + "@redwoodjs/web": "3.5.0", + "humanize-string": "2.1.0", + "prop-types": "15.8.1", + "react": "17.0.2", + "react-dom": "17.0.2" + } +} diff --git a/2022-12-01-service-caching/web/public/README.md b/2022-12-01-service-caching/web/public/README.md new file mode 100644 index 00000000..6df2fa25 --- /dev/null +++ b/2022-12-01-service-caching/web/public/README.md @@ -0,0 +1,36 @@ +# Static Assets +Use this folder to add static files directly to your app. All included files and folders will be copied directly into the `/dist` folder (created when Webpack builds for production). They will also be available during development when you run `yarn rw dev`. +>Note: files will *not* hot reload while the development server is running. You'll need to manually stop/start to access file changes. + +### Example Use +A file like `favicon.png` will be copied to `/dist/favicon.png`. A folder containing a file such as `static-files/my-logo.jpg` will be copied to `/dist/static-files/my-logo.jpg`. These can be referenced in your code directly without any special handling, e.g. +``` + +``` +and +``` + alt="Logo" /> +``` + +Behind the scenes, we are using Webpack's ["copy-webpack-plugin"](https://github.com/webpack-contrib/copy-webpack-plugin). + +## Best Practices +Because assets in this folder are bypassing the javascript module system, **this folder should be used sparingly** for assets such as favicons, robots.txt, manifests, libraries incompatible with Webpack, etc. + +In general, it's best to import files directly into a template, page, or component. This allows Webpack to include that file in the bundle, which ensures Webpack will correctly process and move assets into the distribution folder, providing error checks and correct paths along the way. + +### Example Asset Import with Webpack +Instead of handling our logo image as a static file per the example above, we can do the following: +``` +import React from "react" +import logo from "./my-logo.jpg" + + +function Header() { + return Logo +} + +export default Header +``` + +Behind the scenes, we are using Webpack's ["file-loader"](https://webpack.js.org/loaders/file-loader/) and ["url-loader](https://webpack.js.org/loaders/url-loader/) (for files smaller than 10kb). diff --git a/2022-12-01-service-caching/web/public/favicon.png b/2022-12-01-service-caching/web/public/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..47414294173cb0795dcafb8813599fc382282556 GIT binary patch literal 1741 zcmV;;1~U1HP)u3dvWaK1Jt7p7xtk~lm38V(vb%~9EcN4itP(!;||l>?RBBL}g^A8<`Fn z=_ofw?w2~Qt#0f9Ac3O;;Nt1}TFWmPb1YZ9hDBXZ zTK55jh;jRpRArCUs~@6m!BMLSuZE&5;HTqrDc^;f)?K|FaV6o1RTFbt+uA;);7z?5 z9axBZCgX!V;dhWl*HZCE&V7oz;oZ;*lOh^wZ2aYlLI<1rXkc0&HH!|5!S0|*s- zM*~yi#Ef4dES_G+_-z+`S<%x__Ulk8{Z?I!;wv8DmN?3t1H$+fJ*q^w!} z8`oOx{i(WL4oLgKN0~^gQyJ3t#+tnIhR=h}6@BVu1&_1g7*O6j$-5z)KLsPi3dqCH zq+n<+)2a$Afvr|B97(#s5f6-oU6qYHP<2rWEKfC)aEc=?j9nPwEyIiT4XCI%BScNpoU1Cro6M@BSt>YU4@z^JQPbj- zbMl0tf(CkBNTVH0run?8E#6lyouay;Bf8|_ud%WyA2Dkqc}nAEGkyiO!|#6>OX~jC z_3u?iQ>Xm%XNGGb_3~zzqyj(lHYRC##{sV_zNQl$KP40jQHRR#WeJ!akxfaL;HU(y z@6A7KA;pjflPx?{&_wwQ<6?f(Uld(h*XSf+Ct`QR3EDfau;y#nNiKfJ`Ny24=O+_9 z{chAh!5R0T(`<1ayxDvCtBZ?9Rn)QBoddzqchGPN4C8rB2tQ(*#m6zlySN7XwxM)X zNo%g}Q*?B_&%_K;!PvNxj9-D>BYn6zcIb@VGE=-?gP+zjpQ4x$*@_cm*TL-MtWeV+ z%v$Vh+2e#jDJ4Yc3NPgE9Uhr~V;6)j#bgMC+5!L2yYdX5ef->+k9d_?db{`}fWW+F zU&GKd9pW?cv0e8pA%20doi=OgaTV=dLOHx7cgAQlYDkLWaAUksGbO`Z7+>qo}~5K=?ZI!b@vaF5}r7- zyP2aiwSn}KbwGhrQ0A?W4L_Jwg?C#vAElLzpK~}}&ny0d@_GVhUqVEfXX9}XI8%B; z;BYTG$dM}6WS8urD4fqn$733@mNss6jB7yHY*76e*L=X6apM|Dgg^tZhpge9{Ojy9 z{Sl&x=vUbHU+7KFQEas^U*jQ8^rj_XAzI=0y_Nmx3ChT&K?_-b!N10g5+C9TqMGZ@!a>mh#`}nJM>Cu2v@32F*rQ(x05Xb64 zV-ML!u$4W31M7A@mi~3fnSOQSZ->>TC+02Mt+0csMl0*2TCklB$VOH11pW{4 zD1)V+^h4n@OYlO&;Z!-dk{(LVtA%;(o#!>jYgG>s%eL0iXx~jJsrfL3rwo;cc52kP zRnvwZId>`-FV`PUvUKk4gU&nzX&+gTEm1bNsCdaXc zvaOny-3X43Fs?Jn;>*U?jaR1`9KIVP?p(?ulraQZc;T0UKos^SChGJoJYVu1%?E0v zDGNOfZKPrPKtyFYEU~bZZ~rB{4X2ko>_VJlJw3rw-!>TIT6R!3;POq5yNZdnfu$Ao j!CVlN4fQVi0D=DiS&&%ubg+{I00000NkvXXu0mjf8bDG2 literal 0 HcmV?d00001 diff --git a/2022-12-01-service-caching/web/public/robots.txt b/2022-12-01-service-caching/web/public/robots.txt new file mode 100644 index 00000000..eb053628 --- /dev/null +++ b/2022-12-01-service-caching/web/public/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: diff --git a/2022-12-01-service-caching/web/src/App.js b/2022-12-01-service-caching/web/src/App.js new file mode 100644 index 00000000..5e7beac7 --- /dev/null +++ b/2022-12-01-service-caching/web/src/App.js @@ -0,0 +1,20 @@ +import { FatalErrorBoundary, RedwoodProvider } from '@redwoodjs/web' +import { RedwoodApolloProvider } from '@redwoodjs/web/apollo' + +import FatalErrorPage from 'src/pages/FatalErrorPage' +import Routes from 'src/Routes' + +import './scaffold.css' +import './index.css' + +const App = () => ( + + + + + + + +) + +export default App diff --git a/2022-12-01-service-caching/web/src/Routes.js b/2022-12-01-service-caching/web/src/Routes.js new file mode 100644 index 00000000..38e538a0 --- /dev/null +++ b/2022-12-01-service-caching/web/src/Routes.js @@ -0,0 +1,30 @@ +// In this file, all Page components from 'src/pages` are auto-imported. Nested +// directories are supported, and should be uppercase. Each subdirectory will be +// prepended onto the component name. +// +// Examples: +// +// 'src/pages/HomePage/HomePage.js' -> HomePage +// 'src/pages/Admin/BooksPage/BooksPage.js' -> AdminBooksPage + +import { Set, Router, Route } from '@redwoodjs/router' + +import ScaffoldLayout from 'src/layouts/ScaffoldLayout' + +const Routes = () => { + return ( + + + + + + + + + + + + ) +} + +export default Routes diff --git a/2022-12-01-service-caching/web/src/components/.keep b/2022-12-01-service-caching/web/src/components/.keep new file mode 100644 index 00000000..e69de29b diff --git a/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.js b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.js new file mode 100644 index 00000000..116bba51 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.js @@ -0,0 +1,27 @@ +export const QUERY = gql` + query FindArticleQuery($id: Int!) { + article: post(id: $id) { + id + title + body + createdAt + } + } +` + +export const Loading = () =>
Loading...
+ +export const Empty = () =>
Empty
+ +export const Failure = ({ error }) => ( +
Error: {error?.message}
+) + +export const Success = ({ article }) => { + return ( +
+

{article.title}

+

{article.body}

+
+ ) +} diff --git a/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.mock.js b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.mock.js new file mode 100644 index 00000000..16a3f0da --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.mock.js @@ -0,0 +1,6 @@ +// Define your own mock data here: +export const standard = (/* vars, { ctx, req } */) => ({ + article: { + id: 42, + }, +}) diff --git a/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.stories.js b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.stories.js new file mode 100644 index 00000000..ed9af26f --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.stories.js @@ -0,0 +1,20 @@ +import { Loading, Empty, Failure, Success } from './ArticleCell' +import { standard } from './ArticleCell.mock' + +export const loading = () => { + return Loading ? : <> +} + +export const empty = () => { + return Empty ? : <> +} + +export const failure = (args) => { + return Failure ? : <> +} + +export const success = (args) => { + return Success ? : <> +} + +export default { title: 'Cells/ArticleCell' } diff --git a/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.test.js b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.test.js new file mode 100644 index 00000000..56a6cb23 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticleCell/ArticleCell.test.js @@ -0,0 +1,41 @@ +import { render } from '@redwoodjs/testing/web' +import { Loading, Empty, Failure, Success } from './ArticleCell' +import { standard } from './ArticleCell.mock' + +// Generated boilerplate tests do not account for all circumstances +// and can fail without adjustments, e.g. Float and DateTime types. +// Please refer to the RedwoodJS Testing Docs: +// https://redwoodjs.com/docs/testing#testing-cells +// https://redwoodjs.com/docs/testing#jest-expect-type-considerations + +describe('ArticleCell', () => { + it('renders Loading successfully', () => { + expect(() => { + render() + }).not.toThrow() + }) + + it('renders Empty successfully', async () => { + expect(() => { + render() + }).not.toThrow() + }) + + it('renders Failure successfully', async () => { + expect(() => { + render() + }).not.toThrow() + }) + + // When you're ready to test the actual output of your component render + // you could test that, for example, certain text is present: + // + // 1. import { screen } from '@redwoodjs/testing/web' + // 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument() + + it('renders Success successfully', async () => { + expect(() => { + render() + }).not.toThrow() + }) +}) diff --git a/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.js b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.js new file mode 100644 index 00000000..dcac6d6e --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.js @@ -0,0 +1,38 @@ +import { Link, routes } from '@redwoodjs/router' + +export const QUERY = gql` + query ArticlesQuery { + articles: posts { + id + title + body + createdAt + } + } +` + +export const Loading = () =>
Loading...
+ +export const Empty = () =>
Empty
+ +export const Failure = ({ error }) => ( +
Error: {error?.message}
+) + +export const Success = ({ articles }) => { + return ( +
+ {articles.map((item) => { + return ( +
+

+ {item.title} +

+
{item.createdAt}
+

{item.body}

+
+ ) + })} +
+ ) +} diff --git a/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.mock.js b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.mock.js new file mode 100644 index 00000000..8eababc6 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.mock.js @@ -0,0 +1,4 @@ +// Define your own mock data here: +export const standard = (/* vars, { ctx, req } */) => ({ + articles: [{ id: 42 }, { id: 43 }, { id: 44 }], +}) diff --git a/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.stories.js b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.stories.js new file mode 100644 index 00000000..049ebb70 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.stories.js @@ -0,0 +1,20 @@ +import { Loading, Empty, Failure, Success } from './ArticlesCell' +import { standard } from './ArticlesCell.mock' + +export const loading = () => { + return Loading ? : <> +} + +export const empty = () => { + return Empty ? : <> +} + +export const failure = (args) => { + return Failure ? : <> +} + +export const success = (args) => { + return Success ? : <> +} + +export default { title: 'Cells/ArticlesCell' } diff --git a/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.test.js b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.test.js new file mode 100644 index 00000000..5514f367 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/ArticlesCell/ArticlesCell.test.js @@ -0,0 +1,41 @@ +import { render } from '@redwoodjs/testing/web' +import { Loading, Empty, Failure, Success } from './ArticlesCell' +import { standard } from './ArticlesCell.mock' + +// Generated boilerplate tests do not account for all circumstances +// and can fail without adjustments, e.g. Float and DateTime types. +// Please refer to the RedwoodJS Testing Docs: +// https://redwoodjs.com/docs/testing#testing-cells +// https://redwoodjs.com/docs/testing#jest-expect-type-considerations + +describe('ArticlesCell', () => { + it('renders Loading successfully', () => { + expect(() => { + render() + }).not.toThrow() + }) + + it('renders Empty successfully', async () => { + expect(() => { + render() + }).not.toThrow() + }) + + it('renders Failure successfully', async () => { + expect(() => { + render() + }).not.toThrow() + }) + + // When you're ready to test the actual output of your component render + // you could test that, for example, certain text is present: + // + // 1. import { screen } from '@redwoodjs/testing/web' + // 2. Add test: expect(screen.getByText('Hello, world')).toBeInTheDocument() + + it('renders Success successfully', async () => { + expect(() => { + render() + }).not.toThrow() + }) +}) diff --git a/2022-12-01-service-caching/web/src/components/Post/EditPostCell/EditPostCell.js b/2022-12-01-service-caching/web/src/components/Post/EditPostCell/EditPostCell.js new file mode 100644 index 00000000..681e8408 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/Post/EditPostCell/EditPostCell.js @@ -0,0 +1,64 @@ +import { navigate, routes } from '@redwoodjs/router' + +import { useMutation } from '@redwoodjs/web' +import { toast } from '@redwoodjs/web/toast' + +import PostForm from 'src/components/Post/PostForm' + +export const QUERY = gql` + query EditPostById($id: Int!) { + post: post(id: $id) { + id + title + body + createdAt + updatedAt + } + } +` +const UPDATE_POST_MUTATION = gql` + mutation UpdatePostMutation($id: Int!, $input: UpdatePostInput!) { + updatePost(id: $id, input: $input) { + id + title + body + createdAt + updatedAt + } + } +` + +export const Loading = () =>
Loading...
+ +export const Failure = ({ error }) => ( +
{error?.message}
+) + +export const Success = ({ post }) => { + const [updatePost, { loading, error }] = useMutation(UPDATE_POST_MUTATION, { + onCompleted: () => { + toast.success('Post updated') + navigate(routes.posts()) + }, + onError: (error) => { + toast.error(error.message) + }, + }) + + const onSave = (input, id) => { + updatePost({ variables: { id, input } }) + } + + return ( +
+
+

+ Edit Post {post?.id} +

+
+
+ +
+
+ ) +} diff --git a/2022-12-01-service-caching/web/src/components/Post/NewPost/NewPost.js b/2022-12-01-service-caching/web/src/components/Post/NewPost/NewPost.js new file mode 100644 index 00000000..63affd23 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/Post/NewPost/NewPost.js @@ -0,0 +1,42 @@ +import { navigate, routes } from '@redwoodjs/router' +import { useMutation } from '@redwoodjs/web' +import { toast } from '@redwoodjs/web/toast' + +import PostForm from 'src/components/Post/PostForm' + +const CREATE_POST_MUTATION = gql` + mutation CreatePostMutation($input: CreatePostInput!) { + createPost(input: $input) { + id + } + } +` + +const NewPost = () => { + const [createPost, { loading, error }] = useMutation(CREATE_POST_MUTATION, { + onCompleted: () => { + toast.success('Post created') + navigate(routes.posts()) + }, + onError: (error) => { + toast.error(error.message) + }, + }) + + const onSave = (input) => { + createPost({ variables: { input } }) + } + + return ( +
+
+

New Post

+
+
+ +
+
+ ) +} + +export default NewPost diff --git a/2022-12-01-service-caching/web/src/components/Post/Post/Post.js b/2022-12-01-service-caching/web/src/components/Post/Post/Post.js new file mode 100644 index 00000000..a7ce8e88 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/Post/Post/Post.js @@ -0,0 +1,84 @@ +import { Link, routes, navigate } from '@redwoodjs/router' +import { useMutation } from '@redwoodjs/web' +import { toast } from '@redwoodjs/web/toast' + +import { timeTag } from 'src/lib/formatters' + +const DELETE_POST_MUTATION = gql` + mutation DeletePostMutation($id: Int!) { + deletePost(id: $id) { + id + } + } +` + +const Post = ({ post }) => { + const [deletePost] = useMutation(DELETE_POST_MUTATION, { + onCompleted: () => { + toast.success('Post deleted') + navigate(routes.posts()) + }, + onError: (error) => { + toast.error(error.message) + }, + }) + + const onDeleteClick = (id) => { + if (confirm('Are you sure you want to delete post ' + id + '?')) { + deletePost({ variables: { id } }) + } + } + + return ( + <> +
+
+

+ Post {post.id} Detail +

+
+ + + + + + + + + + + + + + + + + + + + + + + +
Id{post.id}
Title{post.title}
Body{post.body}
Created at{timeTag(post.createdAt)}
Updated at{timeTag(post.updatedAt)}
+
+ + + ) +} + +export default Post diff --git a/2022-12-01-service-caching/web/src/components/Post/PostCell/PostCell.js b/2022-12-01-service-caching/web/src/components/Post/PostCell/PostCell.js new file mode 100644 index 00000000..ae30553d --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/Post/PostCell/PostCell.js @@ -0,0 +1,25 @@ +import Post from 'src/components/Post/Post' + +export const QUERY = gql` + query FindPostById($id: Int!) { + post: post(id: $id) { + id + title + body + createdAt + updatedAt + } + } +` + +export const Loading = () =>
Loading...
+ +export const Empty = () =>
Post not found
+ +export const Failure = ({ error }) => ( +
{error?.message}
+) + +export const Success = ({ post }) => { + return +} diff --git a/2022-12-01-service-caching/web/src/components/Post/PostForm/PostForm.js b/2022-12-01-service-caching/web/src/components/Post/PostForm/PostForm.js new file mode 100644 index 00000000..250a307a --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/Post/PostForm/PostForm.js @@ -0,0 +1,71 @@ +import { + Form, + FormError, + FieldError, + Label, + TextField, + Submit, +} from '@redwoodjs/forms' + +const PostForm = (props) => { + const onSubmit = (data) => { + props.onSave(data, props?.post?.id) + } + + return ( +
+
+ + + + + + + + + + + + + + +
+ + Save + +
+ +
+ ) +} + +export default PostForm diff --git a/2022-12-01-service-caching/web/src/components/Post/Posts/Posts.js b/2022-12-01-service-caching/web/src/components/Post/Posts/Posts.js new file mode 100644 index 00000000..a99b3840 --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/Post/Posts/Posts.js @@ -0,0 +1,92 @@ +import { Link, routes } from '@redwoodjs/router' +import { useMutation } from '@redwoodjs/web' +import { toast } from '@redwoodjs/web/toast' + +import { QUERY } from 'src/components/Post/PostsCell' +import { timeTag, truncate } from 'src/lib/formatters' + +const DELETE_POST_MUTATION = gql` + mutation DeletePostMutation($id: Int!) { + deletePost(id: $id) { + id + } + } +` + +const PostsList = ({ posts }) => { + const [deletePost] = useMutation(DELETE_POST_MUTATION, { + onCompleted: () => { + toast.success('Post deleted') + }, + onError: (error) => { + toast.error(error.message) + }, + // This refetches the query on the list page. Read more about other ways to + // update the cache over here: + // https://www.apollographql.com/docs/react/data/mutations/#making-all-other-cache-updates + refetchQueries: [{ query: QUERY }], + awaitRefetchQueries: true, + }) + + const onDeleteClick = (id) => { + if (confirm('Are you sure you want to delete post ' + id + '?')) { + deletePost({ variables: { id } }) + } + } + + return ( +
+ + + + + + + + + + + + + {posts.map((post) => ( + + + + + + + + + ))} + +
IdTitleBodyCreated atUpdated at 
{truncate(post.id)}{truncate(post.title)}{truncate(post.body)}{timeTag(post.createdAt)}{timeTag(post.updatedAt)} + +
+
+ ) +} + +export default PostsList diff --git a/2022-12-01-service-caching/web/src/components/Post/PostsCell/PostsCell.js b/2022-12-01-service-caching/web/src/components/Post/PostsCell/PostsCell.js new file mode 100644 index 00000000..a649714d --- /dev/null +++ b/2022-12-01-service-caching/web/src/components/Post/PostsCell/PostsCell.js @@ -0,0 +1,36 @@ +import { Link, routes } from '@redwoodjs/router' + +import Posts from 'src/components/Post/Posts' + +export const QUERY = gql` + query FindPosts { + posts { + id + title + body + createdAt + updatedAt + } + } +` + +export const Loading = () =>
Loading...
+ +export const Empty = () => { + return ( +
+ {'No posts yet. '} + + {'Create one?'} + +
+ ) +} + +export const Failure = ({ error }) => ( +
{error?.message}
+) + +export const Success = ({ posts }) => { + return +} diff --git a/2022-12-01-service-caching/web/src/index.css b/2022-12-01-service-caching/web/src/index.css new file mode 100644 index 00000000..e69de29b diff --git a/2022-12-01-service-caching/web/src/index.html b/2022-12-01-service-caching/web/src/index.html new file mode 100644 index 00000000..18108094 --- /dev/null +++ b/2022-12-01-service-caching/web/src/index.html @@ -0,0 +1,17 @@ + + + + + + + + + + +
+ + <%= prerenderPlaceholder %> +
+ + + diff --git a/2022-12-01-service-caching/web/src/layouts/.keep b/2022-12-01-service-caching/web/src/layouts/.keep new file mode 100644 index 00000000..e69de29b diff --git a/2022-12-01-service-caching/web/src/layouts/ScaffoldLayout/ScaffoldLayout.js b/2022-12-01-service-caching/web/src/layouts/ScaffoldLayout/ScaffoldLayout.js new file mode 100644 index 00000000..fc2fd851 --- /dev/null +++ b/2022-12-01-service-caching/web/src/layouts/ScaffoldLayout/ScaffoldLayout.js @@ -0,0 +1,29 @@ +import { Link, routes } from '@redwoodjs/router' +import { Toaster } from '@redwoodjs/web/toast' + +const ScaffoldLayout = ({ + title, + titleTo, + buttonLabel, + buttonTo, + children, +}) => { + return ( +
+ +
+

+ + {title} + +

+ +
+
{buttonLabel} + +
+
{children}
+
+ ) +} + +export default ScaffoldLayout diff --git a/2022-12-01-service-caching/web/src/lib/formatters.js b/2022-12-01-service-caching/web/src/lib/formatters.js new file mode 100644 index 00000000..cb4051f3 --- /dev/null +++ b/2022-12-01-service-caching/web/src/lib/formatters.js @@ -0,0 +1,58 @@ +import React from 'react' + +import humanize from 'humanize-string' + +const MAX_STRING_LENGTH = 150 + +export const formatEnum = (values) => { + let output = '' + + if (Array.isArray(values)) { + const humanizedValues = values.map((value) => humanize(value)) + output = humanizedValues.join(', ') + } else if (typeof values === 'string') { + output = humanize(values) + } + + return output +} + +export const jsonDisplay = (obj) => { + return ( +
+      {JSON.stringify(obj, null, 2)}
+    
+ ) +} + +export const truncate = (value) => { + let output = value?.toString() ?? '' + + if (output.length > MAX_STRING_LENGTH) { + output = output.substring(0, MAX_STRING_LENGTH) + '...' + } + + return output +} + +export const jsonTruncate = (obj) => { + return truncate(JSON.stringify(obj, null, 2)) +} + +export const timeTag = (dateTime) => { + let output = '' + + if (dateTime) { + output = ( + + ) + } + + return output +} + +export const checkboxInputTag = (checked) => { + return +} diff --git a/2022-12-01-service-caching/web/src/lib/formatters.test.js b/2022-12-01-service-caching/web/src/lib/formatters.test.js new file mode 100644 index 00000000..56593386 --- /dev/null +++ b/2022-12-01-service-caching/web/src/lib/formatters.test.js @@ -0,0 +1,192 @@ +import { render, waitFor, screen } from '@redwoodjs/testing/web' + +import { + formatEnum, + jsonTruncate, + truncate, + timeTag, + jsonDisplay, + checkboxInputTag, +} from './formatters' + +describe('formatEnum', () => { + it('handles nullish values', () => { + expect(formatEnum(null)).toEqual('') + expect(formatEnum('')).toEqual('') + expect(formatEnum(undefined)).toEqual('') + }) + + it('formats a list of values', () => { + expect( + formatEnum(['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE', 'VIOLET']) + ).toEqual('Red, Orange, Yellow, Green, Blue, Violet') + }) + + it('formats a single value', () => { + expect(formatEnum('DARK_BLUE')).toEqual('Dark blue') + }) + + it('returns an empty string for values of the wrong type (for JS projects)', () => { + // @ts-expect-error - Testing JS scenario + expect(formatEnum(5)).toEqual('') + }) +}) + +describe('truncate', () => { + it('truncates really long strings', () => { + expect(truncate('na '.repeat(1000) + 'batman').length).toBeLessThan(1000) + expect(truncate('na '.repeat(1000) + 'batman')).not.toMatch(/batman/) + }) + + it('does not modify short strings', () => { + expect(truncate('Short strinG')).toEqual('Short strinG') + }) + + it('adds ... to the end of truncated strings', () => { + expect(truncate('repeat'.repeat(1000))).toMatch(/\w\.\.\.$/) + }) + + it('accepts numbers', () => { + expect(truncate(123)).toEqual('123') + expect(truncate(0)).toEqual('0') + expect(truncate(0o000)).toEqual('0') + }) + + it('handles arguments of invalid type', () => { + // @ts-expect-error - Testing JS scenario + expect(truncate(false)).toEqual('false') + + expect(truncate(undefined)).toEqual('') + expect(truncate(null)).toEqual('') + }) +}) + +describe('jsonTruncate', () => { + it('truncates large json structures', () => { + expect( + jsonTruncate({ + foo: 'foo', + bar: 'bar', + baz: 'baz', + kittens: 'kittens meow', + bazinga: 'Sheldon', + nested: { + foobar: 'I have no imagination', + two: 'Second nested item', + }, + five: 5, + bool: false, + }) + ).toMatch(/.+\n.+\w\.\.\.$/s) + }) +}) + +describe('timeTag', () => { + it('renders a date', async () => { + render(
{timeTag(new Date('1970-08-20').toUTCString())}
) + + await waitFor(() => screen.getByText(/1970.*00:00:00/)) + }) + + it('can take an empty input string', async () => { + expect(timeTag('')).toEqual('') + }) +}) + +describe('jsonDisplay', () => { + it('produces the correct output', () => { + expect( + jsonDisplay({ + title: 'TOML Example (but in JSON)', + database: { + data: [['delta', 'phi'], [3.14]], + enabled: true, + ports: [8000, 8001, 8002], + temp_targets: { + case: 72.0, + cpu: 79.5, + }, + }, + owner: { + dob: '1979-05-27T07:32:00-08:00', + name: 'Tom Preston-Werner', + }, + servers: { + alpha: { + ip: '10.0.0.1', + role: 'frontend', + }, + beta: { + ip: '10.0.0.2', + role: 'backend', + }, + }, + }) + ).toMatchInlineSnapshot(` +
+        
+          {
+        "title": "TOML Example (but in JSON)",
+        "database": {
+          "data": [
+            [
+              "delta",
+              "phi"
+            ],
+            [
+              3.14
+            ]
+          ],
+          "enabled": true,
+          "ports": [
+            8000,
+            8001,
+            8002
+          ],
+          "temp_targets": {
+            "case": 72,
+            "cpu": 79.5
+          }
+        },
+        "owner": {
+          "dob": "1979-05-27T07:32:00-08:00",
+          "name": "Tom Preston-Werner"
+        },
+        "servers": {
+          "alpha": {
+            "ip": "10.0.0.1",
+            "role": "frontend"
+          },
+          "beta": {
+            "ip": "10.0.0.2",
+            "role": "backend"
+          }
+        }
+      }
+        
+      
+ `) + }) +}) + +describe('checkboxInputTag', () => { + it('can be checked', () => { + render(checkboxInputTag(true)) + expect(screen.getByRole('checkbox')).toBeChecked() + }) + + it('can be unchecked', () => { + render(checkboxInputTag(false)) + expect(screen.getByRole('checkbox')).not.toBeChecked() + }) + + it('is disabled when checked', () => { + render(checkboxInputTag(true)) + expect(screen.getByRole('checkbox')).toBeDisabled() + }) + + it('is disabled when unchecked', () => { + render(checkboxInputTag(false)) + expect(screen.getByRole('checkbox')).toBeDisabled() + }) +}) diff --git a/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.js b/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.js new file mode 100644 index 00000000..5672158e --- /dev/null +++ b/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.js @@ -0,0 +1,19 @@ +import { Link, routes } from '@redwoodjs/router' +import { MetaTags } from '@redwoodjs/web' + +import ArticleCell from 'src/components/ArticleCell' + +const ArticlePage = ({ id }) => { + return ( + <> + + +

+ Rob's Blog +

+ + + ) +} + +export default ArticlePage diff --git a/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.stories.js b/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.stories.js new file mode 100644 index 00000000..44244254 --- /dev/null +++ b/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.stories.js @@ -0,0 +1,10 @@ +import ArticlePage from './ArticlePage' + +export const generated = () => { + return +} + +export default { + title: 'Pages/ArticlePage', + component: ArticlePage, +} diff --git a/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.test.js b/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.test.js new file mode 100644 index 00000000..583138dd --- /dev/null +++ b/2022-12-01-service-caching/web/src/pages/ArticlePage/ArticlePage.test.js @@ -0,0 +1,14 @@ +import { render } from '@redwoodjs/testing/web' + +import ArticlePage from './ArticlePage' + +// Improve this test with help from the Redwood Testing Doc: +// https://redwoodjs.com/docs/testing#testing-pages-layouts + +describe('ArticlePage', () => { + it('renders successfully', () => { + expect(() => { + render() + }).not.toThrow() + }) +}) diff --git a/2022-12-01-service-caching/web/src/pages/FatalErrorPage/FatalErrorPage.js b/2022-12-01-service-caching/web/src/pages/FatalErrorPage/FatalErrorPage.js new file mode 100644 index 00000000..7ff31af1 --- /dev/null +++ b/2022-12-01-service-caching/web/src/pages/FatalErrorPage/FatalErrorPage.js @@ -0,0 +1,62 @@ +// This page will be rendered when an error makes it all the way to the top of the +// application without being handled by a Javascript catch statement or React error +// boundary. +// +// You can modify this page as you wish, but it is important to keep things simple to +// avoid the possibility that it will cause its own error. If it does, Redwood will +// still render a generic error page, but your users will prefer something a bit more +// thoughtful. =) + +// Ensures that production builds do not include the error page +let RedwoodDevFatalErrorPage = undefined +if (process.env.NODE_ENV === 'development') { + RedwoodDevFatalErrorPage = + require('@redwoodjs/web/dist/components/DevFatalErrorPage').DevFatalErrorPage +} + +export default RedwoodDevFatalErrorPage || + (() => ( +
+