From c96aea88fe331b610471dd436c8de36844f3aecd Mon Sep 17 00:00:00 2001 From: Divyendu Singh Date: Fri, 9 Mar 2018 13:45:28 +0530 Subject: [PATCH 1/2] make graphql typing for getDescription conform with graphql-js --- types/graphql/index.d.ts | 4 ++++ types/graphql/utilities/buildASTSchema.d.ts | 13 +++++++++++-- types/graphql/utilities/index.d.ts | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/types/graphql/index.d.ts b/types/graphql/index.d.ts index a8bead58fd..49185e11a1 100644 --- a/types/graphql/index.d.ts +++ b/types/graphql/index.d.ts @@ -12,6 +12,7 @@ // Tim Griesser // Dylan Stewart // Alessio Dionisi +// Divyendu Singh // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -100,6 +101,9 @@ export { // Build a GraphQLSchema from a GraphQL schema language document. buildSchema, + // Get the description of an AST node + getDescription, + // Extends an existing GraphQLSchema from a parsed GraphQL Schema // language AST. extendSchema, diff --git a/types/graphql/utilities/buildASTSchema.d.ts b/types/graphql/utilities/buildASTSchema.d.ts index b20c8fc856..8ba2fcc9e9 100644 --- a/types/graphql/utilities/buildASTSchema.d.ts +++ b/types/graphql/utilities/buildASTSchema.d.ts @@ -1,7 +1,13 @@ -import { DocumentNode, Location } from '../language/ast'; +import { DocumentNode, Location, StringValueNode } from '../language/ast'; import { Source } from '../language/source'; import { GraphQLSchema } from '../type/schema'; +type BuildSchemaOptions = { + assumeValid?: boolean; + allowedLegacyNames?: ReadonlyArray; + commentDescriptions?: boolean; +}; + /** * This takes the ast of a schema document produced by the parse function in * src/language/parser.js. @@ -18,7 +24,10 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema; * Given an ast node, returns its string description based on a contiguous * block full-line of comments preceding it. */ -export function getDescription(node: { loc?: Location }): string; +export function getDescription( + node: { description?: StringValueNode; loc?: Location }, + options: BuildSchemaOptions + ): string; /** * A helper function to build a GraphQLSchema directly from a source diff --git a/types/graphql/utilities/index.d.ts b/types/graphql/utilities/index.d.ts index cd0e23948f..c2b2c182ff 100644 --- a/types/graphql/utilities/index.d.ts +++ b/types/graphql/utilities/index.d.ts @@ -27,7 +27,7 @@ export { getOperationAST } from './getOperationAST'; export { buildClientSchema } from './buildClientSchema'; // Build a GraphQLSchema from GraphQL Schema language. -export { buildASTSchema, buildSchema } from './buildASTSchema'; +export { buildASTSchema, buildSchema, getDescription } from './buildASTSchema'; // Extends an existing GraphQLSchema from a parsed GraphQL Schema language AST. export { extendSchema } from './extendSchema'; From b542e6c165dafbc53bea9e474fc731373f5a5494 Mon Sep 17 00:00:00 2001 From: Divyendu Singh Date: Sat, 10 Mar 2018 14:32:35 +0530 Subject: [PATCH 2/2] conform with structure of graphql-js in graphql typings --- types/graphql/type/schema.d.ts | 21 ++++++++++++++ types/graphql/utilities/buildASTSchema.d.ts | 32 ++++++++++++++------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/types/graphql/type/schema.d.ts b/types/graphql/type/schema.d.ts index 3263274dd9..26500c6ed3 100644 --- a/types/graphql/type/schema.d.ts +++ b/types/graphql/type/schema.d.ts @@ -61,6 +61,27 @@ export class GraphQLSchema { getDirective(name: string): GraphQLDirective; } +export type GraphQLSchemaValidationOptions = { + /** + * When building a schema from a GraphQL service's introspection result, it + * might be safe to assume the schema is valid. Set to true to assume the + * produced schema is valid. + * + * Default: false + */ + assumeValid?: boolean; + + /** + * If provided, the schema will consider fields or types with names included + * in this list valid, even if they do not adhere to the specification's + * schema validation rules. + * + * This option is provided to ease adoption and may be removed in a future + * major release. + */ + allowedLegacyNames?: ReadonlyArray; +}; + export interface GraphQLSchemaConfig { query: GraphQLObjectType; mutation?: GraphQLObjectType; diff --git a/types/graphql/utilities/buildASTSchema.d.ts b/types/graphql/utilities/buildASTSchema.d.ts index 8ba2fcc9e9..6b8aa56327 100644 --- a/types/graphql/utilities/buildASTSchema.d.ts +++ b/types/graphql/utilities/buildASTSchema.d.ts @@ -1,12 +1,17 @@ import { DocumentNode, Location, StringValueNode } from '../language/ast'; import { Source } from '../language/source'; -import { GraphQLSchema } from '../type/schema'; +import { GraphQLSchema, GraphQLSchemaValidationOptions } from '../type/schema'; -type BuildSchemaOptions = { - assumeValid?: boolean; - allowedLegacyNames?: ReadonlyArray; - commentDescriptions?: boolean; -}; +interface BuildSchemaOptions extends GraphQLSchemaValidationOptions { + /** + * Descriptions are defined as preceding string literals, however an older + * experimental version of the SDL supported preceding comments as + * descriptions. Set to true to enable this deprecated behavior. + * + * Default: false + */ + commentDescriptions?: boolean; +} /** * This takes the ast of a schema document produced by the parse function in @@ -21,13 +26,18 @@ type BuildSchemaOptions = { export function buildASTSchema(ast: DocumentNode): GraphQLSchema; /** - * Given an ast node, returns its string description based on a contiguous - * block full-line of comments preceding it. + * Given an ast node, returns its string description. + * + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * */ export function getDescription( - node: { description?: StringValueNode; loc?: Location }, - options: BuildSchemaOptions - ): string; + node: { description?: StringValueNode; loc?: Location }, + options: BuildSchemaOptions +): string; /** * A helper function to build a GraphQLSchema directly from a source