Skip to content

Commit

Permalink
SDLConverter fixes (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrearden authored Dec 9, 2020
1 parent 1da85cf commit ea989d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
27 changes: 14 additions & 13 deletions src/sdlConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
}
Expand Down Expand Up @@ -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
Expand All @@ -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 = ''
Expand All @@ -203,6 +200,7 @@ export class SDLConverter {
str += `arg(`
} else {
this.usedImports.add(`${type.toString().toLowerCase()}Arg`)

str += `${type.toString().toLowerCase()}Arg(`
}
const metaToAdd = []
Expand All @@ -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)
}
Expand Down
76 changes: 38 additions & 38 deletions tests/__snapshots__/sdlConverter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
},
})
}
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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 }),
},
})
}
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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({
Expand Down Expand Up @@ -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 }),
},
})
}
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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({
Expand Down

0 comments on commit ea989d5

Please sign in to comment.