@platformatic/sql-graphql 0.43.1 → 0.45.0

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.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { FastifyPluginAsync } from 'fastify'
2
2
  import { GraphQLScalarType } from 'graphql'
3
3
  import { IResolverObject, IResolverOptions, MercuriusContext } from 'mercurius'
4
+ import { FastifyError } from '@fastify/error'
4
5
 
5
6
  type IEnumResolver = {
6
7
  [key: string]: string | number | boolean
@@ -8,12 +9,12 @@ type IEnumResolver = {
8
9
 
9
10
  export interface IResolvers<TSource = any, TContext = MercuriusContext> {
10
11
  [key: string]:
11
- | (() => any)
12
- | IResolverObject<TSource, TContext>
13
- | IResolverOptions<TSource, TContext>
14
- | GraphQLScalarType
15
- | IEnumResolver
16
- | undefined
12
+ | (() => any)
13
+ | IResolverObject<TSource, TContext>
14
+ | IResolverOptions<TSource, TContext>
15
+ | GraphQLScalarType
16
+ | IEnumResolver
17
+ | undefined
17
18
  }
18
19
 
19
20
  export interface SQLGraphQLPluginOptions {
@@ -25,9 +26,9 @@ export interface SQLGraphQLPluginOptions {
25
26
  * Parameter that enables federation metadata support.
26
27
  */
27
28
  federationMetadata?: boolean,
28
- /**
29
- * Object with graphql resolver functions.
30
- */
29
+ /**
30
+ * Object with graphql resolver functions.
31
+ */
31
32
  resolvers?: IResolvers,
32
33
  /*
33
34
  * The graphql schema.
@@ -37,3 +38,13 @@ export interface SQLGraphQLPluginOptions {
37
38
 
38
39
  declare const plugin: FastifyPluginAsync<SQLGraphQLPluginOptions>
39
40
  export default plugin
41
+
42
+ /**
43
+ * All the errors thrown by the plugin.
44
+ */
45
+ export module errors {
46
+ export const UnableToGenerateGraphQLEnumTypeError: () => FastifyError
47
+ export const UnsupportedKindError: (kind: string) => FastifyError
48
+ export const ErrorPrintingGraphQLSchema: () => FastifyError
49
+ }
50
+
package/index.js CHANGED
@@ -8,6 +8,7 @@ const { mercuriusFederationPlugin } = require('@mercuriusjs/federation')
8
8
  const establishRelations = require('./lib/relationship')
9
9
  const setupSubscriptions = require('./lib/subscriptions')
10
10
  const scalars = require('graphql-scalars')
11
+ const errors = require('./lib/errors')
11
12
 
12
13
  async function mapperToGraphql (app, opts) {
13
14
  const mapper = app.platformatic
@@ -128,7 +129,7 @@ async function mapperToGraphql (app, opts) {
128
129
  /* istanbul ignore next */
129
130
  app.log.debug({ query, mutation, subscription }, 'GraphQL input schema')
130
131
  /* istanbul ignore next */
131
- const newError = new Error('Error printing the GraphQL schema')
132
+ const newError = new errors.ErrorPrintingGraphQLSchema()
132
133
  /* istanbul ignore next */
133
134
  newError.cause = error
134
135
  /* istanbul ignore next */
@@ -173,3 +174,4 @@ async function mapperToGraphql (app, opts) {
173
174
  }
174
175
 
175
176
  module.exports = fp(mapperToGraphql)
177
+ module.exports.errors = errors
@@ -6,6 +6,7 @@ const {
6
6
  sqlTypeToGraphQL,
7
7
  fromSelectionSet
8
8
  } = require('./utils')
9
+ const errors = require('./errors')
9
10
 
10
11
  const ascDesc = new graphql.GraphQLEnumType({
11
12
  name: 'OrderByDirection',
@@ -73,7 +74,7 @@ function constructGraph (app, entity, opts, ignore) {
73
74
  meta.type = new graphql.GraphQLEnumType({ name, values: enumValues })
74
75
  } catch (error) {
75
76
  app.log.error({ key, enumValues, entityName, table: entity.table, schema: entity.schema })
76
- throw new Error('Unable to generate GraphQLEnumType')
77
+ throw new errors.UnableToGenerateGraphQLEnumTypeError()
77
78
  }
78
79
  } else {
79
80
  meta.type = sqlTypeToGraphQL(field.sqlType)
package/lib/errors.js ADDED
@@ -0,0 +1,11 @@
1
+ 'use strict'
2
+
3
+ const createError = require('@fastify/error')
4
+
5
+ const ERROR_PREFIX = 'PLT_SQL_GRAPHQL'
6
+
7
+ module.exports = {
8
+ UnableToGenerateGraphQLEnumTypeError: createError(`${ERROR_PREFIX}_UNABLE_GENERATE_GRAPHQL_ENUM_TYPE`, 'Unable to generate GraphQLEnumType'),
9
+ UnsupportedKindError: createError(`${ERROR_PREFIX}_UNSUPPORTED_KIND`, 'Unsupported kind: %s'),
10
+ ErrorPrintingGraphQLSchema: createError(`${ERROR_PREFIX}_ERROR_PRINTING_GRAPHQL_SCHEMA`, 'Error printing the GraphQL schema')
11
+ }
package/lib/utils.js CHANGED
@@ -3,6 +3,7 @@
3
3
  const graphql = require('graphql')
4
4
  const scalars = require('graphql-scalars')
5
5
  const { GraphQLJSONObject } = require('graphql-type-json')
6
+ const errors = require('./errors')
6
7
 
7
8
  // The sqlTypeToGraphQL is shared between
8
9
  // all database adapters.
@@ -71,7 +72,7 @@ function fromSelectionSet (selectionSet, fields = new Set()) {
71
72
  } else if (s.kind === 'InlineFragment') {
72
73
  fromSelectionSet(s.selectionSet, fields)
73
74
  } else {
74
- throw new Error('Unsupported kind: ' + s.kind)
75
+ throw new errors.UnsupportedKindError(s.kind)
75
76
  }
76
77
  }
77
78
  return fields
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-graphql",
3
- "version": "0.43.1",
3
+ "version": "0.45.0",
4
4
  "description": "Map SQL dbs to GraphQL",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -22,10 +22,11 @@
22
22
  "tap": "^16.3.6",
23
23
  "tsd": "^0.29.0",
24
24
  "ws": "^8.13.0",
25
- "@platformatic/sql-events": "0.43.1",
26
- "@platformatic/sql-mapper": "0.43.1"
25
+ "@platformatic/sql-events": "0.45.0",
26
+ "@platformatic/sql-mapper": "0.45.0"
27
27
  },
28
28
  "dependencies": {
29
+ "@fastify/error": "^3.2.1",
29
30
  "@mercuriusjs/federation": "^2.0.0",
30
31
  "camelcase": "^6.3.0",
31
32
  "fastify-plugin": "^4.5.0",
@@ -0,0 +1,15 @@
1
+ import { expectType } from 'tsd'
2
+ import { FastifyError } from '@fastify/error'
3
+ import {
4
+ errors
5
+ } from '../../index'
6
+
7
+ // Errors
8
+ type ErrorWithNoParams = () => FastifyError
9
+ type ErrorWithOneParam = (param: string) => FastifyError
10
+
11
+ expectType<ErrorWithNoParams>(errors.UnableToGenerateGraphQLEnumTypeError)
12
+ expectType<ErrorWithOneParam>(errors.UnsupportedKindError)
13
+ expectType<ErrorWithNoParams>(errors.ErrorPrintingGraphQLSchema)
14
+
15
+