graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Model.find({where: {id}}) returns wrong output.

Open gethassaan opened this issue 9 months ago • 4 comments

Describe the bug I have an application using Nestjs, Graphql and Neo4j. It has a user model and I have used @neo4j/graphql-ogm package and it's generate function to generate OGM object. When I try to get the user by id using find function it returns wrong result.

Type definitions

type User {
    id: ID! @id
    firstName: String!
    lastName: String!
    email: String! @unique
}

To Reproduce Steps to reproduce the behavior:

  • first create a couple of users using the above scehma.
  • then use the below given code try to fetch the user by id (for me it's returning the first user everytime instead of returning the user with matching id)
const User = (await this.neo4jService.ogm).model('User');
//  find user by id is not returning right user
const [record] = await User.find({
  where: { id: id },
});
  1. See error

Expected behavior The function should return the result with the passed id.

Screenshots If applicable, add screenshots to help explain your problem. Screenshot 2024-05-05 at 2 21 32 PM

System (please complete the following information):

gethassaan avatar May 05 '24 12:05 gethassaan

Many thanks for raising this bug report @gethassaan. :bug: We will now attempt to reproduce the bug based on the steps you have provided.

Please ensure that you've provided the necessary information for a minimal reproduction, including but not limited to:

  • Type definitions
  • Resolvers
  • Query and/or Mutation (or multiple) needed to reproduce

If you have a support agreement with Neo4j, please link this GitHub issue to a new or existing Zendesk ticket.

Thanks again! :pray:

neo4j-team-graphql avatar May 05 '24 12:05 neo4j-team-graphql

Hi @gethassaan I tried to reproduce this, but I can successfully request specific users via their IDs, without any issue.

Some thoughts:

  • Did you create the users through GraphQL? It might be an ID mismatch if you made them yourself elsewhere.
  • Can you test without having a NestJS service in between? NestJS may be causing a problem. Perhaps a cache?

mjfwebb avatar May 06 '24 11:05 mjfwebb

Hey @mjfwebb, I created the user via Graphql and the ID's were auto generated and regarding the cache i'm sure it's not cache since I also tried this with a new (fresh) clone.

And as I am trying a simple query as mentioned above, I am out of clues to why this might be happening. 🥲

Can you also share how did you fetch the users? Any process or steps that you followed?

gethassaan avatar May 06 '24 12:05 gethassaan

Sure, here's what I did:

import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL } from "@neo4j/graphql";
import { OGM } from "@neo4j/graphql-ogm";
import neo4j from "neo4j-driver";

const driver = neo4j.driver(
  "bolt://localhost:7687",
  neo4j.auth.basic("username", "password")
);

const typeDefs = `#graphql
  type User {
      id: ID! @id
      firstName: String!
      lastName: String!
      email: String! @unique
  }
`;

const ogm = new OGM({ typeDefs, driver });
const User = ogm.model("User");

const resolvers = {};

const neoSchema = new Neo4jGraphQL({
  typeDefs,
  driver,
  resolvers,
});

async function main() {
  const schema = await neoSchema.getSchema();
  await ogm.init();
  const server = new ApolloServer({
    schema,
  });

  const { url } = await startStandaloneServer(server, {
    context: async ({ req }) => ({ req }),
  });

  console.log(`🚀 Server ready at ${url}`);
  const [record] = await User.find({
    where: { id: "your-id-here" },
  });

  console.log(record);
}

main();

Where you replace your-id-here with the id generated. I selected the ID from when I created the user through a mutation:

mutation Mutation {
  createUsers(
    input: [{ email: "[email protected]", firstName: "First", lastName: "Last" }]
  ) {
    users {
      id
    }
  }
}

mjfwebb avatar May 06 '24 12:05 mjfwebb

Thank you @mjfwebb your solution does work, I am not sure why would the bug be coming up. 🤔

gethassaan avatar May 07 '24 08:05 gethassaan

@gethassaan I'll close the issue then, since we confirmed it's not on our side.

mjfwebb avatar May 07 '24 09:05 mjfwebb