@platformatic/sql-graphql 0.45.0 → 0.46.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -5,6 +5,7 @@ const constructGraph = require('./lib/entity-to-type')
5
5
  const mercurius = require('mercurius')
6
6
  const graphql = require('graphql')
7
7
  const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
8
+ const { findNearestString } = require('@platformatic/utils')
8
9
  const establishRelations = require('./lib/relationship')
9
10
  const setupSubscriptions = require('./lib/subscriptions')
10
11
  const scalars = require('graphql-scalars')
@@ -37,12 +38,27 @@ async function mapperToGraphql (app, opts) {
37
38
  hello: () => 'Hello Platformatic!'
38
39
  }
39
40
  } else {
41
+ const entitiesNames = Object.values(mapper.entities)
42
+ .map(entity => entity.singularName)
43
+
44
+ for (const ignoredEntity of Object.keys(ignore)) {
45
+ if (!entitiesNames.includes(ignoredEntity)) {
46
+ const nearestEntity = findNearestString(entitiesNames, ignoredEntity)
47
+ let warningMessage = `Ignored graphql entity "${ignoredEntity}" not found.`
48
+ /* istanbul ignore next */
49
+ if (nearestEntity) {
50
+ warningMessage += ` Did you mean "${nearestEntity}"?`
51
+ }
52
+ app.log.warn(warningMessage)
53
+ }
54
+ }
55
+
40
56
  for (const entity of Object.values(mapper.entities)) {
41
- if (ignore[entity.pluralName] === true) {
57
+ if (ignore[entity.singularName] === true) {
42
58
  continue
43
59
  }
44
60
  relations.push(...entity.relations)
45
- const meta = constructGraph(app, entity, graphOpts, ignore[entity.pluralName] || {})
61
+ const meta = constructGraph(app, entity, graphOpts, ignore[entity.singularName] || {})
46
62
  metaMap.set(entity, meta)
47
63
  }
48
64
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  const graphql = require('graphql')
4
4
  const camelcase = require('camelcase')
5
+ const { findNearestString } = require('@platformatic/utils')
5
6
  const {
6
7
  sqlTypeToGraphQL,
7
8
  fromSelectionSet
@@ -37,6 +38,19 @@ function constructGraph (app, entity, opts, ignore) {
37
38
  loaders
38
39
  } = opts
39
40
 
41
+ const entityFieldsNames = Object.values(entity.fields)
42
+ .map(field => field.camelcase)
43
+
44
+ for (const ignoredField of Object.keys(ignore)) {
45
+ if (!entityFieldsNames.includes(ignoredField)) {
46
+ const nearestField = findNearestString(entityFieldsNames, ignoredField)
47
+ app.log.warn(
48
+ `Ignored graphql field "${ignoredField}" not found in entity "${entity.singularName}".` +
49
+ ` Did you mean "${nearestField}"?`
50
+ )
51
+ }
52
+ }
53
+
40
54
  const fields = {}
41
55
 
42
56
  for (const key of Object.keys(entity.fields)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-graphql",
3
- "version": "0.45.0",
3
+ "version": "0.46.1",
4
4
  "description": "Map SQL dbs to GraphQL",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -22,8 +22,8 @@
22
22
  "tap": "^16.3.6",
23
23
  "tsd": "^0.29.0",
24
24
  "ws": "^8.13.0",
25
- "@platformatic/sql-events": "0.45.0",
26
- "@platformatic/sql-mapper": "0.45.0"
25
+ "@platformatic/sql-events": "0.46.1",
26
+ "@platformatic/sql-mapper": "0.46.1"
27
27
  },
28
28
  "dependencies": {
29
29
  "@fastify/error": "^3.2.1",
@@ -34,7 +34,8 @@
34
34
  "graphql-scalars": "^1.22.2",
35
35
  "graphql-type-json": "^0.3.2",
36
36
  "inflected": "^2.1.0",
37
- "mercurius": "^13.0.0"
37
+ "mercurius": "^13.0.0",
38
+ "@platformatic/utils": "0.46.1"
38
39
  },
39
40
  "tsd": {
40
41
  "directory": "test/types"
@@ -129,7 +129,7 @@ test('ignore a table via sql-graphql option', async ({ pass, teardown, equal })
129
129
  })
130
130
  app.register(sqlGraphQL, {
131
131
  ignore: {
132
- categories: true
132
+ category: true
133
133
  }
134
134
  })
135
135
  teardown(app.close.bind(app))
@@ -158,6 +158,46 @@ test('ignore a table via sql-graphql option', async ({ pass, teardown, equal })
158
158
  }
159
159
  })
160
160
 
161
+ test('show a warning if there is no ignored entity', async ({ plan, pass, teardown }) => {
162
+ plan(2)
163
+
164
+ const app = fastify({
165
+ logger: {
166
+ info () {},
167
+ debug () {},
168
+ trace () {},
169
+ fatal () {},
170
+ error () {},
171
+ child () {
172
+ return this
173
+ },
174
+ warn (msg) {
175
+ if (msg === 'Ignored graphql entity "missingEntityPages" not found. Did you mean "page"?') {
176
+ pass('warning message is shown')
177
+ }
178
+ }
179
+ }
180
+ })
181
+
182
+ app.register(sqlMapper, {
183
+ ...connInfo,
184
+ async onDatabaseLoad (db, sql) {
185
+ pass('onDatabaseLoad called')
186
+
187
+ await clear(db, sql)
188
+ await createBasicPages(db, sql)
189
+ }
190
+ })
191
+ app.register(sqlGraphQL, {
192
+ ignore: {
193
+ missingEntityPages: true
194
+ }
195
+ })
196
+ teardown(app.close.bind(app))
197
+
198
+ await app.ready()
199
+ })
200
+
161
201
  test('ignore a column via sql-graphql option', async ({ pass, teardown, equal }) => {
162
202
  const app = fastify()
163
203
  app.register(sqlMapper, {
@@ -171,7 +211,7 @@ test('ignore a column via sql-graphql option', async ({ pass, teardown, equal })
171
211
  })
172
212
  app.register(sqlGraphQL, {
173
213
  ignore: {
174
- categories: {
214
+ category: {
175
215
  name: true
176
216
  }
177
217
  }
@@ -205,3 +245,45 @@ test('ignore a column via sql-graphql option', async ({ pass, teardown, equal })
205
245
  equal(Category.fields.find((f) => f.name === 'name'), undefined, 'name column is ignored')
206
246
  }
207
247
  })
248
+
249
+ test('show a warning if there is no ignored entity field', async ({ plan, pass, teardown }) => {
250
+ plan(2)
251
+
252
+ const app = fastify({
253
+ logger: {
254
+ info () {},
255
+ debug () {},
256
+ trace () {},
257
+ fatal () {},
258
+ error () {},
259
+ child () {
260
+ return this
261
+ },
262
+ warn (msg) {
263
+ if (msg === 'Ignored graphql field "missingFieldName" not found in entity "category". Did you mean "name"?') {
264
+ pass('warning message is shown')
265
+ }
266
+ }
267
+ }
268
+ })
269
+
270
+ app.register(sqlMapper, {
271
+ ...connInfo,
272
+ async onDatabaseLoad (db, sql) {
273
+ pass('onDatabaseLoad called')
274
+
275
+ await clear(db, sql)
276
+ await createBasicPages(db, sql)
277
+ }
278
+ })
279
+ app.register(sqlGraphQL, {
280
+ ignore: {
281
+ category: {
282
+ missingFieldName: true
283
+ }
284
+ }
285
+ })
286
+ teardown(app.close.bind(app))
287
+
288
+ await app.ready()
289
+ })