Skip to content

Commit 8c78e97

Browse files
authored
fix(graphql-types-generator): correct scalar and enum type in angular (#257)
* fix(graphql-types-generator): correct scalar and enum type in angular * test: add unit test * fix: rm unused schema
1 parent f9fb70f commit 8c78e97

File tree

4 files changed

+248
-3
lines changed

4 files changed

+248
-3
lines changed

packages/graphql-types-generator/src/angular/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLNonNull, GraphQLType, isScalarType } from 'graphql';
1+
import { GraphQLNonNull, GraphQLType, isScalarType, isEnumType, isListType } from 'graphql';
22
import * as prettier from 'prettier';
33
import { LegacyCompilerContext, LegacyOperation, LegacyInlineFragment, LegacyField } from '../compiler/legacyIR';
44

@@ -128,9 +128,19 @@ function getOperationResultField(operation: LegacyOperation): LegacyField | void
128128
function getReturnTypeName(generator: CodeGenerator, op: LegacyOperation): String {
129129
const { operationName, operationType } = op;
130130
const { type } = op.fields[0];
131-
if (isScalarType(type)) {
131+
//List of scalar or enum type
132+
if (isListType(type)) {
133+
const { ofType } = type;
134+
if (isScalarType(ofType) || isEnumType(ofType)) {
135+
return `Array<${typeNameFromGraphQLType(generator.context, ofType)}>`;
136+
}
137+
}
138+
//Scalar and enum type
139+
if (isScalarType(type) || isEnumType(type)) {
132140
return typeNameFromGraphQLType(generator.context, type);
133-
} else {
141+
}
142+
//Non scalar type
143+
else {
134144
let returnType = interfaceNameFromOperation({ operationName, operationType });
135145
if (op.operationType === 'subscription' && op.fields.length) {
136146
returnType = `Pick<__SubscriptionContainer, "${op.fields[0].responseName}">`;

packages/graphql-types-generator/test/angular/__snapshots__/index.js.snap

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,60 @@ export class APIService {
838838
"
839839
`;
840840
841+
exports[`Angular code generation #generateSource() should generate simple query operation with scalar/enum field and scalar/enum return type w & w/o list 1`] = `
842+
"/* tslint:disable */
843+
/* eslint-disable */
844+
// This file was automatically generated and should not be edited.
845+
import { Injectable } from \\"@angular/core\\";
846+
import API, { graphqlOperation, GraphQLResult } from \\"@aws-amplify/api-graphql\\";
847+
import { Observable } from \\"zen-observable-ts\\";
848+
849+
export interface SubscriptionResponse<T> {
850+
value: GraphQLResult<T>;
851+
}
852+
853+
export enum Status {
854+
APPROVED = \\"APPROVED\\",
855+
PENDING = \\"PENDING\\",
856+
REJECTED = \\"REJECTED\\"
857+
}
858+
859+
@Injectable({
860+
providedIn: \\"root\\"
861+
})
862+
export class APIService {
863+
async GetScalars(): Promise<Array<string | null>> {
864+
const statement = \`query GetScalars {
865+
getScalars
866+
}\`;
867+
const response = (await API.graphql(graphqlOperation(statement))) as any;
868+
return <Array<string | null>>response.data.getScalars;
869+
}
870+
async GetScalar(): Promise<string | null> {
871+
const statement = \`query GetScalar {
872+
getScalar
873+
}\`;
874+
const response = (await API.graphql(graphqlOperation(statement))) as any;
875+
return <string | null>response.data.getScalar;
876+
}
877+
async GetEnums(): Promise<Array<Status | null>> {
878+
const statement = \`query GetEnums {
879+
getEnums
880+
}\`;
881+
const response = (await API.graphql(graphqlOperation(statement))) as any;
882+
return <Array<Status | null>>response.data.getEnums;
883+
}
884+
async GetEnum(): Promise<Status | null> {
885+
const statement = \`query GetEnum {
886+
getEnum
887+
}\`;
888+
const response = (await API.graphql(graphqlOperation(statement))) as any;
889+
return <Status | null>response.data.getEnum;
890+
}
891+
}
892+
"
893+
`;
894+
841895
exports[`Angular code generation #generateSource() should generate simple query operations 1`] = `
842896
"/* tslint:disable */
843897
/* eslint-disable */

packages/graphql-types-generator/test/angular/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,25 @@ describe('Angular code generation', function() {
391391
const source = generateSource(context);
392392
expect(source).toMatchSnapshot();
393393
});
394+
395+
test(`should generate simple query operation with scalar/enum field and scalar/enum return type w & w/o list`, function() {
396+
const { compileFromSource } = setup(loadSchema(require.resolve('../fixtures/misc/queryWithScalarAndEnumType.graphql')));
397+
const context = compileFromSource(`
398+
query GetScalars {
399+
getScalars
400+
}
401+
query GetScalar {
402+
getScalar
403+
}
404+
query GetEnums {
405+
getEnums
406+
}
407+
query GetEnum {
408+
getEnum
409+
}
410+
`);
411+
const source = generateSource(context);
412+
expect(source).toMatchSnapshot();
413+
});
394414
});
395415
});
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
type Person {
2+
id: ID!
3+
name: String!
4+
items: [String!]
5+
createdAt: AWSDateTime!
6+
updatedAt: AWSDateTime!
7+
}
8+
9+
type Query {
10+
getScalars: [String]
11+
getScalar: String
12+
getEnums: [Status]
13+
getEnum: Status
14+
getPerson(id: ID!): Person
15+
listPersons(filter: ModelPersonFilterInput, limit: Int, nextToken: String): ModelPersonConnection
16+
}
17+
18+
enum Status {
19+
APPROVED
20+
PENDING
21+
REJECTED
22+
}
23+
24+
enum ModelSortDirection {
25+
ASC
26+
DESC
27+
}
28+
29+
type ModelPersonConnection {
30+
items: [Person]
31+
nextToken: String
32+
}
33+
34+
input ModelStringInput {
35+
ne: String
36+
eq: String
37+
le: String
38+
lt: String
39+
ge: String
40+
gt: String
41+
contains: String
42+
notContains: String
43+
between: [String]
44+
beginsWith: String
45+
attributeExists: Boolean
46+
attributeType: ModelAttributeTypes
47+
size: ModelSizeInput
48+
}
49+
50+
input ModelIDInput {
51+
ne: ID
52+
eq: ID
53+
le: ID
54+
lt: ID
55+
ge: ID
56+
gt: ID
57+
contains: ID
58+
notContains: ID
59+
between: [ID]
60+
beginsWith: ID
61+
attributeExists: Boolean
62+
attributeType: ModelAttributeTypes
63+
size: ModelSizeInput
64+
}
65+
66+
input ModelIntInput {
67+
ne: Int
68+
eq: Int
69+
le: Int
70+
lt: Int
71+
ge: Int
72+
gt: Int
73+
between: [Int]
74+
attributeExists: Boolean
75+
attributeType: ModelAttributeTypes
76+
}
77+
78+
input ModelFloatInput {
79+
ne: Float
80+
eq: Float
81+
le: Float
82+
lt: Float
83+
ge: Float
84+
gt: Float
85+
between: [Float]
86+
attributeExists: Boolean
87+
attributeType: ModelAttributeTypes
88+
}
89+
90+
input ModelBooleanInput {
91+
ne: Boolean
92+
eq: Boolean
93+
attributeExists: Boolean
94+
attributeType: ModelAttributeTypes
95+
}
96+
97+
input ModelSizeInput {
98+
ne: Int
99+
eq: Int
100+
le: Int
101+
lt: Int
102+
ge: Int
103+
gt: Int
104+
between: [Int]
105+
}
106+
107+
input ModelPersonFilterInput {
108+
name: ModelStringInput
109+
items: ModelStringInput
110+
and: [ModelPersonFilterInput]
111+
or: [ModelPersonFilterInput]
112+
not: ModelPersonFilterInput
113+
}
114+
115+
enum ModelAttributeTypes {
116+
binary
117+
binarySet
118+
bool
119+
list
120+
map
121+
number
122+
numberSet
123+
string
124+
stringSet
125+
_null
126+
}
127+
128+
input CreatePersonInput {
129+
id: ID
130+
name: String!
131+
items: [String!]
132+
}
133+
134+
input UpdatePersonInput {
135+
name: String
136+
items: [String!]
137+
}
138+
139+
input DeletePersonInput {
140+
id: ID!
141+
}
142+
143+
type Mutation {
144+
createPerson(input: CreatePersonInput!, condition: ModelPersonConditionInput): Person
145+
updatePerson(input: UpdatePersonInput!, condition: ModelPersonConditionInput): Person
146+
deletePerson(input: DeletePersonInput!, condition: ModelPersonConditionInput): Person
147+
}
148+
149+
input ModelPersonConditionInput {
150+
name: ModelStringInput
151+
items: ModelStringInput
152+
and: [ModelPersonConditionInput]
153+
or: [ModelPersonConditionInput]
154+
not: ModelPersonConditionInput
155+
}
156+
157+
type Subscription {
158+
onCreatePerson: Person @aws_subscribe(mutations: ["createPerson"])
159+
onUpdatePerson: Person @aws_subscribe(mutations: ["updatePerson"])
160+
onDeletePerson: Person @aws_subscribe(mutations: ["deletePerson"])
161+
}

0 commit comments

Comments
 (0)