graphql#specifiedRules TypeScript Examples
The following examples show how to use
graphql#specifiedRules.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: validation.ts From amplify-codegen with Apache License 2.0 | 6 votes |
export function validateQueryDocument(schema: GraphQLSchema, document: DocumentNode) {
const specifiedRulesToBeRemoved = [NoUnusedFragmentsRule];
const rules = [NoAnonymousQueries, NoTypenameAlias, ...specifiedRules.filter(rule => !specifiedRulesToBeRemoved.includes(rule))];
const validationErrors = validate(schema, document, rules);
if (validationErrors && validationErrors.length > 0) {
for (const error of validationErrors) {
logError(error);
}
throw new ToolError('Validation of GraphQL query document failed');
}
}
Example #2
Source File: index.ts From hono with MIT License | 4 votes |
graphqlServer = (options: Options) => {
const schema = options.schema
const rootValue = options.rootValue
const pretty = options.pretty ?? false
const validationRules = options.validationRules ?? []
// const showGraphiQL = options.graphiql ?? false
return async (c: Context, next: Next) => {
// GraphQL HTTP only supports GET and POST methods.
if (c.req.method !== 'GET' && c.req.method !== 'POST') {
return c.json(errorMessages(['GraphQL only supports GET and POST requests.']), 405, {
Allow: 'GET, POST',
})
}
let params: GraphQLParams
try {
params = await getGraphQLParams(c.req)
} catch (e) {
if (e instanceof Error) {
console.error(`${e.stack || e.message}`)
return c.json(errorMessages([e.message], [e]), 400)
}
throw e
}
const { query, variables, operationName } = params
if (query == null) {
return c.json(errorMessages(['Must provide query string.']), 400)
}
const schemaValidationErrors = validateSchema(schema)
if (schemaValidationErrors.length > 0) {
// Return 500: Internal Server Error if invalid schema.
return c.json(
errorMessages(['GraphQL schema validation error.'], schemaValidationErrors),
500
)
}
let documentAST: DocumentNode
try {
documentAST = parse(new Source(query, 'GraphQL request'))
} catch (syntaxError: unknown) {
// Return 400: Bad Request if any syntax errors errors exist.
if (syntaxError instanceof Error) {
console.error(`${syntaxError.stack || syntaxError.message}`)
const e = new GraphQLError(syntaxError.message, {
originalError: syntaxError,
})
return c.json(errorMessages(['GraphQL syntax error.'], [e]), 400)
}
throw syntaxError
}
// Validate AST, reporting any errors.
const validationErrors = validate(schema, documentAST, [...specifiedRules, ...validationRules])
if (validationErrors.length > 0) {
// Return 400: Bad Request if any validation errors exist.
return c.json(errorMessages(['GraphQL validation error.'], validationErrors), 400)
}
if (c.req.method === 'GET') {
// Determine if this GET request will perform a non-query.
const operationAST = getOperationAST(documentAST, operationName)
if (operationAST && operationAST.operation !== 'query') {
/*
Now , does not support GraphiQL
if (showGraphiQL) {
//return respondWithGraphiQL(response, graphiqlOptions, params)
}
*/
// Otherwise, report a 405: Method Not Allowed error.
return c.json(
errorMessages([
`Can only perform a ${operationAST.operation} operation from a POST request.`,
]),
405,
{ Allow: 'POST' }
)
}
}
let result: FormattedExecutionResult
try {
result = await execute({
schema,
document: documentAST,
rootValue,
variableValues: variables,
operationName: operationName,
})
} catch (contextError: unknown) {
if (contextError instanceof Error) {
console.error(`${contextError.stack || contextError.message}`)
const e = new GraphQLError(contextError.message, {
originalError: contextError,
nodes: documentAST,
})
// Return 400: Bad Request if any execution context errors exist.
return c.json(errorMessages(['GraphQL execution context error.'], [e]), 400)
}
throw contextError
}
if (!result.data) {
if (result.errors) {
return c.json(errorMessages([result.errors.toString()], result.errors), 500)
}
}
/*
Now, does not support GraphiQL
if (showGraphiQL) {
}
*/
if (pretty) {
const payload = JSON.stringify(result, null, pretty ? 2 : 0)
return c.text(payload, 200, {
'Content-Type': 'application/json',
})
} else {
return c.json(result)
}
await next() // XXX
}
}