Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[THU-41]: new directive @log #29

Merged
merged 14 commits into from
Apr 30, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
bundle
*.log
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Simple utility library for custom GraphQL schema directives
- [@cache](#cache)
- [Overriding in-memory cache](#overriding-in-memory-cache)
- [@currency](#currency)
- [@log](#log)
- [Logging to file](#logging-to-file)

# Get started

Expand Down Expand Up @@ -222,3 +224,45 @@ type Car {
The field can either be resolved with scalar types `String` or `Float`

The valid currency codes to use as part of the directive's arguments can be found [here](./src/types.ts).

## @log

`logDirective({ directiveName, filePath }?: { directiveName?: string, filePath?: string })`

Use the `@log` directive to log fields, queries and mutations once they are resolved.

For example, this graphql schema with the directive on the query:

```graphql
type User {
firstName: String
lastName: String
age: Int
amount: String
}

type Query {
user(firstName: String!): User @log(level: INFO)
}
```

Will log to the console in the following format:

`[<TIMESTAMP>] [INFO] @log - Operation Type: query, Arguments: [{"firstName":"Eddie"}], Return Type: User`

The following log levels are valid:

- `INFO`
- `DEBUG`
- `WARN`
- `ERROR`

### Logging to file

In order to migrate logs to a custom log file, you can define a filepath with the appropriate file name:

```typescript
const { logDirectiveTypeDefs, logDirectiveTransformer } = logDirective({
filePath: path.join(__dirname, 'logs', 'application.log')
})
```
150 changes: 135 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@
"build:server": "tsc --project tsconfig.server.json",
"dev": "nodemon --project tsconfig.server.json server/index.ts",
"lint": "eslint .",
"test": "jest",
"test": "jest --silent",
"test:watch": "npm run test -- --watch test/*.ts",
"prepare": "npm run build:prod",
"release": "semantic-release"
},
"dependencies": {
"@graphql-tools/schema": "^10.0.2",
"@graphql-tools/utils": "^10.0.13",
"cheerio": "^1.0.0-rc.12",
"graphql": "^16.8.1"
"graphql": "^16.8.1",
"log4js": "^6.9.1"
},
"devDependencies": {
"@apollo/server": "^4.10.0",
Expand Down
Loading
Loading