graphql-anywhere-mongodb
Version:
Use graphql to query mongodb
147 lines (146 loc) • 4.68 kB
TypeScript
import { Db } from 'mongodb';
import { DocumentNode } from 'graphql';
import { GraphQLExecutionResult } from './mongo-queries';
/**
* Function that formats errors off of {GraphQLExecutionResult}s.
*/
export declare type ErrorFormatter = (result: GraphQLExecutionResult, includeStack?: boolean) => any;
/**
* Options to change the behavior of the GraphQL mongo client.
*/
export interface GraphQLMongoClientOptions {
/**
* Whitelist of collections that can be queried. If provided, an error will be thrown any time an attempt is
* made to query a collection not in the whitelist.
*/
whitelist?: string[];
/**
* If true, will include stack trace in error results. Defaults to false.
*/
includeStack?: boolean;
/**
* Function to format error objects. Defaults to the following format:
* {
* collection: string;
* message: string;
* stack?: string[]; // if includeStack === true
* }
*/
formatError?: ErrorFormatter;
/**
* If no limit clause is passed, this default limit will be used. This is to prevent
* someone accidentally pulling in thousands or millions of mongo documents by mistake.
* Defaults to 100 if not passed.
*/
defaultLimit?: number;
/**
* The maximum limit accepted for a graphql request. If a limit is encountered above this
* an error will be thrown. Defaults to 10000.
*/
maxLimit?: number;
}
/**
* Results from a graphql query. Will contain the {data} element which will have results
* for every collection-based query that succeeded as well as an {errors} array that will contain
* any errors that occurred.
*/
export interface QueryResult {
/**
* The data that was retrieved. Will have an entry per collection queried under that collection's name.
*
* @example
* If you query for the following:
*
* {
* users {
* name
* }
* places {
* lat
* lng
* }
* }
*
* Your response will be:
* {
* data: {
* users: [ ...users results here ],
* places: [ ...places results here ],
* }
* }
*/
data: {
[collection: string]: any;
};
/**
* Errors that occurred during the process (if any).
*/
errors?: any[];
/**
* Metadata about each collection query such as the limit applied and offset.
*/
_meta?: {
[collection: string]: {
limit?: number;
offset?: number;
};
};
}
/**
* A mongo client that wraps the standard node MongoDB driver with
* an API that allows for making queries using GraphQL.
*/
export declare class MongoGraphQLClient {
private readonly connection;
private readonly whitelist;
private readonly includeStack;
private readonly errorFormatter;
private readonly defaultLimit;
private readonly maxLimit;
/**
* Create a new {MongoGraphQLClient}.
* @param connection The DB connection.
* @param options Options to change the behavior of the client.
*/
constructor(connection: Db, options?: GraphQLMongoClientOptions);
/**
* Gets the options that this client was configured with.
*/
getOptions(): {
database: string;
whitelist: string[];
includeStack: boolean;
errorFormatter: ErrorFormatter;
defaultLimit: number;
maxLimit: number;
};
/**
* Performs a MongoDB find operation for every collection specified in the passed
* GraphQL query and returns the results and any errors as a promise.
*
* @param query The query to perform.
* @param variables Variables to use in the query.
* @return {Promise<QueryResult>} The result of the queries.
*/
find(query: DocumentNode | string, variables?: object): Promise<QueryResult>;
/**
* Performs a MongoDB findOne operation for exactly one collection and returns
* just a single document for that collection. Will throw an error if multiple
* collections are included in the query.
*
* @param query The query to perform,.
* @param variables Variables to use in the query.
* @return {Promise<QueryResult>}
*/
findOne(query: DocumentNode | string, variables?: object): Promise<QueryResult>;
}
/**
* Default error formatter
* @param result The result of the query.
* @param includeStack If true, signifies that stack should be printed.
*/
export declare function defaultErrorFormatter(result: GraphQLExecutionResult, includeStack: boolean): {
collection: string;
message: string | Error;
stack: string[];
};