From ea989d5f7d3fd3bbf24c69452d512d99095f4ac8 Mon Sep 17 00:00:00 2001 From: Daniel Rearden Date: Wed, 9 Dec 2020 10:38:24 -0500 Subject: [PATCH] SDLConverter fixes (#717) --- src/sdlConverter.ts | 27 +++---- tests/__snapshots__/sdlConverter.spec.ts.snap | 76 +++++++++---------- 2 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/sdlConverter.ts b/src/sdlConverter.ts index f9f0d7a9..4a0711be 100644 --- a/src/sdlConverter.ts +++ b/src/sdlConverter.ts @@ -114,17 +114,13 @@ export class SDLConverter { let typeString: string | undefined = undefined - if (wrapping.length > 2) { - typeString = this.addWrapping(namedType.name, wrapping) - } else { - ;[...wrapping].reverse().forEach((w) => { - if (w === 'List') { - prefix += `list.` - } else { - prefix += `nonNull.` - } - }) - } + ;[...wrapping].reverse().forEach((w) => { + if (w === 'List') { + prefix += `list.` + } else { + prefix += `nonNull.` + } + }) return ` ${prefix}${this.printFieldMethod(source, field, namedType, typeString)}` } @@ -185,7 +181,7 @@ export class SDLConverter { if (key === 'args') { let str = `{\n` ;(val as GraphQLArgument[]).forEach((arg) => { - str += ` ${arg.name}: ${this.printArg(arg)}\n` + str += ` ${arg.name}: ${this.printArg(arg)},\n` }) str += ` }` return str @@ -195,6 +191,7 @@ export class SDLConverter { printArg(arg: GraphQLArgument) { const description = arg.description + const defaultValue = arg.defaultValue const { namedType: type, wrapping } = unwrapGraphQLDef(arg.type) const isArg = !isSpecifiedScalarType(type) let str = '' @@ -203,6 +200,7 @@ export class SDLConverter { str += `arg(` } else { this.usedImports.add(`${type.toString().toLowerCase()}Arg`) + str += `${type.toString().toLowerCase()}Arg(` } const metaToAdd = [] @@ -214,12 +212,15 @@ export class SDLConverter { if (description) { metaToAdd.push(`description: ${JSON.stringify(description)}`) } + if (defaultValue) { + metaToAdd.push(`default: ${JSON.stringify(defaultValue)}`) + } str += metaToAdd.length > 1 ? `{\n ${metaToAdd.join(',\n ')}\n })` : metaToAdd.length ? `{ ${metaToAdd[0]} })` - : '' + : ')' return isArg ? str : this.addWrapping(str, wrapping) } diff --git a/tests/__snapshots__/sdlConverter.spec.ts.snap b/tests/__snapshots__/sdlConverter.spec.ts.snap index e0c012da..87679fc2 100644 --- a/tests/__snapshots__/sdlConverter.spec.ts.snap +++ b/tests/__snapshots__/sdlConverter.spec.ts.snap @@ -17,19 +17,19 @@ exports[`SDLConverter printObjectTypes 1`] = ` definition(t) { t.nonNull.list.string(\\"someList\\", { args: { - items: nonNull(list(stringArg()) + items: nonNull(list(stringArg())), }, }) t.nonNull.field(\\"createPost\\", { type: Post, args: { - input: arg({ type: nonNull(CreatePostInput) }) + input: arg({ type: nonNull(CreatePostInput) }), }, }) t.nonNull.field(\\"registerClick\\", { type: Query, args: { - uuid: arg({ type: UUID }) + uuid: arg({ type: UUID }), }, }) } @@ -41,18 +41,18 @@ export const Post = objectType({ t.implements(Node) t.nonNull.uuid(\\"uuid\\") t.nonNull.field(\\"author\\", { type: User }) - t.field(\\"geo\\", { type: nonNull(list(nonNull(list(nonNull(Float))))) }) - t.field(\\"messyGeo\\", { type: list(list(nonNull(Float))) }) + t.nonNull.list.nonNull.list.nonNull.float(\\"geo\\") + t.list.list.nonNull.float(\\"messyGeo\\") } }) export const Query = objectType({ name: \\"Query\\", definition(t) { t.nonNull.field(\\"user\\", { type: User }) - t.field(\\"posts\\", { - type: nonNull(list(nonNull(Post))), + t.nonNull.list.nonNull.field(\\"posts\\", { + type: Post, args: { - filters: arg({ type: nonNull(PostFilters) }) + filters: arg({ type: nonNull(PostFilters) }), }, }) t.nonNull.field(\\"unionField\\", { type: ExampleUnion }) @@ -65,15 +65,15 @@ export const User = objectType({ t.nonNull.string(\\"name\\", { description: \\"This is a description of a name\\", args: { - prefix: stringArg({ description: \\"And a description of an arg\\" }) + prefix: stringArg({ description: \\"And a description of an arg\\" }), }, }) t.nonNull.string(\\"email\\") t.string(\\"phone\\") - t.field(\\"posts\\", { - type: nonNull(list(nonNull(Post))), + t.nonNull.list.nonNull.field(\\"posts\\", { + type: Post, args: { - filters: arg({ type: PostFilters }) + filters: arg({ type: PostFilters }), }, }) t.field(\\"outEnum\\", { type: SomeEnum }) @@ -89,19 +89,19 @@ export const Mutation = objectType({ definition(t) { t.nonNull.list.string(\\"someList\\", { args: { - items: nonNull(list(stringArg()) + items: nonNull(list(stringArg())), }, }) t.nonNull.field(\\"createPost\\", { type: Post, args: { - input: arg({ type: nonNull(CreatePostInput) }) + input: arg({ type: nonNull(CreatePostInput) }), }, }) t.nonNull.field(\\"registerClick\\", { type: Query, args: { - uuid: arg({ type: UUID }) + uuid: arg({ type: UUID }), }, }) } @@ -113,18 +113,18 @@ export const Post = objectType({ t.implements(Node) t.nonNull.uuid(\\"uuid\\") t.nonNull.field(\\"author\\", { type: User }) - t.field(\\"geo\\", { type: nonNull(list(nonNull(list(nonNull(Float))))) }) - t.field(\\"messyGeo\\", { type: list(list(nonNull(Float))) }) + t.nonNull.list.nonNull.list.nonNull.float(\\"geo\\") + t.list.list.nonNull.float(\\"messyGeo\\") } }) export const Query = objectType({ name: \\"Query\\", definition(t) { t.nonNull.field(\\"user\\", { type: User }) - t.field(\\"posts\\", { - type: nonNull(list(nonNull(Post))), + t.nonNull.list.nonNull.field(\\"posts\\", { + type: Post, args: { - filters: arg({ type: nonNull(PostFilters) }) + filters: arg({ type: nonNull(PostFilters) }), }, }) t.nonNull.field(\\"unionField\\", { type: ExampleUnion }) @@ -137,15 +137,15 @@ export const User = objectType({ t.nonNull.string(\\"name\\", { description: \\"This is a description of a name\\", args: { - prefix: stringArg({ description: \\"And a description of an arg\\" }) + prefix: stringArg({ description: \\"And a description of an arg\\" }), }, }) t.nonNull.string(\\"email\\") t.string(\\"phone\\") - t.field(\\"posts\\", { - type: nonNull(list(nonNull(Post))), + t.nonNull.list.nonNull.field(\\"posts\\", { + type: Post, args: { - filters: arg({ type: PostFilters }) + filters: arg({ type: PostFilters }), }, }) t.field(\\"outEnum\\", { type: SomeEnum }) @@ -165,7 +165,7 @@ export const CreatePostInput = inputObjectType({ definition(t) { t.nonNull.string(\\"name\\") t.nonNull.id(\\"author\\") - t.field(\\"geo\\", { type: nonNull(list(nonNull(list(Float)))) }) + t.nonNull.list.nonNull.list.float(\\"geo\\") } }); export const PostFilters = inputObjectType({ @@ -209,19 +209,19 @@ const Mutation = objectType({ definition(t) { t.nonNull.list.string(\\"someList\\", { args: { - items: nonNull(list(stringArg()) + items: nonNull(list(stringArg())), }, }) t.nonNull.field(\\"createPost\\", { type: Post, args: { - input: arg({ type: nonNull(CreatePostInput) }) + input: arg({ type: nonNull(CreatePostInput) }), }, }) t.nonNull.field(\\"registerClick\\", { type: Query, args: { - uuid: arg({ type: UUID }) + uuid: arg({ type: UUID }), }, }) } @@ -233,18 +233,18 @@ const Post = objectType({ t.implements(Node) t.nonNull.uuid(\\"uuid\\") t.nonNull.field(\\"author\\", { type: User }) - t.field(\\"geo\\", { type: nonNull(list(nonNull(list(nonNull(Float))))) }) - t.field(\\"messyGeo\\", { type: list(list(nonNull(Float))) }) + t.nonNull.list.nonNull.list.nonNull.float(\\"geo\\") + t.list.list.nonNull.float(\\"messyGeo\\") } }) const Query = objectType({ name: \\"Query\\", definition(t) { t.nonNull.field(\\"user\\", { type: User }) - t.field(\\"posts\\", { - type: nonNull(list(nonNull(Post))), + t.nonNull.list.nonNull.field(\\"posts\\", { + type: Post, args: { - filters: arg({ type: nonNull(PostFilters) }) + filters: arg({ type: nonNull(PostFilters) }), }, }) t.nonNull.field(\\"unionField\\", { type: ExampleUnion }) @@ -257,15 +257,15 @@ const User = objectType({ t.nonNull.string(\\"name\\", { description: \\"This is a description of a name\\", args: { - prefix: stringArg({ description: \\"And a description of an arg\\" }) + prefix: stringArg({ description: \\"And a description of an arg\\" }), }, }) t.nonNull.string(\\"email\\") t.string(\\"phone\\") - t.field(\\"posts\\", { - type: nonNull(list(nonNull(Post))), + t.nonNull.list.nonNull.field(\\"posts\\", { + type: Post, args: { - filters: arg({ type: PostFilters }) + filters: arg({ type: PostFilters }), }, }) t.field(\\"outEnum\\", { type: SomeEnum }) @@ -285,7 +285,7 @@ const CreatePostInput = inputObjectType({ definition(t) { t.nonNull.string(\\"name\\") t.nonNull.id(\\"author\\") - t.field(\\"geo\\", { type: nonNull(list(nonNull(list(Float)))) }) + t.nonNull.list.nonNull.list.float(\\"geo\\") } }); const PostFilters = inputObjectType({