findMany works but findOne doesn't work
See original GitHub issueI am using the basic GraphQL boilerplate, the one without auth.
My prisma/schema.prisma
file looks like:
datasource db {
provider = "sqlite"
url = "file:dev.db"
default = true
}
generator photon {
provider = "photonjs"
}
generator nexus_prisma {
provider = "nexus-prisma"
}
model Pokemon {
id String @default(cuid()) @id @unique
number Int
name String
attacks PokemonAttack
}
model PokemonAttack {
id Int @id
special Attack[]
}
model Attack {
id Int @id
name String
damage String
}
I changed my src/index.js
to:
const { GraphQLServer } = require('graphql-yoga')
const { join } = require('path')
const { makeSchema, objectType, idArg, stringArg } = require('@prisma/nexus')
const Photon = require('@generated/photon')
const { nexusPrismaPlugin } = require('@generated/nexus-prisma')
const photon = new Photon()
const nexusPrisma = nexusPrismaPlugin({
photon: ctx => ctx.photon,
})
const Attack = objectType({
name: "Attack",
definition(t) {
t.model.id()
t.model.name()
t.model.damage()
}
})
const PokemonAttack = objectType({
name: "PokemonAttack",
definition(t) {
t.model.id()
t.model.special()
}
})
const Pokemon = objectType({
name: "Pokemon",
definition(t) {
t.model.id()
t.model.number()
t.model.name()
t.model.attacks()
}
})
const Query = objectType({
name: 'Query',
definition(t) {
t.list.field('pokemon', {
type: 'Pokemon',
args: {
name: stringArg(),
},
resolve: (parent, { name }, ctx) => {
return ctx.photon.pokemon.findMany({
where: {
name
}
})
},
})
},
})
const Mutation = objectType({
name: 'Mutation',
definition(t) {
t.crud.createOnePokemon({ alias: 'addPokemon' })
},
})
const schema = makeSchema({
types: [Query, Mutation, Pokemon, Attack, PokemonAttack, nexusPrisma],
outputs: {
schema: join(__dirname, '/schema.graphql'),
},
typegenAutoConfig: {
sources: [
{
source: '@generated/photon',
alias: 'photon',
},
],
},
})
const server = new GraphQLServer({
schema,
context: request => {
return {
...request,
photon,
}
},
})
server.start(() => console.log(`🚀 Server ready at http://localhost:4000`))
module.exports = { Pokemon }
And my prisma/seed.js
looks like:
const Photon = require('@generated/photon')
const photon = new Photon.default()
async function main() {
const pikachu = await photon.pokemon.create({
data: {
name: "Pikachu",
id: '123132',
number: 215,
attacks: {
create: {
special: {
create: {
name: "Abra cadabra",
damage: "100",
}
}
}
}
}
})
console.log({ pikachu })
}
main()
.catch(e => console.error(e))
.finally(async () => {
await photon.disconnect()
})
Now if I use findOne
instead of findMany
, it doesn’t work:
ctx.photon.pokemon.findOne({
where: {
name
}
})
Is this intentional because FWIW I think it should work?
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
findOne works but not get all/find - node.js - Stack Overflow
I'm trying to get all the documents instead of just one. I'm changing findOne to find (shown below) and it doesn't work. How...
Read more >db.collection.findOne() — MongoDB Manual
Returns one document that satisfies the specified query criteria on the collection or view. If multiple documents satisfy the query, this method returns...
Read more >Mongoose v6.8.1: Query Population
For example, the below query won't return any results, even though author is populated. const story = await Story. findOne({ 'author.name': 'Ian Fleming ......
Read more >Prisma Client API (Reference)
A nested disconnect query breaks the connection between a parent record and a related record, but does not delete either record. See: Working...
Read more >Typed Queries - KMongo
findOne (Jedi::name eq 2) //you can use property reference with instances val yoda2 ... Then use the % operator - it has the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is expected.
name
is not a unique field so there is always a possibility of more than one matches when you are filtering by name. So it is available infindMany
but not infindOne
. If you want to make it available infindOne
, add the@unique
directive to it and regenerate the client.@meyer9 seems this is the closest we can get for now.