@nerjs/gql 1.5.0 → 2.0.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/README.md CHANGED
@@ -1,220 +1,3 @@
1
- # Gql (apollo) config and utils
1
+ # The utility is incomplete and is no longer supported.
2
2
 
3
- ## Install
4
-
5
- ```
6
- npm i @nerjs/gql
7
- ```
8
- or:
9
- ```
10
- yarn add @nerjs/gql
11
- ```
12
-
13
-
14
- ## Use
15
-
16
-
17
- ### createClient()
18
-
19
- create [Apollo client](https://www.apollographql.com/docs/react/get-started/#create-a-client).
20
-
21
- ```js
22
- const createClient = require('@nerjs/gql/client')
23
- // or
24
- import createClient from '@nerjs/gql/client'
25
-
26
-
27
- const client = createClient({/* ...clientOptions */})
28
- ```
29
-
30
- #### clientOptions
31
-
32
- |prop name|type|required|description|
33
- |:--:|:--:|:--:|:--|
34
- |***uri***|**String**|:white_check_mark:|Graphql server endpoint. Used in [HttpLink](https://www.apollographql.com/docs/link/links/batch-http/)|
35
- |***httpOptions***|**Object**|| Other options [HttpLink](https://www.apollographql.com/docs/link/links/batch-http/#options)|
36
- |***wsUri***|**String**||Graphql server websocket endpoint. Used in [WebSocketLink](https://www.apollographql.com/docs/link/links/ws/)|
37
- |***wsOptions***|**Object**|| Other otions [WebSocketLink](https://www.apollographql.com/docs/link/links/ws/#options)|
38
- |***links***|**Array**|| Array of [ApolloLinks](https://www.apollographql.com/docs/link/)|
39
- |***onError***|**Function**||errorHandler for [apollo-link-error](https://www.apollographql.com/docs/link/links/error/)|
40
-
41
- ### createGqlServer
42
-
43
- create [ApolloServer](https://www.apollographql.com/docs/apollo-server/api/apollo-server/)
44
-
45
- ```js
46
- const createGqlServer = require('@nerjs/gql/server')
47
-
48
- const server = createGqlServer({/* ...serverOptions */})
49
- ```
50
-
51
- #### serverOptions
52
-
53
- |prop name|type|required|description|
54
- |:--:|:--:|:--:|:--|
55
- |***app***|**[Application](https://expressjs.com/ru/4x/api.html#app)**|:white_check_mark:|Express application|
56
- |***path***|**String**|:white_check_mark:|Graphql [uri endpoint](https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-express)|
57
- |***playground***|**Boolean**||Enable graphql [playground](https://www.apollographql.com/docs/apollo-server/testing/graphql-playground/#gatsby-focus-wrapper)|
58
- |***types***|**String**|:white_check_mark:|Path to graphql [types](https://github.com/Urigo/merge-graphql-schemas#merging-type-definitions). Used by [merge-graphql-schemas](https://github.com/Urigo/merge-graphql-schemas)|
59
- |***resolvers***|**String**|:white_check_mark:|Path to graphql [resolvers](https://github.com/Urigo/merge-graphql-schemas#merging-resolvers). Used by [merge-graphql-schemas](https://github.com/Urigo/merge-graphql-schemas)|
60
- |***middlewares***|**Array(Function \| Object)**||[graphql-middleware](https://github.com/prisma-labs/graphql-middleware)|
61
- |***cors***|||
62
- |***formatError***|||
63
- |***context***|||
64
- |***subscriptions***|||
65
-
66
-
67
-
68
-
69
- ### GqlProvider
70
-
71
- [React component](https://en.reactjs.org/docs/react-component.html). Wrap over [ApolloProvider](https://www.apollographql.com/docs/react/api/react-hooks/#apolloprovider).
72
-
73
- ```js
74
- const GqlProvider = require('@nerjs/gql/provider')
75
- // or
76
- import GqlProvider from '@nerjs/gql/provider'
77
- import React from 'react'
78
-
79
-
80
- const App = () => {
81
- return <GqlProvider {.../* providerProps */} />
82
- }
83
- ```
84
-
85
- #### providerProps
86
-
87
- 1. ***client***: (**[ApolloClient](https://www.apollographql.com/docs/react/api/apollo-client/)**): Required if not use [clientOptions](#clientoptions)
88
-
89
- or:
90
-
91
- 2. `{...options}` **[clientOptions](#clientoptions)**
92
-
93
- ### useGqlErrors
94
-
95
- ```js
96
- const useGqlErrors = require('@nerjs/gql/useGqlErrors')
97
- // or
98
- import useGqlErrors from '@nerjs/gql/useGqlErrors'
99
-
100
- const { lastError } = useGqlErrors()
101
- ```
102
-
103
- > Returns the last error
104
- > Does not work outside the [GqlProvider](#gqlprovider) or when using the [first option](#providerprops)
105
-
106
- ---
107
-
108
- ### Scalars (resolvers)
109
-
110
- ```js
111
- const { ...scalars } = require('@nerjs/gql/scalars')
112
- ```
113
-
114
- * DateResolver (scalar `Date`)
115
- * NumberResolver (scalar `Number`)
116
-
117
- ### gql middlewares
118
-
119
- ```js
120
- const { ...middlewares } = require('@nerjs/gql/mdw')
121
- ```
122
-
123
- #### validate middleware
124
-
125
- ```js
126
- const validateMiddleware = require('@nerjs/gql/mdw/validate')
127
-
128
-
129
- const schemaMiddlewares = {
130
- Query: {
131
- getItem: validateMiddleware({ input: yupInputSchema }),
132
- getItems: validateMiddleware(yupInputSchema)
133
- }
134
- }
135
- ```
136
-
137
- > Used [yup](https://github.com/jquense/yup#readme) validation
138
- > default **errorWrapper** [YupGqlError](https://github.com/nerjs/utils/tree/master/errors#yupgqlerror)
139
-
140
- ***Custom (not yup) validation***
141
-
142
- > The schema must have a `.validate(input)` method
143
-
144
- ```js
145
- const { createValidateMiddleware } = require('@nerjs/gql/mdw/validate')
146
-
147
- const validateMiddleware = createValidateMiddleware({
148
- isSchemaField: '__isYupSchema__', // The property of an object, by which it is clear that this is a validator scheme
149
- schemaOptions: { abortEarly: false },
150
- errorWrapper: Error
151
- })
152
- ```
153
-
154
- #### notNull middleware
155
-
156
- > Prevents return NULL
157
-
158
- ```js
159
- const notNullMiddleware = require('@nerjs/gql/mdw/notNull')
160
-
161
- const schemaMiddlewares = {
162
- User: {
163
- getUser: notNullMiddleware('User not found' /* error message */)
164
- }
165
- }
166
- ```
167
-
168
- > throw [NotFoundGqlError(message)](https://github.com/nerjs/utils/tree/master/errors#notfoundgqlerror) if resolver return null
169
-
170
- #### onlyId middleware
171
-
172
- > Prevents unnecessary resolver calls
173
-
174
- ```js
175
- const onlyIdMiddleware = require('@nerjs/gql/mdw/onlyId')
176
-
177
- const schemaMiddlewares = {
178
- Post: {
179
- author: onlyIdMiddleware()
180
- }
181
- }
182
- ```
183
-
184
- #### returnBoolean middleware
185
-
186
- > Returns a boolean value depending on the success of the resolver.
187
- > If `preventError` is specified as true - returns false instead of an exception
188
-
189
- ```js
190
- const returnBooleanMiddleware = require('@nerjs/gql/mdw/returnBoolean')
191
-
192
- const schemaMiddlewares = {
193
- Post: {
194
- author: onlyIdMiddleware(/* preventError = false */)
195
- }
196
- }
197
- ```
198
-
199
- ### combine middlewares
200
-
201
- > Combines and launches middlewares
202
-
203
- ```js
204
- const combineMiddlewares = require('@nerjs/gql/mdw/combine')
205
-
206
- const schemaMiddlewares = {
207
- Post: {
208
- author: combine(
209
- onlyIdMiddleware(),
210
- notNullMiddleware('Not foud author')
211
- )
212
- }
213
- }
214
- ```
215
-
216
-
217
- ---
218
-
219
-
220
- #### [:link: All utils ](https://github.com/nerjs/utils#readme)
3
+ Feel free to check out [my profile](https://github.com/nerjs) if you're interested
package/index.js ADDED
@@ -0,0 +1 @@
1
+ throw new Error('The utility is incomplete and is no longer supported. ')
package/package.json CHANGED
@@ -1,52 +1,31 @@
1
1
  {
2
2
  "name": "@nerjs/gql",
3
- "version": "1.5.0",
4
- "description": "Gql (apollo) config and utils",
3
+ "version": "2.0.0",
4
+ "description": "The utility is incomplete and is no longer supported.",
5
5
  "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
6
  "keywords": [
10
- "config",
11
- "Apollo",
12
- "gql",
13
- "graphql",
14
- "react"
7
+ "react",
8
+ "transition",
9
+ "css",
10
+ "className"
15
11
  ],
16
12
  "author": "nerjs",
17
13
  "license": "MIT",
18
- "bugs": {
19
- "url": "https://github.com/nerjs/utils/labels/gql"
20
- },
21
- "homepage": "https://github.com/nerjs/utils/tree/master/gql#readme",
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/nerjs/utils.git"
25
- },
14
+ "homepage": "https://github.com/nerjs",
26
15
  "dependencies": {
27
- "@nerjs/errors": "1.2.2",
28
- "apollo-cache-inmemory": "1.6.5",
29
- "apollo-client": "2.6.8",
30
- "apollo-link": "1.2.14",
31
- "apollo-link-batch-http": "1.2.14",
32
- "apollo-link-error": "1.1.13",
33
- "apollo-link-ws": "1.0.20",
34
- "apollo-server-express": "2.12.0",
35
- "graphql": "15.0.0",
36
- "graphql-fields": "2.0.3",
37
- "graphql-middleware": "4.0.2",
38
- "graphql-tools": "5.0.0",
39
- "merge-graphql-schemas": "1.7.8",
40
- "nlogs": "1.1.2",
41
- "subscriptions-transport-ws": "0.9.16"
42
- },
43
- "optionalDependencies": {
44
- "@apollo/react-hooks": "^3.1.5",
45
- "react": "^16.13.1"
46
- },
47
- "devDependencies": {
48
- "@apollo/react-hooks": "^3.1.5",
16
+ "@nerjs/batchloader": "^2.1.0",
17
+ "@nerjs/errors": "^1.2.2",
49
18
  "@nerjs/eslint": "^1.0.2",
50
- "react": "^16.13.1"
19
+ "@nerjs/express": "^1.0.3",
20
+ "@nerjs/webpack": "^1.0.3",
21
+ "create-redux-store": "^1.1.2",
22
+ "electron-ebt": "^1.1.0",
23
+ "helpers-promise": "^1.3.0",
24
+ "nerjs-utils": "^0.1.23",
25
+ "nlogs": "^2.4.4",
26
+ "@nerjs/gnome-shell-types": "^0.0.2",
27
+ "react-trc": "^2.0.0",
28
+ "@nerjs/task-planner": "^0.0.3",
29
+ "@nerjs/eslint-config": "^2.0.0"
51
30
  }
52
31
  }
@@ -1,33 +0,0 @@
1
- const { BatchHttpLink } = require('apollo-link-batch-http')
2
- const { WebSocketLink } = require('apollo-link-ws')
3
- const { getMainDefinition } = require('apollo-utilities')
4
- const { split } = require('apollo-link')
5
-
6
- const createHttpLink = ({ uri, httpOptions = {}, wsUri, wsOptions = {} }) => {
7
- const httpLink = new BatchHttpLink({ uri, ...httpOptions })
8
-
9
- if (!wsUri) return httpLink
10
-
11
- const wsLink = new WebSocketLink({
12
- uri: wsUri,
13
- options: {
14
- reconnect: true,
15
- lazy: true,
16
- ...wsOptions,
17
- },
18
- })
19
-
20
- return split(
21
- // split based on operation type
22
- ({ query }) => {
23
- const definition = getMainDefinition(query)
24
- return (
25
- definition.kind === 'OperationDefinition' && definition.operation === 'subscription'
26
- )
27
- },
28
- wsLink,
29
- httpLink,
30
- )
31
- }
32
-
33
- module.exports = createHttpLink
package/client/index.js DELETED
@@ -1,22 +0,0 @@
1
- const { ApolloClient } = require('apollo-client')
2
- const { InMemoryCache } = require('apollo-cache-inmemory')
3
- const { ApolloLink } = require('apollo-link')
4
- const { onError: onErrorLink } = require('apollo-link-error')
5
- const createHttpLink = require('./createHttpLink')
6
-
7
- const createClient = ({ uri, httpOptions, wsUri, wsOptions, links, onError }) => {
8
- const apolloLinks = [createHttpLink({ uri, httpOptions, wsUri, wsOptions })]
9
-
10
- if (links && Array.isArray(links)) apolloLinks.unshift(...links)
11
-
12
- if (onError && typeof onError === 'function') apolloLinks.unshift(onErrorLink(onError))
13
-
14
- const client = new ApolloClient({
15
- cache: new InMemoryCache(),
16
- link: apolloLinks.length === 1 ? apolloLinks[0] : ApolloLink.from(apolloLinks),
17
- })
18
-
19
- return client
20
- }
21
-
22
- module.exports = createClient
package/client.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import {ApolloClient} from "apollo-client";
2
-
3
- export = createClient
4
-
5
- declare namespace createClient2 {
6
- export declare interface ClientOptions2 {
7
-
8
- /** @description graphql server endpoint */
9
- uri: string;
10
- }
11
- }
12
-
13
- declare function createClient2(options: createClient.ClientOptions2): ApolloClient<any>;
@@ -1,12 +0,0 @@
1
- const isObjectId = require('./isObjectId')
2
-
3
- const isNumber = num => typeof num === 'number'
4
-
5
- const isString = str => typeof str === 'string'
6
-
7
- module.exports = {
8
- ObjectId: isObjectId,
9
- string: isString,
10
- number: isNumber,
11
- id: id => isObjectId(id) || isNumber(id),
12
- }
@@ -1,3 +0,0 @@
1
- module.exports = id =>
2
- (typeof id === 'string' && id.length === 24) ||
3
- (typeof id === 'object' && id._bsontype === 'ObjectID' && id.id instanceof Buffer)
package/index.d.ts DELETED
@@ -1,72 +0,0 @@
1
- import {ApolloClient} from "apollo-client";
2
- import { ApolloServer } from "apollo-server-express";
3
- import {Application} from "express";
4
- import { IMiddleware, IMiddlewareFieldMap, IMiddlewareFunction } from "graphql-middleware";
5
-
6
-
7
- declare module '@nerjs/gql/client' {
8
-
9
-
10
- declare interface ClientOptions {
11
-
12
- /** @description graphql server endpoint */
13
- uri: string;
14
- }
15
-
16
- export default function createClient(options: ClientOptions): ApolloClient<TCacheShape>;
17
- }
18
-
19
- declare module '@nerjs/gql/server' {
20
-
21
- declare interface GqlServerOptions {
22
-
23
- /** @description Express application */
24
- app: Application;
25
-
26
- /** @description Graphql endpoint */
27
- path: string;
28
-
29
- /** @description Enable graphql playground */
30
- playground?: boolean;
31
-
32
- /** @description Path to graphql types */
33
- types: string;
34
-
35
- /** @description Path to graphql resolvers */
36
- resolvers: string;
37
-
38
- middlewares?: [IMiddlewareFieldMap | IMiddlewareFunction | IMiddleware]
39
- }
40
-
41
- export default function createGqlServer(options:GqlServerOptions): ApolloServer;
42
- }
43
-
44
- declare module '@nerjs/gql/provider' {
45
-
46
-
47
- declare interface ClientOptions {
48
-
49
- /** @description graphql server endpoint */
50
- uri: string;
51
- }
52
-
53
- declare interface ApolloProviderProps {
54
-
55
- /** @description graphql server endpoint */
56
- uri: string;
57
-
58
- /** @description options for create apollo client */
59
- options?: ClientOptions
60
- }
61
-
62
- declare interface ApolloProviderProps {
63
-
64
- /** @description Apollo client */
65
- client: ApolloClient<any>;
66
- }
67
-
68
-
69
- declare const GqlProvider: FC<ApolloProviderProps>;
70
-
71
- export default GqlProvider
72
- }
package/mdw/combine.js DELETED
@@ -1,13 +0,0 @@
1
- const combineMiddlewares = (..._fns) => {
2
- const fns = _fns.filter(fn => typeof fn === 'function')
3
-
4
- const fabric = (resolver, idx) => {
5
- if (!fns[idx]) return resolver
6
- const fn = fns[idx]
7
- return (...args) => fn(fabric(resolver, idx + 1), ...args)
8
- }
9
-
10
- return (resolver, ...args) => fabric(resolver, 0)(...args)
11
- }
12
-
13
- module.exports = combineMiddlewares
package/mdw/index.js DELETED
@@ -1,5 +0,0 @@
1
- exports.validateMiddleware = require('./validate')
2
- exports.notNullMiddleware = require('./notNull')
3
- exports.onlyIdMiddleware = require('./onlyId')
4
- exports.returnBooleanMiddleware = require('./returnBoolean')
5
- exports.combine = require('./combine')
package/mdw/notNull.js DELETED
@@ -1,9 +0,0 @@
1
- const NotFoundGqlError = require('@nerjs/errors/NotFoundGqlError')
2
-
3
- const notNullMiddleware = message => async (resolver, ...args) => {
4
- const res = await resolver(...args)
5
- if (res === null) throw new NotFoundGqlError(message)
6
- return res
7
- }
8
-
9
- module.exports = notNullMiddleware
package/mdw/onlyId.js DELETED
@@ -1,25 +0,0 @@
1
- const graphqlFields = require('graphql-fields')
2
- const checkType = require('../helpers/checkType')
3
-
4
- const onlyIdMiddleware = (type = 'ObjectId', fieldQueryName = 'id', nullIfNotArg = true) => async (
5
- resolver,
6
- parent,
7
- args,
8
- ctx,
9
- info,
10
- ) => {
11
- const { fieldName } = info
12
- if (!parent[fieldName]) return nullIfNotArg ? null : resolver(parent, args, ctx, info)
13
-
14
- if (!checkType[type] || !checkType[type](parent[fieldName]))
15
- return resolver(parent, args, ctx, info)
16
-
17
- const fields = Object.keys(graphqlFields(info))
18
-
19
- if (fields.length === 1 && fields[0] === fieldQueryName)
20
- return { [fieldQueryName]: parent[fieldName] }
21
-
22
- return resolver(parent, args, ctx, info)
23
- }
24
-
25
- module.exports = onlyIdMiddleware
@@ -1,11 +0,0 @@
1
- const returnBooleanMiddleware = preventError => async (resolver, ...args) => {
2
- try {
3
- await resolver(...args)
4
- return true
5
- } catch (e) {
6
- if (preventError) return false
7
- throw e
8
- }
9
- }
10
-
11
- module.exports = returnBooleanMiddleware
package/mdw/validate.js DELETED
@@ -1,35 +0,0 @@
1
- const YupGqlError = require('@nerjs/errors/YupGqlError')
2
- const GqlError = require('@nerjs/errors/GqlError')
3
-
4
- const createValidateMiddleware = ({ isSchemaField, schemaOptions, errorWrapper }) => {
5
- const isSchema = obj =>
6
- obj && !!obj[isSchemaField] && obj.validate && typeof obj.validate === 'function'
7
-
8
- return fields => async (resolver, parent, args, ctx, info) => {
9
- try {
10
- if (isSchema(fields)) {
11
- await fields.validate(args, schemaOptions)
12
- } else {
13
- await Promise.all(
14
- Object.keys(fields || {}).map(field =>
15
- fields[field].validate(args[field], schemaOptions),
16
- ),
17
- )
18
- }
19
- } catch (e) {
20
- if (e.name === 'ValidationError') throw new errorWrapper(e)
21
-
22
- throw new GqlError('Something went wrong', GqlError.codes.INTERNAL_SERVER_ERROR, {}, e)
23
- }
24
-
25
- return resolver(parent, args, ctx, info)
26
- }
27
- }
28
-
29
- exports = module.exports = createValidateMiddleware({
30
- isSchemaField: '__isYupSchema__',
31
- schemaOptions: { abortEarly: false },
32
- errorWrapper: YupGqlError,
33
- })
34
-
35
- exports.createValidateMiddleware = createValidateMiddleware
@@ -1,7 +0,0 @@
1
- const { createContext } = require('react')
2
-
3
- const ErrorsContext = createContext({
4
- lastError: null,
5
- })
6
-
7
- module.exports = ErrorsContext
package/provider/index.js DELETED
@@ -1,29 +0,0 @@
1
- const React = require('react')
2
- const { ApolloProvider } = require('@apollo/react-hooks')
3
- const createClient = require('../client')
4
- const ErrorsContext = require('./ErrorsContext')
5
-
6
- const Provider = ({ children, client, onError, ...clientOptions }) => {
7
- const [lastError, setLastError] = React.useState(null)
8
- const [apolloClient] = React.useState(
9
- () => client || createClient({ ...clientOptions, onError: onErrorHandler }),
10
- )
11
-
12
- const onErrorHandler = React.useCallback(
13
- err => {
14
- setLastError(err)
15
- return onError && typeof onError === 'function'
16
- ? onError(err)
17
- : err.forward(err.operation)
18
- },
19
- [onError, setLastError],
20
- )
21
-
22
- return React.createElement(
23
- ApolloProvider,
24
- { client: apolloClient },
25
- React.createElement(ErrorsContext.Provider, { value: { lastError } }, children),
26
- )
27
- }
28
-
29
- module.exports = Provider
package/scalars/date.js DELETED
@@ -1,31 +0,0 @@
1
- const { GraphQLScalarType, Kind } = require('graphql')
2
-
3
- const parseDate = _date => {
4
- const date = new Date(_date)
5
- // if (isNaN(date)) throw new GqlValidationError(`Invalid Date.`, _date, date)
6
- if (isNaN(date)) throw new Error(`Invalid Date.`)
7
- return date
8
- }
9
-
10
- const DateResolver = new GraphQLScalarType({
11
- name: 'Date',
12
- description:
13
- 'The `Date` scalar type' +
14
- ' Can be transmitted as a number (`timestamp`) or a string (`js Date format`)',
15
- serialize(value) {
16
- const date = parseDate(value)
17
- return process.env.NODE_ENV !== 'production' ? date.toJSON() : date.getTime()
18
- },
19
- parseValue: parseDate,
20
- parseLiteral: ast => {
21
- if (ast.kind === Kind.INT) return parseDate(Number(ast.value))
22
- if (ast.kind === Kind.STRING) return parseDate(ast.value)
23
- throw new GqlValidationError(
24
- `Incorrect type argument. [${ast.kind}!==(${Kind.INT}|${Kind.STRING})]`,
25
- ast.kind,
26
- [Kind.INT, Kind.STRING],
27
- )
28
- },
29
- })
30
-
31
- module.exports = DateResolver
package/scalars/index.js DELETED
@@ -1,2 +0,0 @@
1
- exports.DateResolver = require('./date')
2
- exports.NumberResolver = require('./number')
package/scalars/number.js DELETED
@@ -1,44 +0,0 @@
1
- const { GraphQLScalarType, Kind } = require('graphql')
2
-
3
- const MAX_NUMBER = Math.pow(2, 64) - 1
4
- const MIN_NUMBER = -Math.pow(2, 63)
5
-
6
- /*
7
- feature
8
- После создания кастомного GqlError
9
- */
10
- // const parseNumber = num => {
11
- // if (typeof num !== 'number' || isNaN(num))
12
- // throw new GqlValidationError('Value is not a number', 'NaN', 'Number')
13
- // if (num < MIN_NUMBER || num > MAX_NUMBER)
14
- // throw new GqlValidationError('Number is out of range', num, `${MIN_NUMBER}<>${MAX_NUMBER}`)
15
- // if (num !== parseInt(num, 10))
16
- // throw new GqlValidationError(`Invalid non-fractional number.`, num, parseInt(num))
17
-
18
- // return num
19
- // }
20
-
21
- const parseNumber = num => {
22
- if (typeof num !== 'number' || isNaN(num)) throw new Error('Value is not a number')
23
- if (num < MIN_NUMBER || num > MAX_NUMBER) throw new Error('Number is out of range')
24
- if (num !== parseInt(num, 10)) throw new Error('Invalid non-fractional number.')
25
-
26
- return num
27
- }
28
-
29
- const NumberResolver = new GraphQLScalarType({
30
- name: 'Number',
31
- description:
32
- 'The `Number` scalar type represents non-fractional signed whole numeric values.' +
33
- ' `Number` can represent values between -(2^64) and 2^64 - 1.',
34
- serialize: parseNumber,
35
- parseValue: parseNumber,
36
- parseLiteral(ast) {
37
- if (ast.kind !== Kind.INT)
38
- throw new GqlValidationError(`Incorrect type argument.`, ast.kind, Kind.INT)
39
-
40
- return parseNumber(Number(ast.value))
41
- },
42
- })
43
-
44
- module.export = NumberResolver
package/server.js DELETED
@@ -1,63 +0,0 @@
1
- const { ApolloServer, ApolloError } = require('apollo-server-express')
2
- const { makeExecutableSchema } = require('graphql-tools')
3
- const { fileLoader, mergeTypes, mergeResolvers } = require('merge-graphql-schemas')
4
- const { applyMiddleware } = require('graphql-middleware')
5
- const DbGqlError = require('@nerjs/errors/DbGqlError')
6
- const logger = require('nlogs')(module)
7
-
8
- const createGqlServer = ({
9
- app,
10
- path,
11
- playground,
12
- types,
13
- resolvers,
14
- middlewares,
15
- cors,
16
- formatError,
17
- context,
18
- subscriptions,
19
- }) => {
20
- const executableSchema = makeExecutableSchema({
21
- typeDefs: mergeTypes(fileLoader(types)),
22
- resolvers: mergeResolvers(fileLoader(resolvers)),
23
- })
24
-
25
- const schema =
26
- middlewares && Array.isArray(middlewares)
27
- ? applyMiddleware(executableSchema, ...middlewares)
28
- : executableSchema
29
-
30
- const server = new ApolloServer({
31
- schema,
32
- playground: !!playground,
33
- cors,
34
- context,
35
- subscriptions,
36
- formatError: err => {
37
- if (process.env.NODE_ENV === 'production') {
38
- if (DbGqlError.is(err)) {
39
- logger.error(err)
40
- err = new ApolloError(
41
- 'Something went wrong',
42
- DbGqlError.codes.INTERNAL_SERVER_ERROR,
43
- )
44
- }
45
-
46
- const { code, exception, ...details } = err.extensions
47
- return new ApolloError(err.message, code, details)
48
- }
49
-
50
- return formatError && typeof formatError === 'function' ? formatError(err) : err
51
- },
52
- })
53
-
54
- server.applyMiddleware({
55
- app,
56
- path,
57
- cors,
58
- })
59
-
60
- return server
61
- }
62
-
63
- module.exports = createGqlServer
package/useGqlErrors.js DELETED
@@ -1,4 +0,0 @@
1
- const { useContext } = require('react')
2
- const ErrorsContext = require('./provider/ErrorsContext')
3
-
4
- module.exports = () => useContext(ErrorsContext)