Skip to content

Commit

Permalink
fix input field detection & args (#149)
Browse files Browse the repository at this point in the history
* fix input field detection & args

* add parentheses

* fix nullability

* proper undefined attributes

* fix for optional args

* update snapshot
  • Loading branch information
brettjurgens committed Mar 9, 2018
1 parent cec7941 commit aab925e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 113 deletions.
23 changes: 23 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
200 changes: 100 additions & 100 deletions __tests__/__snapshots__/from-schema-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
25 changes: 15 additions & 10 deletions packages/from-schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const run: (schemaInput: GraphQLSchema, optionsInput: IInternalOptions) => strin
}

function isInputField (field: GraphQLField<any, any> | 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<any, any> | GraphQLInputField) => IJSDocTag[] = field => {
Expand Down Expand Up @@ -164,39 +164,44 @@ 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<any, any> | 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)
);
};

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');
};

Expand All @@ -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;
}

Expand Down

0 comments on commit aab925e

Please sign in to comment.