From 6bc29303b69f07a329e0c192dea6be8e1b6d32e5 Mon Sep 17 00:00:00 2001 From: Salman Dabbakuti <29351207+Salmandabbakuti@users.noreply.github.com> Date: Fri, 10 Jan 2025 11:08:58 +0530 Subject: [PATCH] Initial commit --- .gitignore | 22 ++++++++++++++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 31 +++++++++++++++++++++++++++++++ index.ts | 36 ++++++++++++++++++++++++++++++++++++ package.json | 24 ++++++++++++++++++++++++ tsconfig.json | 19 +++++++++++++++++++ 6 files changed, 153 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.ts create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..166c887 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Build and Release Folders +bin-debug/ +bin-release/ +[Oo]bj/ +[Bb]in/ + +# Other files and folders +.settings/ +node_modules/ +dist/ +package-lock.json + +# Executables +*.swf +*.air +*.ipa +*.apk +*.env + +# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties` +# should NOT be excluded as they contain compiler settings and other important +# information for Eclipse / Flash Builder. \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3dc35db --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Salman Dabbakuti + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8708077 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# graphql-yoga-cookies + +This project demonstrates how to set and get cookies in a GraphQL server using [graphql-yoga](https://the-guild.dev/graphql/yoga-server) + +## Getting Started + +```bash + +# Install dependencies +npm install + +# Start server +npm start + +``` + +### Sample Queries + +```graphql +mutation SetCookie { + setCookie(name: "session", value: "eyjh.....") +} + +query GetCookie { + cookie(name: "session") +} +``` + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..7fc87a2 --- /dev/null +++ b/index.ts @@ -0,0 +1,36 @@ +import { createServer } from "node:http"; +import { createSchema, createYoga, YogaInitialContext } from "graphql-yoga"; +import { useCookies } from "@whatwg-node/server-plugin-cookies"; + +const yoga = createYoga({ + schema: createSchema({ + typeDefs: /* GraphQL */ ` + type Query { + cookie(name: String!): String + } + type Mutation { + setCookie(name: String!, value: String!): String + } + `, + resolvers: { + Query: { + async cookie(root, args, ctx: YogaInitialContext) { + const cookie = await ctx.request.cookieStore?.get(args.name); + return cookie?.value; + } + }, + Mutation: { + async setCookie(root, args, ctx: YogaInitialContext) { + await ctx.request.cookieStore?.set(args.name, args.value); + return args.value; + } + } + } + }), + plugins: [useCookies()] +}); + +const server = createServer(yoga); +server.listen(4000, () => { + console.log("Server is running on http://localhost:4000"); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..d5e3097 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "graphql-yoga-cookies", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "tsc", + "dev": "tsx watch index.ts", + "start": "node dist/index.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "@whatwg-node/server-plugin-cookies": "^1.0.3", + "graphql": "^16.10.0", + "graphql-yoga": "^5.10.9" + }, + "devDependencies": { + "@types/node": "^22.10.5", + "tsx": "^4.19.2" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..88a33b3 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + "module": "commonjs" /* Specify what module code is generated. */, + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + "strict": true /* Enable all strict type-checking options. */, + "noUnusedLocals": true, + "removeComments": true, + "noUnusedParameters": false, + "strictNullChecks": false, + "strictFunctionTypes": true, + "allowJs": true, + "checkJs": true, + "skipLibCheck": true /* Skip type checking all .d.ts files. */, + "rootDir": "./", + "outDir": "dist" + } +}