Skip to content

Commit

Permalink
fix(client): support non-scalar sub selection
Browse files Browse the repository at this point in the history
Fixes https://github.com/prisma/prisma/issues/3309

1. Add __typename when a type has no scalar fields.
2. Removes __typename from result when it is the only field (aware of
    $fragment)
  • Loading branch information
Divyendu Singh committed Jan 18, 2019
1 parent 4eb8149 commit 3bbb3cf
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
11 changes: 11 additions & 0 deletions cli/packages/prisma-client-lib/src/Client.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,15 @@ Generated by [AVA](https://ava.li).
content␊
}␊
}␊


## automatic non-scalar sub selection

> Snapshot 1
`query ($where: UserWhereInput) {␊
users(where: $where) {␊
__typename␊
}␊
}␊
`
Binary file modified cli/packages/prisma-client-lib/src/Client.test.js.snap
Binary file not shown.
49 changes: 44 additions & 5 deletions cli/packages/prisma-client-lib/src/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@ import { Client } from './Client'
import { Model } from './types'
import { print } from 'graphql'

test('automatic non-scalar sub selection', t => {
const typeDefs = `
type Query {
users(where: UserWhereInput): [User]
}
input UserWhereInput {
id: ID!
}
type User {
house: House!
}
type House {
id: ID!
name: String!
}
`

const models: Model[] = []

const endpoint = 'http://localhost:4466'

const client: any = new Client({
typeDefs,
endpoint,
models,
})

client.users()

const document = client.getDocumentForInstructions(
Object.keys(client._currentInstructions)[0],
)

t.snapshot(print(document))
})

test('related type', t => {
const typeDefs = `
type Query {
Expand Down Expand Up @@ -30,7 +69,7 @@ test('related type', t => {
},
]

const endpoint = 'http://localhost;4466'
const endpoint = 'http://localhost:4466'

const client: any = new Client({
typeDefs,
Expand Down Expand Up @@ -74,7 +113,7 @@ test('deep related type', t => {
},
]

const endpoint = 'http://localhost;4466'
const endpoint = 'http://localhost:4466'

const client: any = new Client({
typeDefs,
Expand Down Expand Up @@ -118,7 +157,7 @@ test('embedded type', t => {
},
]

const endpoint = 'http://localhost;4466'
const endpoint = 'http://localhost:4466'

const client: any = new Client({
typeDefs,
Expand Down Expand Up @@ -171,7 +210,7 @@ test('nested mbedded type', t => {
},
]

const endpoint = 'http://localhost;4466'
const endpoint = 'http://localhost:4466'

const client: any = new Client({
typeDefs,
Expand Down Expand Up @@ -269,7 +308,7 @@ test('nested args', t => {
},
]

const endpoint = 'http://localhost;4466'
const endpoint = 'http://localhost:4466'

const client: any = new Client({
typeDefs,
Expand Down
33 changes: 33 additions & 0 deletions cli/packages/prisma-client-lib/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ export class Client {
}
log('unpack it')

const lastInstruction = instructions[count - 1]
const selectionFromFragment = Boolean(lastInstruction.fragment)

if (
!selectionFromFragment &&
Array.isArray(pointer) &&
pointer.length > 0
) {
/*
As per the spec: https://github.com/prisma/prisma/issues/3309
We need to remove objects of the shape {__typename: <type>}
from the output (except when fragment). Checking one element
is enough, as they will have the same shape.
*/
if (
Object.keys(pointer[0]).length === 1 &&
Object.keys(pointer[0])[0] === '__typename'
) {
pointer = []
}
}

return pointer
}

Expand Down Expand Up @@ -335,6 +357,17 @@ export class Client {
return node
}, null)

if (ast.selectionSet.selections.length === 0) {
ast.selectionSet.selections = [
{
kind: 'Field',
name: { kind: 'Name', value: '__typename' },
arguments: [],
directives: [],
},
]
}

return {
ast: { ...ast, variableDefinitions },
variables,
Expand Down

0 comments on commit 3bbb3cf

Please sign in to comment.