Skip to content

Commit

Permalink
tests: relation cover 1-to-n composite IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed May 17, 2021
1 parent 767d2d8 commit 2b636ce
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
34 changes: 34 additions & 0 deletions tests/integration/__snapshots__/relation1ToN.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`can project user-to-posts relation where user has composite ID: graphqlOperationExecutionResult 1`] = `
Object {
"data": Object {
"users": Array [
Object {
"id1": "user1",
"posts": Array [
Object {
"id": "post1",
},
],
},
],
},
}
`;

exports[`can project user-to-posts relation where user has composite ID: graphqlSchemaSDL 1`] = `
"
type Query {
users: [User!]!
}
type User {
id1: String!
posts: [Post!]!
}
type Post {
id: ID!
}
"
`;

exports[`can project user-to-posts relationship in reverse (access use via post author field): graphqlOperationExecutionResult 1`] = `
Object {
"data": Object {
Expand Down
75 changes: 70 additions & 5 deletions tests/integration/relation1ToN.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import endent from 'endent'
import { gql } from 'graphql-tag'
import { objectType, queryType } from 'nexus'
import { testIntegration } from '../__helpers__'

testIntegration({
description: 'can project user-to-posts relationship',
datasourceSchema: endent`
datasourceSchema: `
model User {
id String @id
posts Post[]
id String @id
posts Post[]
}
model Post {
id String @id
Expand Down Expand Up @@ -67,7 +66,7 @@ testIntegration({

testIntegration({
description: 'can project user-to-posts relationship in reverse (access use via post author field)',
datasourceSchema: endent`
datasourceSchema: `
model User {
id String @id
posts Post[]
Expand Down Expand Up @@ -126,3 +125,69 @@ testIntegration({
}
`,
})

testIntegration({
description: 'can project user-to-posts relation where user has composite ID',
datasourceSchema: `
model User {
id1 String
id2 String
posts Post[]
@@id(fields: [id1, id2])
}
model Post {
id String @id
author User? @relation(fields: [authorId1, authorId2], references: [id1, id2])
authorId1 String
authorId2 String
}
`,
apiSchema({ User, Post }) {
return [
queryType({
definition(t) {
t.nonNull.list.nonNull.field('users', {
type: User.$name,
resolve(_, __, ctx) {
return ctx.prisma.user.findMany()
},
})
},
}),
objectType({
name: User.$name,
definition(t) {
t.field(User.id1.name, User.id1)
t.field(User.posts.name, User.posts)
},
}),
objectType({
name: Post.$name,
definition(t) {
t.field(Post.id.name, Post.id)
},
}),
]
},
async datasourceSeed(prisma) {
await prisma.user.create({
data: {
id1: 'user1',
id2: 'user1',
posts: {
create: [{ id: 'post1' }],
},
},
})
},
apiClientQuery: gql`
query {
users {
id1
posts {
id
}
}
}
`,
})

0 comments on commit 2b636ce

Please sign in to comment.