Skip to content

Commit

Permalink
cherry pick part of "fix: Improve GraphQL Schema Validation to allow …
Browse files Browse the repository at this point in the history
…Subscription types when not using Realtime and ensure schema does not use reserved names (#9005)"
  • Loading branch information
jtoar committed Aug 9, 2023
1 parent 5c8fb2c commit efa86ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/internal/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,29 @@ export const getTsConfigs = () => {
web: webTsConfig?.config ?? null,
}
}

export const isTypeScriptProject = () => {
const paths = getPaths()
return (
fs.existsSync(path.join(paths.web.base, 'tsconfig.json')) ||
fs.existsSync(path.join(paths.api.base, 'tsconfig.json'))
)
}

export const isServerFileSetup = () => {
const serverFilePath = path.join(
getPaths().api.src,
`server.${isTypeScriptProject() ? 'ts' : 'js'}`
)

return fs.existsSync(serverFilePath)
}

export const isRealtimeSetup = () => {
const realtimePath = path.join(
getPaths().api.lib,
`realtime.${isTypeScriptProject() ? 'ts' : 'js'}`
)

return fs.existsSync(realtimePath)
}
11 changes: 10 additions & 1 deletion packages/internal/src/validateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ import { DocumentNode, Kind, ObjectTypeDefinitionNode, visit } from 'graphql'
import { rootSchema } from '@redwoodjs/graphql-server'
import { getPaths } from '@redwoodjs/project-config'

import { isServerFileSetup, isRealtimeSetup } from './project'

export const DIRECTIVE_REQUIRED_ERROR_MESSAGE =
'You must specify one of @requireAuth, @skipAuth or a custom directive'

export const DIRECTIVE_INVALID_ROLE_TYPES_ERROR_MESSAGE =
'Please check that the requireAuth roles is a string or an array of strings.'
export function validateSchemaForDirectives(
schemaDocumentNode: DocumentNode,
typesToCheck: string[] = ['Query', 'Mutation', 'Subscription']
typesToCheck: string[] = ['Query', 'Mutation']
) {
const validationOutput: string[] = []
const directiveRoleValidationOutput: Record<string, any> = []

// Is Subscriptions are enabled with Redwood Realtime, then enforce a rule
// that a Subscription type needs to have a authentication directive applied,
// just as Query and Mutation requires
if (isServerFileSetup() && isRealtimeSetup()) {
typesToCheck.push('Subscription')
}

visit(schemaDocumentNode, {
ObjectTypeDefinition(typeNode) {
if (typesToCheck.includes(typeNode.name.value)) {
Expand Down

0 comments on commit efa86ef

Please sign in to comment.