Skip to content

Commit cd21f60

Browse files
veliasconfuser
andauthored
fix: patched documentation generator exception on missing astNode (#176)
fixes #175 Co-authored-by: James Mortemore <[email protected]>
1 parent 1d59819 commit cd21f60

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed

index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,25 @@ function constraintDirectiveDocumentation (options) {
147147
return (schema) =>
148148
mapSchema(schema, {
149149
[MapperKind.FIELD]: (fieldConfig) => {
150-
const directiveArgumentMap = getDirectiveValues(constraintDirectiveTypeDefsObj, fieldConfig.astNode)
150+
if (fieldConfig?.astNode) {
151+
const directiveArgumentMap = getDirectiveValues(constraintDirectiveTypeDefsObj, fieldConfig.astNode)
151152

152-
if (directiveArgumentMap) {
153-
documentConstraintDirective(fieldConfig, directiveArgumentMap)
153+
if (directiveArgumentMap) {
154+
documentConstraintDirective(fieldConfig, directiveArgumentMap)
154155

155-
return fieldConfig
156+
return fieldConfig
157+
}
156158
}
157159
},
158160
[MapperKind.ARGUMENT]: (fieldConfig) => {
159-
const directiveArgumentMap = getDirectiveValues(constraintDirectiveTypeDefsObj, fieldConfig.astNode)
161+
if (fieldConfig?.astNode) {
162+
const directiveArgumentMap = getDirectiveValues(constraintDirectiveTypeDefsObj, fieldConfig.astNode)
160163

161-
if (directiveArgumentMap) {
162-
documentConstraintDirective(fieldConfig, directiveArgumentMap)
164+
if (directiveArgumentMap) {
165+
documentConstraintDirective(fieldConfig, directiveArgumentMap)
163166

164-
return fieldConfig
167+
return fieldConfig
168+
}
165169
}
166170
}
167171
})

test/snapshots/ws-1-1.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
directive @constraint(minLength: Int, maxLength: Int, startsWith: String, endsWith: String, contains: String, notContains: String, pattern: String, format: String, min: Float, max: Float, exclusiveMin: Float, exclusiveMax: Float, multipleOf: Float, minItems: Int, maxItems: Int, uniqueTypeName: String) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | ARGUMENT_DEFINITION
2+
3+
type Query {
4+
"""Query books field documented"""
5+
books(
6+
"*Constraints:*\n* Minimal value: `1`\n* Maximal value: `3`\n"
7+
size: Int
8+
9+
" Query argument documented \n\n*Constraints:*\n* Minimal length: `1`\n"
10+
first: String
11+
): [Book]
12+
13+
""" Query book field documented """
14+
book: Book
15+
addedField: String
16+
}
17+
18+
type Book {
19+
"*Constraints:*\n* Maximal length: `10`\n"
20+
title: String
21+
22+
"Book description already documented\n\n*Constraints:*\n* Minimal length: `10`\n* Maximal length: `50`\n"
23+
description: String
24+
authors(
25+
"Book authors argument documented\n\n*Constraints:*\n* Maximal value: `4`\n"
26+
size: Int
27+
28+
"""
29+
Already documented
30+
31+
*Constraints:*
32+
* Minimal length as documented: 1
33+
"""
34+
first: String
35+
): [String]
36+
}
37+
38+
type Mutation {
39+
createBook(input: BookInput): Book
40+
}
41+
42+
input BookInput {
43+
"*Constraints:*\n* Minimal value: `3`\n"
44+
title: Int!
45+
author: AuthorInput
46+
}
47+
48+
input AuthorInput {
49+
"*Constraints:*\n* Minimal length: `2`\n"
50+
name: String!
51+
}

test/testsuite-documentation.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const { constraintDirectiveTypeDefs, constraintDirectiveDocumentation, constraintDirective } = require('..')
22
const { makeExecutableSchema } = require('@graphql-tools/schema')
3-
const { printSchema } = require('graphql')
3+
const { GraphQLString, GraphQLObjectType, printSchema } = require('graphql')
44
const { equal } = require('assert')
55
const fs = require('fs')
6+
const { mapSchema, MapperKind } = require('@graphql-tools/utils')
67

78
/**
89
* If `true` schema data are refreshed in the storage, if `false` schema is compared with the data in the storage
@@ -82,6 +83,33 @@ describe('Schema Documentation', function () {
8283
assertSchemaData('ws-1', schema)
8384
})
8485

86+
it('works - default options - modified schema', async function () {
87+
let schema = makeExecutableSchema({
88+
typeDefs: [constraintDirectiveTypeDefs, typeDefs]
89+
})
90+
91+
schema = addFieldToSchema(schema)
92+
93+
schema = constraintDirectiveDocumentation()(schema)
94+
95+
assertSchemaData('ws-1-1', schema)
96+
})
97+
98+
function addFieldToSchema (schema) {
99+
return mapSchema(schema, {
100+
[MapperKind.OBJECT_TYPE]: type => {
101+
if (type.name === 'Query') {
102+
const config = type.toConfig()
103+
config.fields.addedField = {
104+
type: GraphQLString,
105+
args: {}
106+
}
107+
return new GraphQLObjectType(config)
108+
}
109+
}
110+
})
111+
}
112+
85113
it('works - customized options', async function () {
86114
let schema = makeExecutableSchema({
87115
typeDefs: [constraintDirectiveTypeDefs, typeDefs]

0 commit comments

Comments
 (0)