Skip to content

Commit 2f1aa22

Browse files
authored
fix: apollo-server-errors import for typescript projects using apollo4 (#243)
fixes #156
1 parent 817edc6 commit 2f1aa22

File tree

13 files changed

+138
-31
lines changed

13 files changed

+138
-31
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
${{ runner.OS }}-node-
3333
${{ runner.OS }}-
3434
- run: npm ci
35+
- run: npm run test-typescript
3536
- run: npm test
3637
- name: Coveralls Parallel
3738
uses: coverallsapp/github-action@master

apollo4.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const {
33
buildSchema,
44
GraphQLError
55
} = require('graphql')
6-
const { validateQuery } = require('./index')
76
const { constraintDirectiveTypeDefs } = require('./lib/type-defs')
87
const { gql } = require('graphql-tag')
8+
const { validateQuery } = require('./lib/validate-query')
99

1010
let currentSchema
1111

index.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
const {
22
GraphQLNonNull,
33
GraphQLList,
4-
TypeInfo,
5-
ValidationContext,
6-
visit,
7-
visitWithTypeInfo,
84
separateOperations,
95
GraphQLError,
106
getDirectiveValues
117
} = require('graphql')
128
const QueryValidationVisitor = require('./lib/query-validation-visitor.js')
9+
const { validateQuery } = require('./lib/validate-query')
10+
1311
const { getDirective, mapSchema, MapperKind } = require('@graphql-tools/utils')
1412
const { getConstraintTypeObject, getScalarType } = require('./lib/type-utils')
1513
const { constraintDirectiveTypeDefs, constraintDirectiveTypeDefsObj } = require('./lib/type-defs')
@@ -173,28 +171,6 @@ function constraintDirectiveDocumentation (options) {
173171
})
174172
}
175173

176-
function validateQuery (schema, query, variables, operationName, pluginOptions = {}) {
177-
const typeInfo = new TypeInfo(schema)
178-
179-
const errors = []
180-
const context = new ValidationContext(
181-
schema,
182-
query,
183-
typeInfo,
184-
(error) => errors.push(error)
185-
)
186-
187-
const visitor = new QueryValidationVisitor(context, {
188-
variables,
189-
operationName,
190-
pluginOptions
191-
})
192-
193-
visit(query, visitWithTypeInfo(typeInfo, visitor))
194-
195-
return errors
196-
}
197-
198174
function createApolloQueryValidationPlugin ({ schema }, options = {}) {
199175
return {
200176
async requestDidStart () {

lib/error.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module.exports = class ConstraintDirectiveError extends Error {
2+
;
3+
24
constructor (fieldName, message, context) {
35
super(message)
46
this.name = this.constructor.name
@@ -7,5 +9,6 @@ module.exports = class ConstraintDirectiveError extends Error {
79
this.code = 'ERR_GRAPHQL_CONSTRAINT_VALIDATION'
810
this.fieldName = fieldName
911
this.context = context
12+
this.originalError = undefined
1013
}
1114
}

lib/type-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function getScalarType (fieldConfig) {
4848
} else if (isNonNullType(fieldConfig) && isScalarType(fieldConfig.ofType)) {
4949
return { scalarType: fieldConfig.ofType, scalarNotNull: true }
5050
} else if (isNonNullType(fieldConfig)) {
51-
return { ...getScalarType(fieldConfig.ofType.ofType), list: true, listNotNull: true }
51+
return { ...getScalarType(fieldConfig.ofType), list: true, listNotNull: true }
5252
} else {
5353
throw new Error(`Not a valid scalar type: ${fieldConfig.toString()}`)
5454
}

lib/validate-query.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const {
2+
TypeInfo,
3+
ValidationContext,
4+
visit,
5+
visitWithTypeInfo
6+
} = require('graphql')
7+
const QueryValidationVisitor = require('./query-validation-visitor.js')
8+
9+
function validateQuery (schema, query, variables, operationName, pluginOptions = {}) {
10+
const typeInfo = new TypeInfo(schema)
11+
12+
const errors = []
13+
const context = new ValidationContext(
14+
schema,
15+
query,
16+
typeInfo,
17+
(error) => errors.push(error)
18+
)
19+
20+
const visitor = new QueryValidationVisitor(context, {
21+
variables,
22+
operationName,
23+
pluginOptions
24+
})
25+
26+
visit(query, visitWithTypeInfo(typeInfo, visitor))
27+
28+
return errors
29+
}
30+
31+
module.exports = { validateQuery }

package-lock.json

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"test-apollo-plugin": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-apollo-plugin.js",
1212
"test-apollo4-plugin": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-apollo4-plugin.js",
1313
"test-envelop-plugin": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-envelop-plugin.js",
14+
"test-typescript": "tsc --project tsconfig.apollo4.json && tsc --project tsconfig.apollo.json",
1415
"test-validation-rule-express-graphql": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-validation-rule-express-graphql.js"
1516
},
1617
"author": "James Mortemore ([email protected])",
@@ -34,16 +35,17 @@
3435
"constraint"
3536
],
3637
"devDependencies": {
37-
"apollo-server-express": "3.13.0",
3838
"@apollo/server": "4.9.5",
39+
"@graphql-yoga/node": "2.13.13",
40+
"apollo-server-express": "3.13.0",
3941
"coveralls": "3.1.1",
4042
"express": "4.21.0",
4143
"graphql": "16.6.0",
4244
"mocha": "10.7.3",
4345
"nyc": "15.1.0",
4446
"standard": "16.0.4",
4547
"supertest": "6.3.4",
46-
"@graphql-yoga/node": "2.13.13"
48+
"typescript": "^5.5.3"
4749
},
4850
"dependencies": {
4951
"@graphql-tools/schema": "^9.0.0",

test/typescript/apollo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { constraintDirective, constraintDirectiveTypeDefs } from 'graphql-constraint-directive';

test/typescript/apollo4.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { constraintDirectiveTypeDefs, createApollo4QueryValidationPlugin } from 'graphql-constraint-directive/apollo4';
2+
3+
export { constraintDirectiveTypeDefs, createApollo4QueryValidationPlugin };

0 commit comments

Comments
 (0)