Skip to content

Commit

Permalink
feat: support passing options to graphql validate and parse
Browse files Browse the repository at this point in the history
  • Loading branch information
markrzen committed Nov 8, 2023
1 parent 62f41ee commit 54d1f70
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { SocketStream } from "@fastify/websocket"
import { IncomingMessage, IncomingHttpHeaders, OutgoingHttpHeaders } from "http";
import { Readable } from "stream";
import { GraphQLParseOptions } from "@graphql-tools/utils";

export interface PubSub {
subscribe<TResult = any>(topics: string | string[]): Promise<Readable & AsyncIterableIterator<TResult>>;
Expand Down Expand Up @@ -356,6 +357,15 @@ export interface MercuriusGraphiQLOptions {
}>
}

export interface GrapQLValidateOptions {
maxErrors?: number;
}

export interface MercuriusGraphQLOptions {
parseOptions?: GraphQLParseOptions,
validateOptions?: GrapQLValidateOptions
}

export interface MercuriusCommonOptions {
/**
* Serve GraphiQL on /graphiql if true or 'graphiql' and if routes is true
Expand Down Expand Up @@ -413,6 +423,10 @@ export interface MercuriusCommonOptions {
* The maximum depth allowed for a single query.
*/
queryDepth?: number;
/**
* GraphQL function optional option overrides
*/
graphql?: MercuriusGraphQLOptions,
context?: (
request: FastifyRequest,
reply: FastifyReply
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ const plugin = fp(async function (app, opts) {
const queryDepthLimit = opts.queryDepth
const errorFormatter = typeof opts.errorFormatter === 'function' ? opts.errorFormatter : defaultErrorFormatter

opts.graphql = opts.graphql || {}
const gqlParseOpts = opts.graphql.parseOptions || {}
const gqlValidateOpts = opts.graphql.validateOptions || {}

if (opts.persistedQueries) {
if (opts.onlyPersisted) {
opts.persistedQueryProvider = persistedQueryDefaults.preparedOnly(opts.persistedQueries)
Expand Down Expand Up @@ -246,7 +250,7 @@ const plugin = fp(async function (app, opts) {

fastifyGraphQl.extendSchema = fastifyGraphQl.extendSchema || function (s) {
if (typeof s === 'string') {
s = parse(s)
s = parse(s, gqlParseOpts)
} else if (!s || typeof s !== 'object') {
throw new MER_ERR_INVALID_OPTS('Must provide valid Document AST')
}
Expand Down Expand Up @@ -428,7 +432,7 @@ const plugin = fp(async function (app, opts) {
}

try {
document = parse(source)
document = parse(source, gqlParseOpts)
} catch (syntaxError) {
try {
// Try to parse the source as ast
Expand All @@ -454,7 +458,7 @@ const plugin = fp(async function (app, opts) {
validationRules = opts.validationRules({ source, variables, operationName })
}
}
const validationErrors = validate(fastifyGraphQl.schema, document, [...specifiedRules, ...validationRules])
const validationErrors = validate(fastifyGraphQl.schema, document, [...specifiedRules, ...validationRules], gqlValidateOpts)

if (validationErrors.length > 0) {
if (lruErrors) {
Expand Down

0 comments on commit 54d1f70

Please sign in to comment.