question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findMany works but findOne doesn't work

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
pantharshit00commented, Aug 20, 2019

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 in findMany but not in findOne. If you want to make it available in findOne, add the @unique directive to it and regenerate the client.

4reactions
OmgImAlexiscommented, Jun 11, 2020

@meyer9 seems this is the closest we can get for now.

const show = await prisma.objects.findMany({
    where: {
        id: 'id-here',
        owner: 'owners-id-here'
    },
    take: 1
}).then(_ => _[0]);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found