@fluentity/core
Version:
Fluentity is a fluent, model-oriented, typed HTTP client for TypeScript and framework agnostic.
176 lines (175 loc) • 5.65 kB
TypeScript
import { QueryBuilder } from '../QueryBuilder';
import { HttpAdapter, HttpAdapterOptions, HttpRequest } from './HttpAdapter';
/**
* GraphQL adapter for making GraphQL API requests.
* Extends HttpAdapter to provide GraphQL-specific functionality including query building,
* mutation handling, and GraphQL operation management.
*
* Features:
* - Automatic GraphQL query generation
* - Support for queries and mutations
* - Variable handling and operation naming
* - Field selection and argument building
*
* @class
* @extends {HttpAdapter}
* @example
* ```typescript
* const adapter = new GraphqlAdapter({
* baseUrl: 'https://api.example.com',
* endpoint: '/graphql'
* });
*
* // Use with Fluentity
* Fluentity.initialize({
* adapter: adapter
* });
* ```
*/
export declare class GraphqlAdapter<T extends GraphqlAdapterOptions> extends HttpAdapter<T> {
options: T;
/**
* Constructor for the GraphqlAdapter class.
* Initializes the adapter with GraphQL-specific configuration.
*
* @param options - Partial configuration options to merge with existing options
* @throws {Error} If endpoint is not provided
* @example
* ```typescript
* const adapter = new GraphqlAdapter({
* baseUrl: 'https://api.example.com',
* endpoint: '/graphql',
* options: {
* headers: {
* 'Authorization': 'Bearer token'
* }
* }
* });
* ```
*/
constructor(options: Partial<T>);
/**
* Builds an HTTP request from a query builder for GraphQL operations.
* Converts the query builder into a GraphQL-specific HTTP request.
*
* @param queryBuilder - The query builder containing request details
* @returns An HttpRequest configured for GraphQL
* @protected
*/
protected buildRequest(queryBuilder: QueryBuilder<T>): HttpRequest;
/**
* Builds the GraphQL request body containing query, variables, and operation name.
*
* @param queryBuilder - The query builder to build the body from
* @returns GraphQL request body object
* @private
*/
private buildBody;
/**
* Determines the GraphQL operation type based on the HTTP method.
* Maps HTTP methods to GraphQL operations (query or mutation).
*
* @param queryBuilder - The query builder containing the HTTP method
* @returns The GraphQL operation type ('query' or 'mutation')
* @private
*/
private generateOperationType;
/**
* Builds a complete GraphQL query string from the query builder.
* Constructs the operation, arguments, and field selection.
*
* @param queryBuilder - The query builder to build the query from
* @returns A complete GraphQL query string
* @private
*/
private buildGraphQLQuery;
/**
* Builds the field selection part of a GraphQL query.
* Determines which fields to include in the response.
*
* @param queryBuilder - The query builder to build fields from
* @returns A string of field names separated by newlines
* @private
*/
private buildFields;
/**
* Builds GraphQL variable declarations from the query builder.
* Creates the variable definitions for the GraphQL operation.
*
* @param queryBuilder - The query builder to build arguments from
* @returns Array of GraphQL variable declarations
* @private
*/
private buildArguments;
/**
* Builds GraphQL query arguments from the query builder.
* Creates the argument usage within the GraphQL operation.
*
* @param queryBuilder - The query builder to build query arguments from
* @returns A string of GraphQL query arguments
* @private
*/
private buildQueryArguments;
/**
* Builds GraphQL variables object from the query builder.
* Creates the variables that will be sent with the GraphQL request.
*
* @param queryBuilder - The query builder to build variables from
* @returns Object containing GraphQL variables
* @private
*/
private buildVariables;
/**
* Generates a unique operation name for the GraphQL query.
* Creates descriptive operation names based on the resource and HTTP method.
*
* @param queryBuilder - The query builder to generate the operation name from
* @returns A unique GraphQL operation name
* @private
*/
private generateOperationName;
}
/**
* Configuration options for the GraphQL adapter.
* Extends HttpAdapterOptions with GraphQL-specific configuration.
*
* @interface
* @extends {HttpAdapterOptions}
* @example
* ```typescript
* const options: GraphqlAdapterOptions = {
* baseUrl: 'https://api.example.com',
* endpoint: '/graphql',
* options: {
* headers: {
* 'Authorization': 'Bearer token'
* }
* }
* };
*
* const adapter = new GraphqlAdapter(options);
* ```
*/
export type GraphqlAdapterOptions = {
/** The GraphQL endpoint URL (required) */
endpoint?: string;
operationType?: GraphqlOperationType;
operationName?: string;
variables?: Record<string, any>;
fields?: string[];
} & HttpAdapterOptions;
/**
* GraphQL operation types.
*
* @enum
* @readonly
* @enummember {string} QUERY - GraphQL query operation
* @enummember {string} MUTATION - GraphQL mutation operation
* @enummember {string} SUBSCRIPTION - GraphQL subscription operation
*/
export declare enum GraphqlOperationType {
QUERY = "query",
MUTATION = "mutation",
SUBSCRIPTION = "subscription"
}
export type OperationType = keyof typeof GraphqlOperationType;