diff --git a/Changelog.md b/Changelog.md index bd78606..a5ea561 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,28 @@ # Changelog +## Master + +- Generate Argument Interfaces in `@gql2ts/from-schema` [#148](https://github.com/avantcredit/gql2ts/pull/148) + +For example: + +```graphql +type Query { + test(arg1: String, arg2: Boolean): String +} +``` + +will output + +```typescript +interface ITestOnQueryArguments { + arg1: string; + arg2: boolean; +} +``` + +- Only warn about prettier once in `@gql2ts/language-typescript` [#147](https://github.com/avantcredit/gql2ts/pull/147) + ## 1.4.3 - Fix 1.4.2 release diff --git a/__tests__/__snapshots__/from-schema-test.ts.snap b/__tests__/__snapshots__/from-schema-test.ts.snap index c619cd1..a123dbc 100644 --- a/__tests__/__snapshots__/from-schema-test.ts.snap +++ b/__tests__/__snapshots__/from-schema-test.ts.snap @@ -38,64 +38,64 @@ exports[`gql2ts Supports older TypeScript versions removes Nullability annotatio node: Node; } interface IAllFilmsOnRootArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IFilmOnRootArguments { -id: string; -filmID: string; +id?: string; +filmID?: string; } interface IAllPeopleOnRootArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IPersonOnRootArguments { -id: string; -personID: string; +id?: string; +personID?: string; } interface IAllPlanetsOnRootArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IPlanetOnRootArguments { -id: string; -planetID: string; +id?: string; +planetID?: string; } interface IAllSpeciesOnRootArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface ISpeciesOnRootArguments { -id: string; -speciesID: string; +id?: string; +speciesID?: string; } interface IAllStarshipsOnRootArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IStarshipOnRootArguments { -id: string; -starshipID: string; +id?: string; +starshipID?: string; } interface IAllVehiclesOnRootArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IVehicleOnRootArguments { -id: string; -vehicleID: string; +id?: string; +vehicleID?: string; } interface INodeOnRootArguments { /** @@ -221,34 +221,34 @@ full \\"{ edges { node } }\\" version should be used instead. id: string; } interface ISpeciesConnectionOnFilmArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IStarshipConnectionOnFilmArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IVehicleConnectionOnFilmArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface ICharacterConnectionOnFilmArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IPlanetConnectionOnFilmArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } /** @@ -377,16 +377,16 @@ have skin. id: string; } interface IPersonConnectionOnSpeciesArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IFilmConnectionOnSpeciesArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } /** @@ -451,16 +451,16 @@ of water. id: string; } interface IResidentConnectionOnPlanetArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IFilmConnectionOnPlanetArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } /** @@ -576,22 +576,22 @@ person does not have hair. id: string; } interface IFilmConnectionOnPersonArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IStarshipConnectionOnPersonArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IVehicleConnectionOnPersonArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } /** @@ -767,16 +767,16 @@ entire crew without having to resupply. id: string; } interface IPilotConnectionOnStarshipArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IFilmConnectionOnStarshipArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } /** @@ -985,16 +985,16 @@ entire crew without having to resupply. id: string; } interface IPilotConnectionOnVehicleArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } interface IFilmConnectionOnVehicleArguments { -after: string; -first: number; -before: string; -last: number; +after?: string; +first?: number; +before?: string; +last?: number; } /** diff --git a/packages/from-schema/src/index.ts b/packages/from-schema/src/index.ts index ee48d84..3f9c4b3 100644 --- a/packages/from-schema/src/index.ts +++ b/packages/from-schema/src/index.ts @@ -100,7 +100,7 @@ const run: (schemaInput: GraphQLSchema, optionsInput: IInternalOptions) => strin } function isInputField (field: GraphQLField | GraphQLInputField): field is GraphQLInputField { - return !!field.astNode && field.astNode.kind === 'InputValueDefinition'; + return (!!field.astNode && field.astNode.kind === 'InputValueDefinition') || !({}).hasOwnProperty.call(field, 'args'); } const buildDocTags: (field: GraphQLField | GraphQLInputField) => IJSDocTag[] = field => { @@ -164,24 +164,29 @@ const run: (schemaInput: GraphQLSchema, optionsInput: IInternalOptions) => strin interface IInterfaceMetadata { name: string; showNullabilityAttribute: boolean; + isNonNull: boolean; } type ExtractInterfaceMetadata = (interfaceName: string, supportsNullability: boolean) => IInterfaceMetadata; - const extractInterfaceMetadata: ExtractInterfaceMetadata = (interfaceName, supportsNullability) => ({ - name: interfaceName.replace(/\!/g, ''), - showNullabilityAttribute: !interfaceName.includes('!') && supportsNullability - }); + const extractInterfaceMetadata: ExtractInterfaceMetadata = (interfaceName, supportsNullability) => { + const isNonNull: boolean = interfaceName.includes('!'); + return { + isNonNull, + name: interfaceName.replace(/\!/g, ''), + showNullabilityAttribute: !isNonNull && supportsNullability + }; + }; type FieldToDefinition = (field: GraphQLField | GraphQLInputField, isInput: boolean, supportsNullability: boolean) => string; const fieldToDefinition: FieldToDefinition = (field, isInput, supportsNullability) => { - const { name, showNullabilityAttribute } = extractInterfaceMetadata( + const { name, showNullabilityAttribute, isNonNull } = extractInterfaceMetadata( resolveInterfaceName(field.type), supportsNullability ); return formatInput( field.name, - isInput && showNullabilityAttribute, + isInput && !isNonNull, printType(name, !showNullabilityAttribute) ); }; @@ -189,14 +194,14 @@ const run: (schemaInput: GraphQLSchema, optionsInput: IInternalOptions) => strin type ArgumentToDefinition = (arg: GraphQLArgument, supportsNullability: boolean) => string; const generateArgumentDeclaration: ArgumentToDefinition = (arg, supportsNullability) => { - const { name, showNullabilityAttribute } = extractInterfaceMetadata( + const { name, isNonNull, showNullabilityAttribute } = extractInterfaceMetadata( resolveInterfaceName(arg.type), supportsNullability ); return [ generateDescription(arg.description, buildDocTags(arg)), - formatInput(arg.name, showNullabilityAttribute, printType(name, !showNullabilityAttribute)) + formatInput(arg.name, !isNonNull, printType(name, !showNullabilityAttribute)) ].filter(Boolean).join('\n'); }; @@ -207,7 +212,7 @@ const run: (schemaInput: GraphQLSchema, optionsInput: IInternalOptions) => strin ) => string | null; const generateArgumentsDeclaration: ArgumentsToDefinition = (field, parentName, supportsNullability) => { - if (isInputField(field) || !field.args.length) { + if (isInputField(field) || !field.args || !field.args.length) { return null; } diff --git a/yarn.lock b/yarn.lock index eb24948..862f19c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3193,9 +3193,9 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" -"typescript@^2.4.1 <2.7.0": - version "2.6.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4" +typescript@^2.4.1: + version "2.7.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836" uglify-js@^2.6: version "2.8.29"