Skip to content

Commit a7fba3a

Browse files
authored
fix: create correct types for lists (#87)
1 parent e917fc3 commit a7fba3a

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/create-schema-customization/__tests__/build-types.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,30 @@ describe(`Build objectType`, () => {
203203
})
204204
})
205205
})
206+
207+
it(`creates correct types for lists`, () => {
208+
const gatsbyNodeDefs = createGatsbyNodeDefinitions([
209+
{
210+
remoteTypeName: `Category`,
211+
queries: `{
212+
categories {
213+
optionalListOfOptionalType
214+
optionalListOfRequiredType
215+
requiredListOfOptionalType
216+
requiredListOfRequiredType
217+
}
218+
}`,
219+
},
220+
])
221+
const context = createTestContext({ gatsbyNodeDefs })
222+
const categoryDef = buildTypeDefinition(context, `Category`)
223+
const categoryFields = (categoryDef as GatsbyGraphQLObjectType).config.fields
224+
225+
expect(categoryFields).toMatchObject({
226+
optionalListOfOptionalType: { type: `[TestApiCategory]` },
227+
optionalListOfRequiredType: { type: `[TestApiCategory!]` },
228+
requiredListOfOptionalType: { type: `[TestApiCategory]!` },
229+
requiredListOfRequiredType: { type: `[TestApiCategory!]!` },
230+
})
231+
})
206232
})

src/create-schema-customization/__tests__/fixtures/schema-blog.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ type Category implements Named {
3737
id: ID!
3838
entries: [Entry]
3939
displayName: String!
40+
41+
optionalListOfOptionalType: [Category]
42+
optionalListOfRequiredType: [Category!]
43+
requiredListOfOptionalType: [Category]!
44+
requiredListOfRequiredType: [Category!]!
4045
}
4146

4247
type Guest implements Entry & Named {

src/create-schema-customization/transform-fields/field-transformers.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,23 @@ function toGatsbyType(
169169
* i.e. wrapType(`JSON`, myRemoteListOfJSONType) => `[JSON]`
170170
*/
171171
function wrap(typeName: string, remoteType: GraphQLType): string {
172-
let wrappedType = typeName
172+
const wrappingTypes: GraphQLType[] = []
173173
let currentRemoteType = remoteType
174174
while (isWrappingType(currentRemoteType)) {
175-
if (isListType(currentRemoteType)) {
176-
wrappedType = `[${wrappedType}]`
177-
}
178-
if (isNonNullType(currentRemoteType)) {
175+
wrappingTypes.push(currentRemoteType)
176+
currentRemoteType = currentRemoteType.ofType
177+
}
178+
179+
let wrappedType = typeName
180+
for (const wrappingType of wrappingTypes.reverse()) {
181+
if (isNonNullType(wrappingType)) {
179182
wrappedType = `${wrappedType}!`
180183
}
181-
currentRemoteType = currentRemoteType.ofType
184+
if (isListType(wrappingType)) {
185+
wrappedType = `[${wrappedType}]`
186+
}
182187
}
188+
183189
return wrappedType
184190
}
185191

0 commit comments

Comments
 (0)