Skip to content

Commit

Permalink
tests: cover 1-to-n reverse access (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored May 17, 2021
1 parent 7a26a49 commit d7030ef
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 6 deletions.
18 changes: 17 additions & 1 deletion tests/__helpers__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ import { ModuleSpec } from '../src/generator/types'
type APISchemaSpec = (nexusPrisma: any) => AllNexusTypeDefs[]

type IntegrationTestParams = {
/**
* Name of this test
*/
description: string
/**
* Proxy for it.only
*/
only?: boolean
/**
* Proxy for it.skip
*/
skip?: boolean
/**
* Define a Prisma schema file
*
Expand All @@ -48,7 +59,12 @@ export function testGeneratedModules(params: { databaseSchema: string; descripti
* GraphQL schema and execution result.
*/
export function testIntegration(params: IntegrationTestParams) {
it(params.description, async () => {
if (params.skip && params.only)
throw new Error(`Cannot specify to skip this test AND only run this test at the same time.`)

const itOrItOnlyOrItSkip = params.only ? it.only : params.skip ? it.skip : it

itOrItOnlyOrItSkip(params.description, async () => {
const result = await integrationTest(params)
expect(result.graphqlSchemaSDL).toMatchSnapshot(`graphqlSchemaSDL`)
expect(result.graphqlOperationExecutionResult).toMatchSnapshot(`graphqlOperationExecutionResult`)
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/__snapshots__/relation1ToN.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`can project user-to-posts relationship in reverse (access use via post author field): graphqlOperationExecutionResult 1`] = `
Object {
"data": Object {
"posts": Array [
Object {
"author": Object {
"id": "user1",
},
"id": "post1",
},
Object {
"author": Object {
"id": "user1",
},
"id": "post2",
},
],
},
}
`;

exports[`can project user-to-posts relationship in reverse (access use via post author field): graphqlSchemaSDL 1`] = `
"
type Query {
posts: [Post!]!
}
type User {
id: ID!
}
type Post {
id: ID!
author: User
}
"
`;

exports[`can project user-to-posts relationship: graphqlOperationExecutionResult 1`] = `
Object {
"data": Object {
Expand Down
34 changes: 31 additions & 3 deletions tests/integration/json.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import endent from 'endent'
import { objectType } from 'nexus'
import gql from 'graphql-tag'
import { list, objectType, queryType } from 'nexus'
import NexusPrismaScalars from '../../scalars'
import { testGraphqlSchema } from '../__helpers__'
import { testIntegration } from '../__helpers__'

testGraphqlSchema({
testIntegration({
skip: true, // integration test currently only works against SQLite which doesn't support JSON
description: 'When a JSON field is defined in the Prisma schema it can be projected into the GraphQL API',
datasourceSchema: endent`
model Foo {
Expand All @@ -21,6 +23,32 @@ testGraphqlSchema({
t.field(Foo.json.$name, Foo.json)
},
}),
queryType({
definition(t) {
t.field('foos', {
type: list(Foo.name),
resolve(_, __, ctx) {
return ctx.prisma.foo.findMany()
},
})
},
}),
]
},
async datasourceSeed(prisma) {
await prisma.foo.create({
data: {
id: 'foo1',
json: JSON.stringify({ babar: true }),
},
})
},
apiClientQuery: gql`
query {
foos {
id
json
}
}
`,
})
64 changes: 63 additions & 1 deletion tests/integration/relation1ToN.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ testIntegration({
queryType({
definition(t) {
t.nonNull.list.nonNull.field('users', {
type: 'User',
type: User.$name,
resolve(_, __, ctx) {
return ctx.prisma.user.findMany()
},
Expand Down Expand Up @@ -64,3 +64,65 @@ testIntegration({
}
`,
})

testIntegration({
description: 'can project user-to-posts relationship in reverse (access use via post author field)',
datasourceSchema: endent`
model User {
id String @id
posts Post[]
}
model Post {
id String @id
author User? @relation(fields: [authorId], references: [id])
authorId String
}
`,
apiSchema({ User, Post }) {
return [
queryType({
definition(t) {
t.nonNull.list.nonNull.field('posts', {
type: Post.$name,
resolve(_, __, ctx) {
return ctx.prisma.post.findMany()
},
})
},
}),
objectType({
name: User.$name,
definition(t) {
t.field(User.id.name, User.id)
},
}),
objectType({
name: Post.$name,
definition(t) {
t.field(Post.id.name, Post.id)
t.field(Post.author.name, Post.author)
},
}),
]
},
async datasourceSeed(prisma) {
await prisma.user.create({
data: {
id: 'user1',
posts: {
create: [{ id: 'post1' }, { id: 'post2' }],
},
},
})
},
apiClientQuery: gql`
query {
posts {
id
author {
id
}
}
}
`,
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import endent from 'endent'
import { enumType } from 'nexus'
import { testGraphqlSchema } from '../__helpers__'
import { testGraphqlSchema } from '../../__helpers__'

testGraphqlSchema({
description: 'When an enum is defined in the Prisma schema it can be projected into the GraphQL API',
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/graphqlSchema/json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import endent from 'endent'
import { objectType } from 'nexus'
import NexusPrismaScalars from '../../../scalars'
import { testGraphqlSchema } from '../../__helpers__'

testGraphqlSchema({
description: 'When a JSON field is defined in the Prisma schema it can be projected into the GraphQL API',
datasourceSchema: endent`
model Foo {
id String @id
json Json
}
`,
apiSchema({ Foo }) {
return [
NexusPrismaScalars.DateTime,
NexusPrismaScalars.Json,
objectType({
name: Foo.$name,
definition(t) {
t.field(Foo.json.$name, Foo.json)
},
}),
]
},
})

0 comments on commit d7030ef

Please sign in to comment.