Skip to content

Commit

Permalink
fix: errorFormatter relative to project root and improved merging o…
Browse files Browse the repository at this point in the history
…f protocol config (#129)

* Create a symlink to self to enable test projects to require `@cap-js/graphql`

* Revert "Create a symlink to self to enable test projects to require `@cap-js/graphql`"

This reverts commit 60d7d0f.

* Add dev dependency to self

* Re-add self requires in tests

* Configure protocol path in test project `package.json`

* Improve merging of protocol configuration with plugin defaults

* Replace more programmatic with declarative configs

* Load custom `errorFormatter` relative to CDS project root

* Single quotes instead of double quotes

* Remove `impl` from config

* Improve readability of loading error formatter

* Another declarative instead of programmatic config

* Simpler programmatic config that points to module name

* Reorder imports
  • Loading branch information
schwma authored Oct 13, 2023
1 parent ea4cb89 commit eecc08b
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 48 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Moved registration of `cds.compile.to.gql` and `cds.compile.to.graphql` targets from `@sap/cds` to `@cap-js/graphql`
- Improve merging of custom `graphql` protocol configuration with plugin default configuration

### Fixed

- Load custom `errorFormatter` relative to CDS project root

### Removed

## Version 0.8.0 - 2023-10-06
Expand Down
6 changes: 3 additions & 3 deletions cds-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const cds = require('@sap/cds')
require('./lib/api').registerCompileTargets()
const defaults = { path: '/graphql', impl: '@cap-js/graphql' }
const protocols = cds.env.protocols ??= {}
if (!protocols.graphql) protocols.graphql = {
path: "/graphql", impl: "@cap-js/graphql"
}
protocols.graphql ??= {}
protocols.graphql = { ...defaults, ...protocols.graphql}
9 changes: 7 additions & 2 deletions lib/GraphQLAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const express = require('express')
const { generateSchema4 } = require('./schema')
const path = require('path')
const cds = require('@sap/cds')
const defaultErrorFormatter = require('./errorFormatter')
const queryLogger = require('./logger')
const graphiql = require('../app/graphiql')
const { createHandler } = require('graphql-http/lib/use/express')
Expand All @@ -8,11 +11,13 @@ const { formatError } = require('./resolvers/error')
function GraphQLAdapter(options) {
const router = express.Router()
const { services } = options
const defaults = { graphiql: true, errorFormatter: './errorFormatter' }
const defaults = { graphiql: true }
const schema = generateSchema4(services)
options = { ...defaults, ...options }

const errorFormatter = require(options.errorFormatter)
const errorFormatter = options.errorFormatter
? require(path.join(cds.root, options.errorFormatter))
: defaultErrorFormatter

router
.use(express.json()) //> required by logger below
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@sap/cds": ">=7.3"
},
"devDependencies": {
"@cap-js/graphql": "file:.",
"axios": "^1",
"eslint": "^8",
"express": "^4.17.1",
Expand Down
12 changes: 12 additions & 0 deletions test/resources/annotations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"dependencies": {
"@cap-js/graphql": "*"
},
"cds": {
"protocols": {
"graphql": {
"path": "/custom-graphql-path"
}
}
}
}
6 changes: 0 additions & 6 deletions test/resources/annotations/server.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/resources/bookshop-graphql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"@cap-js/graphql": "../../.."
"@cap-js/graphql": "*"
},
"cds": {
"requires": {
Expand Down
6 changes: 0 additions & 6 deletions test/resources/bookshop-graphql/server.js

This file was deleted.

5 changes: 5 additions & 0 deletions test/resources/cds.Request/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@cap-js/graphql": "*"
}
}
6 changes: 0 additions & 6 deletions test/resources/cds.Request/server.js

This file was deleted.

5 changes: 5 additions & 0 deletions test/resources/concurrency/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@cap-js/graphql": "*"
}
}
6 changes: 0 additions & 6 deletions test/resources/concurrency/server.js

This file was deleted.

13 changes: 13 additions & 0 deletions test/resources/custom-error-formatter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"dependencies": {
"@cap-js/graphql": "*"
},
"cds": {
"protocols": {
"graphql": {
"path": "/graphql",
"errorFormatter": "customErrorFormatter"
}
}
}
}
8 changes: 0 additions & 8 deletions test/resources/custom-error-formatter/server.js

This file was deleted.

6 changes: 2 additions & 4 deletions test/resources/custom-handlers/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const cds = require('@sap/cds')
const path = require('path')
// Programmatic configuration of GraphQL protocol adapter
const protocols = cds.env.protocols ??= {}
if (!protocols.graphql) protocols.graphql = {
path: '/graphql', impl: path.join(__dirname, '../../../index.js')
}
protocols.graphql = { path: '/graphql', impl: '@cap-js/graphql' }
3 changes: 3 additions & 0 deletions test/resources/error-handling/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"dependencies": {
"@cap-js/graphql": "*"
},
"cds": {
"requires": {
"db": {
Expand Down
6 changes: 0 additions & 6 deletions test/resources/error-handling/server.js

This file was deleted.

0 comments on commit eecc08b

Please sign in to comment.