Skip to content

Commit

Permalink
fix: apollo-server-errors import for typescript projects using apol…
Browse files Browse the repository at this point in the history
…lo4 (#243)

fixes #156
  • Loading branch information
damienwebdev authored Sep 17, 2024
1 parent 817edc6 commit 2f1aa22
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
${{ runner.OS }}-node-
${{ runner.OS }}-
- run: npm ci
- run: npm run test-typescript
- run: npm test
- name: Coveralls Parallel
uses: coverallsapp/github-action@master
Expand Down
2 changes: 1 addition & 1 deletion apollo4.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const {
buildSchema,
GraphQLError
} = require('graphql')
const { validateQuery } = require('./index')
const { constraintDirectiveTypeDefs } = require('./lib/type-defs')
const { gql } = require('graphql-tag')
const { validateQuery } = require('./lib/validate-query')

let currentSchema

Expand Down
28 changes: 2 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const {
GraphQLNonNull,
GraphQLList,
TypeInfo,
ValidationContext,
visit,
visitWithTypeInfo,
separateOperations,
GraphQLError,
getDirectiveValues
} = require('graphql')
const QueryValidationVisitor = require('./lib/query-validation-visitor.js')
const { validateQuery } = require('./lib/validate-query')

const { getDirective, mapSchema, MapperKind } = require('@graphql-tools/utils')
const { getConstraintTypeObject, getScalarType } = require('./lib/type-utils')
const { constraintDirectiveTypeDefs, constraintDirectiveTypeDefsObj } = require('./lib/type-defs')
Expand Down Expand Up @@ -173,28 +171,6 @@ function constraintDirectiveDocumentation (options) {
})
}

function validateQuery (schema, query, variables, operationName, pluginOptions = {}) {
const typeInfo = new TypeInfo(schema)

const errors = []
const context = new ValidationContext(
schema,
query,
typeInfo,
(error) => errors.push(error)
)

const visitor = new QueryValidationVisitor(context, {
variables,
operationName,
pluginOptions
})

visit(query, visitWithTypeInfo(typeInfo, visitor))

return errors
}

function createApolloQueryValidationPlugin ({ schema }, options = {}) {
return {
async requestDidStart () {
Expand Down
3 changes: 3 additions & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = class ConstraintDirectiveError extends Error {
;

constructor (fieldName, message, context) {
super(message)
this.name = this.constructor.name
Expand All @@ -7,5 +9,6 @@ module.exports = class ConstraintDirectiveError extends Error {
this.code = 'ERR_GRAPHQL_CONSTRAINT_VALIDATION'
this.fieldName = fieldName
this.context = context
this.originalError = undefined
}
}
2 changes: 1 addition & 1 deletion lib/type-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function getScalarType (fieldConfig) {
} else if (isNonNullType(fieldConfig) && isScalarType(fieldConfig.ofType)) {
return { scalarType: fieldConfig.ofType, scalarNotNull: true }
} else if (isNonNullType(fieldConfig)) {
return { ...getScalarType(fieldConfig.ofType.ofType), list: true, listNotNull: true }
return { ...getScalarType(fieldConfig.ofType), list: true, listNotNull: true }
} else {
throw new Error(`Not a valid scalar type: ${fieldConfig.toString()}`)
}
Expand Down
31 changes: 31 additions & 0 deletions lib/validate-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const {
TypeInfo,
ValidationContext,
visit,
visitWithTypeInfo
} = require('graphql')
const QueryValidationVisitor = require('./query-validation-visitor.js')

function validateQuery (schema, query, variables, operationName, pluginOptions = {}) {
const typeInfo = new TypeInfo(schema)

const errors = []
const context = new ValidationContext(
schema,
query,
typeInfo,
(error) => errors.push(error)
)

const visitor = new QueryValidationVisitor(context, {
variables,
operationName,
pluginOptions
})

visit(query, visitWithTypeInfo(typeInfo, visitor))

return errors
}

module.exports = { validateQuery }
22 changes: 21 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test-apollo-plugin": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-apollo-plugin.js",
"test-apollo4-plugin": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-apollo4-plugin.js",
"test-envelop-plugin": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-envelop-plugin.js",
"test-typescript": "tsc --project tsconfig.apollo4.json && tsc --project tsconfig.apollo.json",
"test-validation-rule-express-graphql": "standard && nyc --reporter=html --reporter=text --reporter=lcov mocha test/**/testsuite-validation-rule-express-graphql.js"
},
"author": "James Mortemore ([email protected])",
Expand All @@ -34,16 +35,17 @@
"constraint"
],
"devDependencies": {
"apollo-server-express": "3.13.0",
"@apollo/server": "4.9.5",
"@graphql-yoga/node": "2.13.13",
"apollo-server-express": "3.13.0",
"coveralls": "3.1.1",
"express": "4.21.0",
"graphql": "16.6.0",
"mocha": "10.7.3",
"nyc": "15.1.0",
"standard": "16.0.4",
"supertest": "6.3.4",
"@graphql-yoga/node": "2.13.13"
"typescript": "^5.5.3"
},
"dependencies": {
"@graphql-tools/schema": "^9.0.0",
Expand Down
1 change: 1 addition & 0 deletions test/typescript/apollo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { constraintDirective, constraintDirectiveTypeDefs } from 'graphql-constraint-directive';
3 changes: 3 additions & 0 deletions test/typescript/apollo4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { constraintDirectiveTypeDefs, createApollo4QueryValidationPlugin } from 'graphql-constraint-directive/apollo4';

export { constraintDirectiveTypeDefs, createApollo4QueryValidationPlugin };
3 changes: 3 additions & 0 deletions test/typescript/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const errorFn: () => string = {

}
32 changes: 32 additions & 0 deletions tsconfig.apollo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"declaration": false,
"moduleResolution": "node",
"noEmit": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": [
"es2020",
],
"checkJs": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"paths": {
"graphql-constraint-directive": [
"index.js",
"index.d.ts",
]
}
},
"include": [
"./test/typescript/apollo.ts"
],
"exclude": [
"node_modules",
"tmp"
]
}
35 changes: 35 additions & 0 deletions tsconfig.apollo4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"declaration": false,
"moduleResolution": "node",
"noEmit": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": [
"es2020",
],
"checkJs": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"paths": {
"graphql-constraint-directive/apollo4": [
"apollo4.js",
"apollo4.d.ts",
],
"apollo-server-errors": [
"./test/typescript/error.ts"
]
}
},
"include": [
"./test/typescript/apollo4.ts"
],
"exclude": [
"node_modules",
"tmp"
]
}

0 comments on commit 2f1aa22

Please sign in to comment.