( directiveName: string, schema: GraphQLSchema, ast: DocumentNode, )
| 91 | } |
| 92 | |
| 93 | export function requiresAuth( |
| 94 | directiveName: string, |
| 95 | schema: GraphQLSchema, |
| 96 | ast: DocumentNode, |
| 97 | ): boolean { |
| 98 | const typeInfo = new TypeInfo(schema) |
| 99 | let result = false |
| 100 | |
| 101 | visit( |
| 102 | ast, |
| 103 | visitWithTypeInfo(typeInfo, { |
| 104 | enter(node) { |
| 105 | if (node.kind === 'FragmentSpread' || node.kind === 'InlineFragment') { |
| 106 | const type = typeInfo.getParentType() |
| 107 | if (type) { |
| 108 | if ( |
| 109 | type.astNode?.kind === 'InterfaceTypeDefinition' || |
| 110 | type.astNode?.kind === 'ObjectTypeDefinition' |
| 111 | ) { |
| 112 | result = !!type.astNode?.fields?.some((field) => |
| 113 | hasAuthDirective(field, directiveName), |
| 114 | ) |
| 115 | } else { |
| 116 | result = hasAuthDirective(type.astNode, directiveName) |
| 117 | } |
| 118 | } |
| 119 | } else if (node.kind === 'Field') { |
| 120 | const field = typeInfo.getFieldDef() |
| 121 | if (field) { |
| 122 | result = hasAuthDirective(field.astNode, directiveName) |
| 123 | } |
| 124 | } |
| 125 | if (result) return BREAK |
| 126 | }, |
| 127 | }), |
| 128 | ) |
| 129 | |
| 130 | return result |
| 131 | } |
| 132 | |
| 133 | export const buildGraphQLSchema = (schema: string) => { |
| 134 | return buildSchema(schema, { |
no outgoing calls
no test coverage detected