Skip to content

Commit

Permalink
fix(query): use field type for cursor value (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Mar 30, 2021
1 parent c08444f commit 964976c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion generator/templates/query.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@

{{/* If it's a unique ID, allow using these fields as cursor queries */}}
{{ if or ($field.IsID) ($field.IsUnique) }}
func (r {{ $struct }}) Cursor(cursor string) {{ $name }}CursorParam {
func (r {{ $struct }}) Cursor(cursor {{ $field.Type.Value }}) {{ $name }}CursorParam {
return {{ $name }}CursorParam{
data: builder.Field{
Name: "{{ $field.Name }}",
Expand Down
71 changes: 71 additions & 0 deletions test/projects/pagination/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,77 @@ func TestPagination(t *testing.T) {
},
}}

assert.Equal(t, expected, actual)
},
}, {
name: "int cursor",
// language=GraphQL
before: []string{`
mutation {
result: createOnePost(data: {
id: "a",
title: "a",
content: "a",
intTest: 3,
}) {
id
}
}
`, `
mutation {
result: createOnePost(data: {
id: "c",
title: "c",
content: "c",
intTest: 1,
}) {
id
}
}
`, `
mutation {
result: createOnePost(data: {
id: "b",
title: "b",
content: "b",
intTest: 2,
}) {
id
}
}
`},
run: func(t *testing.T, client *PrismaClient, ctx cx) {
actual, err := client.
Post.
FindMany().
OrderBy(
Post.IntTest.Order(DESC),
).
Cursor(Post.IntTest.Cursor(2)).
Exec(ctx)

if err != nil {
t.Fatalf("fail %s", err)
}

b := 2
c := 1
expected := []PostModel{{
InnerPost: InnerPost{
ID: "b",
Title: "b",
Content: "b",
IntTest: &b,
},
}, {
InnerPost: InnerPost{
ID: "c",
Title: "c",
Content: "c",
IntTest: &c,
},
}}

assert.Equal(t, expected, actual)
},
}}
Expand Down
1 change: 1 addition & 0 deletions test/projects/pagination/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ model Post {
id String @id @default(cuid())
title String @unique
content String
intTest Int? @unique
}

0 comments on commit 964976c

Please sign in to comment.