generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move registration of
graphql
and gql
cds.compile.to
targe…
…ts from `@sap/cds` (#125) * Move compile to gql/graphql to module from @sap/cds * Use `generateSchema4` instead of `SchemaGenerator` * Remove unused `SchemaGenerator` class * Extract `cds.compile.to.(gql|graphql)` registration * Prettier format * Reword comment * Simplify generate-schemas script * Fix module paths * WIP comment out require cli * Add option to compile to GraphQLSchema object * Add compile option to lexicographically sort schema * Fix registration of gql and graphql getters * `Object.defineProperty` -> `Object.defineProperties` * Rename `compile` imports to `cds_compile_to_gql` * Extract require to lazy function for readability * Remove default options destructuring from args * Rename `cli.js` to `api.js` * Re-add `SchemaGenerator` class for backwards compatibility * `options.object` -> `options.as: 'obj'` to align with `cds.compile.to.sql` * Default options to empty object * Fix `SchemaGenerator.generate()` chaining * Fix missing import * Ensure plugin is loaded to extend `compile` in bookshop-graphql project * Remove `formatSchema` util function * Make compile format regex case insensitive * Extract CSN to variable for better readability * Add changelog entry * Improve lazy loading and clarify purpose of api module * Shorten registration and avoid specifying properties twice * Rename lazy function * Use `.to.gql` and `.to.graphql` in tests * Use `cds.compile.to` in enrich test * Ensure compile target registration only happens once * Move `cds.compile.to` into register function * Registration in `GraphQLAdapter` is not necessary since the plugin should always be loaded * Add comment to explain why all compile targets are lazy loaded * Comment wording * Swap require const order * Clarify revisit comment
- Loading branch information
Showing
10 changed files
with
82 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const cds = require('@sap/cds') | ||
|
||
const TARGETS = ['gql', 'graphql'] | ||
|
||
function _lazyRegisterCompileTargets() { | ||
const value = require('./compile') | ||
// Lazy load all compile targets if any of them is accessed | ||
// For example .to.gql was called -> load .to.gql and .to.graphql | ||
TARGETS.forEach(target => Object.defineProperty(this, target, { value })) | ||
return value | ||
} | ||
|
||
// Register gql and graphql as cds.compile.to targets | ||
const registerCompileTargets = () => { | ||
TARGETS.forEach(target => | ||
Object.defineProperty(cds.compile.to, target, { | ||
get: _lazyRegisterCompileTargets, | ||
configurable: true | ||
}) | ||
) | ||
} | ||
|
||
module.exports = { registerCompileTargets } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const cds = require('@sap/cds') | ||
const { generateSchema4 } = require('./schema') | ||
const { lexicographicSortSchema, printSchema } = require('graphql') | ||
|
||
function cds_compile_to_gql(csn, options = {}) { | ||
const m = cds.linked(csn) | ||
const services = Object.fromEntries(m.services.map(s => [s.name, new cds.ApplicationService(s.name, m)])) | ||
|
||
let schema = generateSchema4(services) | ||
|
||
if (options.sort) schema = lexicographicSortSchema(schema) | ||
if (/^obj|object$/i.test(options.as)) return schema | ||
|
||
return printSchema(schema) | ||
} | ||
|
||
module.exports = cds_compile_to_gql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"@cap-js/graphql": "../../.." | ||
}, | ||
"cds": { | ||
"requires": { | ||
"db": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters