From 70de26a8414df4090f91091f797be7e3748bfa7e Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Wed, 14 Aug 2019 17:19:51 +0300 Subject: [PATCH] type definitions: Use consistent order for public fields --- src/type/definition.js | 49 +++++++++++++++++++++++------------------- src/type/schema.js | 9 ++++---- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/type/definition.js b/src/type/definition.js index f322f4d9d13..610151ba62e 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -663,9 +663,9 @@ export type GraphQLScalarTypeConfig = {| export class GraphQLObjectType { name: string; description: ?string; + isTypeOf: ?GraphQLIsTypeOfFn<*, *>; astNode: ?ObjectTypeDefinitionNode; extensionASTNodes: ?$ReadOnlyArray; - isTypeOf: ?GraphQLIsTypeOfFn<*, *>; _fields: Thunk>; _interfaces: Thunk>; @@ -673,9 +673,10 @@ export class GraphQLObjectType { constructor(config: GraphQLObjectTypeConfig<*, *>): void { this.name = config.name; this.description = config.description; + this.isTypeOf = config.isTypeOf; this.astNode = config.astNode; this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes); - this.isTypeOf = config.isTypeOf; + this._fields = defineFieldMap.bind(undefined, config); this._interfaces = defineInterfaces.bind(undefined, config); devAssert(typeof config.name === 'string', 'Must provide name.'); @@ -709,9 +710,9 @@ export class GraphQLObjectType { return { name: this.name, description: this.description, - isTypeOf: this.isTypeOf, interfaces: this.getInterfaces(), fields: fieldsToFieldsConfig(this.getFields()), + isTypeOf: this.isTypeOf, astNode: this.astNode, extensionASTNodes: this.extensionASTNodes || [], }; @@ -797,12 +798,12 @@ function isPlainObj(obj) { function fieldsToFieldsConfig(fields) { return mapValue(fields, field => ({ + description: field.description, type: field.type, args: argsToArgsConfig(field.args), resolve: field.resolve, subscribe: field.subscribe, deprecationReason: field.deprecationReason, - description: field.description, astNode: field.astNode, })); } @@ -814,9 +815,9 @@ export function argsToArgsConfig( args, arg => arg.name, arg => ({ + description: arg.description, type: arg.type, defaultValue: arg.defaultValue, - description: arg.description, astNode: arg.astNode, }), ); @@ -824,10 +825,10 @@ export function argsToArgsConfig( export type GraphQLObjectTypeConfig = {| name: string, + description?: ?string, interfaces?: Thunk>, fields: Thunk>, isTypeOf?: ?GraphQLIsTypeOfFn, - description?: ?string, astNode?: ?ObjectTypeDefinitionNode, extensionASTNodes?: ?$ReadOnlyArray, |}; @@ -874,21 +875,21 @@ export type GraphQLFieldConfig< TContext, TArgs = { [argument: string]: any, ... }, > = {| + description?: ?string, type: GraphQLOutputType, args?: GraphQLFieldConfigArgumentMap, resolve?: GraphQLFieldResolver, subscribe?: GraphQLFieldResolver, deprecationReason?: ?string, - description?: ?string, astNode?: ?FieldDefinitionNode, |}; export type GraphQLFieldConfigArgumentMap = ObjMap; export type GraphQLArgumentConfig = {| + description?: ?string, type: GraphQLInputType, defaultValue?: mixed, - description?: ?string, astNode?: ?InputValueDefinitionNode, |}; @@ -914,9 +915,9 @@ export type GraphQLField< export type GraphQLArgument = {| name: string, + description?: ?string, type: GraphQLInputType, defaultValue?: mixed, - description?: ?string, astNode?: ?InputValueDefinitionNode, |}; @@ -958,9 +959,10 @@ export class GraphQLInterfaceType { constructor(config: GraphQLInterfaceTypeConfig<*, *>): void { this.name = config.name; this.description = config.description; + this.resolveType = config.resolveType; this.astNode = config.astNode; this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes); - this.resolveType = config.resolveType; + this._fields = defineFieldMap.bind(undefined, config); devAssert(typeof config.name === 'string', 'Must provide name.'); devAssert( @@ -985,8 +987,8 @@ export class GraphQLInterfaceType { return { name: this.name, description: this.description, - resolveType: this.resolveType, fields: fieldsToFieldsConfig(this.getFields()), + resolveType: this.resolveType, astNode: this.astNode, extensionASTNodes: this.extensionASTNodes || [], }; @@ -1003,6 +1005,7 @@ defineToJSON(GraphQLInterfaceType); export type GraphQLInterfaceTypeConfig = {| name: string, + description?: ?string, fields: Thunk>, /** * Optionally provide a custom type resolver function. If one is not provided, @@ -1010,7 +1013,6 @@ export type GraphQLInterfaceTypeConfig = {| * Object type. */ resolveType?: ?GraphQLTypeResolver, - description?: ?string, astNode?: ?InterfaceTypeDefinitionNode, extensionASTNodes?: ?$ReadOnlyArray, |}; @@ -1041,18 +1043,19 @@ export type GraphQLInterfaceTypeConfig = {| export class GraphQLUnionType { name: string; description: ?string; + resolveType: ?GraphQLTypeResolver<*, *>; astNode: ?UnionTypeDefinitionNode; extensionASTNodes: ?$ReadOnlyArray; - resolveType: ?GraphQLTypeResolver<*, *>; _types: Thunk>; constructor(config: GraphQLUnionTypeConfig<*, *>): void { this.name = config.name; this.description = config.description; + this.resolveType = config.resolveType; this.astNode = config.astNode; this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes); - this.resolveType = config.resolveType; + this._types = defineTypes.bind(undefined, config); devAssert(typeof config.name === 'string', 'Must provide name.'); devAssert( @@ -1077,8 +1080,8 @@ export class GraphQLUnionType { return { name: this.name, description: this.description, - resolveType: this.resolveType, types: this.getTypes(), + resolveType: this.resolveType, astNode: this.astNode, extensionASTNodes: this.extensionASTNodes || [], }; @@ -1106,6 +1109,7 @@ function defineTypes( export type GraphQLUnionTypeConfig = {| name: string, + description?: ?string, types: Thunk>, /** * Optionally provide a custom type resolver function. If one is not provided, @@ -1113,7 +1117,6 @@ export type GraphQLUnionTypeConfig = {| * Object type. */ resolveType?: ?GraphQLTypeResolver, - description?: ?string, astNode?: ?UnionTypeDefinitionNode, extensionASTNodes?: ?$ReadOnlyArray, |}; @@ -1154,6 +1157,7 @@ export class GraphQLEnumType /* */ { this.description = config.description; this.astNode = config.astNode; this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes); + this._values = defineEnumValues(this.name, config.values); this._valueLookup = new Map( this._values.map(enumValue => [enumValue.value, enumValue]), @@ -1251,18 +1255,18 @@ function defineEnumValues( return { name: valueName, description: value.description, + value: 'value' in value ? value.value : valueName, isDeprecated: Boolean(value.deprecationReason), deprecationReason: value.deprecationReason, astNode: value.astNode, - value: 'value' in value ? value.value : valueName, }; }); } export type GraphQLEnumTypeConfig /* */ = {| name: string, - values: GraphQLEnumValueConfigMap /* */, description?: ?string, + values: GraphQLEnumValueConfigMap /* */, astNode?: ?EnumTypeDefinitionNode, extensionASTNodes?: ?$ReadOnlyArray, |}; @@ -1270,9 +1274,9 @@ export type GraphQLEnumTypeConfig /* */ = {| export type GraphQLEnumValueConfigMap /* */ = ObjMap */>; export type GraphQLEnumValueConfig /* */ = {| + description?: ?string, value?: any /* T */, deprecationReason?: ?string, - description?: ?string, astNode?: ?EnumValueDefinitionNode, |}; @@ -1318,6 +1322,7 @@ export class GraphQLInputObjectType { this.description = config.description; this.astNode = config.astNode; this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes); + this._fields = defineInputFieldMap.bind(undefined, config); devAssert(typeof config.name === 'string', 'Must provide name.'); } @@ -1375,9 +1380,9 @@ function defineInputFieldMap( return { name: fieldName, + description: fieldConfig.description, type: fieldConfig.type, defaultValue: fieldConfig.defaultValue, - description: fieldConfig.description, astNode: fieldConfig.astNode, }; }); @@ -1385,16 +1390,16 @@ function defineInputFieldMap( export type GraphQLInputObjectTypeConfig = {| name: string, - fields: Thunk, description?: ?string, + fields: Thunk, astNode?: ?InputObjectTypeDefinitionNode, extensionASTNodes?: ?$ReadOnlyArray, |}; export type GraphQLInputFieldConfig = {| + description?: ?string, type: GraphQLInputType, defaultValue?: mixed, - description?: ?string, astNode?: ?InputValueDefinitionNode, |}; diff --git a/src/type/schema.js b/src/type/schema.js index 070c915555b..516c5180a0b 100644 --- a/src/type/schema.js +++ b/src/type/schema.js @@ -156,14 +156,15 @@ export class GraphQLSchema { ); } + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + this.__allowedLegacyNames = config.allowedLegacyNames || []; this._queryType = config.query; this._mutationType = config.mutation; this._subscriptionType = config.subscription; // Provide specified directives (e.g. @include and @skip) by default. this._directives = config.directives || specifiedDirectives; - this.astNode = config.astNode; - this.extensionASTNodes = config.extensionASTNodes; // Build type map now to detect any errors within this schema. const initialTypes: Array = [ @@ -266,11 +267,11 @@ export class GraphQLSchema { allowedLegacyNames: $ReadOnlyArray, |} { return { - types: objectValues(this.getTypeMap()), - directives: this.getDirectives().slice(), query: this.getQueryType(), mutation: this.getMutationType(), subscription: this.getSubscriptionType(), + types: objectValues(this.getTypeMap()), + directives: this.getDirectives().slice(), astNode: this.astNode, extensionASTNodes: this.extensionASTNodes || [], assumeValid: this.__validationErrors !== undefined,