Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronise TS typings for graphql.js/execute.js/subscribe.js #2987

Merged
merged 1 commit into from Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 39 additions & 19 deletions src/execution/execute.d.ts
Expand Up @@ -22,6 +22,26 @@ import {
GraphQLObjectType,
} from '../type/definition';

/**
* Terminology
*
* "Definitions" are the generic name for top-level statements in the document.
* Examples of this include:
* 1) Operations (such as a query)
* 2) Fragments
*
* "Operations" are a generic name for requests in the document.
* Examples of this include:
* 1) query,
* 2) mutation
*
* "Selections" are the definitions that can appear legally and at
* single level of the query. These include:
* 1) field references e.g "a"
* 2) fragment "spreads" e.g. "...c"
* 3) inline fragment "spreads" e.g. "...on Type { a }"
*/

/**
* Data that must be available at all points during query execution.
*
Expand All @@ -36,6 +56,7 @@ export interface ExecutionContext {
operation: OperationDefinitionNode;
variableValues: { [key: string]: any };
fieldResolver: GraphQLFieldResolver<any, any>;
typeResolver: GraphQLTypeResolver<any, any>;
errors: Array<GraphQLError>;
}

Expand Down Expand Up @@ -86,20 +107,8 @@ export interface ExecutionArgs {
*
* If the arguments to this function do not result in a legal execution context,
* a GraphQLError will be thrown immediately explaining the invalid input.
*
* Accepts either an object with named arguments, or individual arguments.
*/
export function execute(args: ExecutionArgs): PromiseOrValue<ExecutionResult>;
export function execute(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: any,
contextValue?: any,
variableValues?: Maybe<{ [key: string]: any }>,
operationName?: Maybe<string>,
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
): PromiseOrValue<ExecutionResult>;

/**
* Also implements the "Evaluating requests" section of the GraphQL specification.
Expand All @@ -111,6 +120,8 @@ export function executeSync(args: ExecutionArgs): ExecutionResult;
/**
* Essential assertions before executing to provide developer feedback for
* improper use of the GraphQL library.
*
* @internal
*/
export function assertValidExecutionArguments(
schema: GraphQLSchema,
Expand All @@ -123,6 +134,8 @@ export function assertValidExecutionArguments(
* execute, which we will pass throughout the other execution methods.
*
* Throws a GraphQLError if a valid execution context cannot be created.
*
* @internal
*/
export function buildExecutionContext(
schema: GraphQLSchema,
Expand All @@ -142,6 +155,8 @@ export function buildExecutionContext(
* CollectFields requires the "runtime type" of an object. For a field which
* returns an Interface or Union type, the "runtime type" will be the actual
* Object type returned by that field.
*
* @internal
*/
export function collectFields(
exeContext: ExecutionContext,
Expand All @@ -151,6 +166,9 @@ export function collectFields(
visitedFragmentNames: { [key: string]: boolean },
): { [key: string]: Array<FieldNode> };

/**
* @internal
*/
export function buildResolveInfo(
exeContext: ExecutionContext,
fieldDef: GraphQLField<any, any>,
Expand All @@ -175,18 +193,20 @@ export const defaultTypeResolver: GraphQLTypeResolver<any, any>;
* If a resolve function is not given, then a default resolve behavior is used
* which takes the property of the source object of the same name as the field
* and returns it as the result, or if it's a function, returns the result
* of calling that function while passing along args and context.
* of calling that function while passing along args and context value.
*/
export const defaultFieldResolver: GraphQLFieldResolver<any, any>;

/**
* This method looks up the field on the given type definition.
* It has special casing for the two introspection fields, __schema
* and __typename. __typename is special because it can always be
* queried as a field, even in situations where no other fields
* are allowed, like on a Union. __schema could get automatically
* added to the query type, but that would require mutating type
* definitions, which would cause issues.
* It has special casing for the three introspection fields,
* __schema, __type and __typename. __typename is special because
* it can always be queried as a field, even in situations where no
* other fields are allowed, like on a Union. __schema and __type
* could get automatically added to the query type, but that would
* require mutating type definitions, which would cause issues.
*
* @internal
*/
export function getFieldDef(
schema: GraphQLSchema,
Expand Down
24 changes: 4 additions & 20 deletions src/graphql.d.ts
Expand Up @@ -39,6 +39,10 @@ import { ExecutionResult } from './execution/execute';
* A resolver function to use when one is not provided by the schema.
* If not provided, the default field resolver is used (which looks for a
* value or method on the source value with the field's name).
* typeResolver:
* A type resolver function to use when none is provided by the schema.
* If not provided, the default type resolver is used (which looks for a
* `__typename` field or alternatively calls the `isTypeOf` method).
*/
export interface GraphQLArgs {
schema: GraphQLSchema;
Expand All @@ -52,16 +56,6 @@ export interface GraphQLArgs {
}

export function graphql(args: GraphQLArgs): Promise<ExecutionResult>;
export function graphql(
schema: GraphQLSchema,
source: Source | string,
rootValue?: any,
contextValue?: any,
variableValues?: Maybe<{ [key: string]: any }>,
operationName?: Maybe<string>,
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
): Promise<ExecutionResult>;

/**
* The graphqlSync function also fulfills GraphQL operations by parsing,
Expand All @@ -70,13 +64,3 @@ export function graphql(
* that all field resolvers are also synchronous.
*/
export function graphqlSync(args: GraphQLArgs): ExecutionResult;
export function graphqlSync(
schema: GraphQLSchema,
source: Source | string,
rootValue?: any,
contextValue?: any,
variableValues?: Maybe<{ [key: string]: any }>,
operationName?: Maybe<string>,
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>,
): ExecutionResult;
36 changes: 18 additions & 18 deletions src/subscription/subscribe.d.ts
Expand Up @@ -20,14 +20,15 @@ export interface SubscriptionArgs {
* Implements the "Subscribe" algorithm described in the GraphQL specification.
*
* Returns a Promise which resolves to either an AsyncIterator (if successful)
* or an ExecutionResult (client error). The promise will be rejected if a
* server error occurs.
* or an ExecutionResult (error). The promise will be rejected if the schema or
* other arguments to this function are invalid, or if the resolved event stream
* is not an async iterable.
*
* If the client-provided arguments to this function do not result in a
* compliant subscription, a GraphQL Response (ExecutionResult) with
* descriptive errors and no data will be returned.
*
* If the the source stream could not be created due to faulty subscription
* If the source stream could not be created due to faulty subscription
* resolver logic or underlying systems, the promise will resolve to a single
* ExecutionResult containing `errors` and no `data`.
*
Expand All @@ -40,26 +41,25 @@ export function subscribe(
args: SubscriptionArgs,
): Promise<AsyncIterableIterator<ExecutionResult> | ExecutionResult>;

export function subscribe(
schema: GraphQLSchema,
document: DocumentNode,
rootValue?: any,
contextValue?: any,
variableValues?: Maybe<{ [key: string]: any }>,
operationName?: Maybe<string>,
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>,
): Promise<AsyncIterableIterator<ExecutionResult> | ExecutionResult>;

/**
* Implements the "CreateSourceEventStream" algorithm described in the
* GraphQL specification, resolving the subscription source event stream.
*
* Returns a Promise<AsyncIterable>.
* Returns a Promise which resolves to either an AsyncIterable (if successful)
* or an ExecutionResult (error). The promise will be rejected if the schema or
* other arguments to this function are invalid, or if the resolved event stream
* is not an async iterable.
*
* If the client-provided arguments to this function do not result in a
* compliant subscription, a GraphQL Response (ExecutionResult) with
* descriptive errors and no data will be returned.
*
* If the the source stream could not be created due to faulty subscription
* resolver logic or underlying systems, the promise will resolve to a single
* ExecutionResult containing `errors` and no `data`.
*
* If the client-provided invalid arguments, the source stream could not be
* created, or the resolver did not return an AsyncIterable, this function will
* will throw an error, which should be caught and handled by the caller.
* If the operation succeeded, the promise resolves to the AsyncIterable for the
* event stream returned by the resolver.
*
* A Source Event Stream represents a sequence of events, each of which triggers
* a GraphQL execution for that event.
Expand Down