Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb embedded documents resolver doesn't work #194

Open
asaadmohammed74 opened this issue Mar 3, 2022 · 1 comment
Open

mongodb embedded documents resolver doesn't work #194

asaadmohammed74 opened this issue Mar 3, 2022 · 1 comment
Labels
type/bug Something is not working the way it should

Comments

@asaadmohammed74
Copy link

With the release of prisma 3.10.0, we now can use embedded documents via the type keyword.

currently the generated field resolver doesn't work, it gives
Cannot read properties of undefined (reading 'fields')

example

model Account {
  id       String @id @default(auto()) @map("_id") @db.ObjectId
  username String
  posts    Post[]
}

type Post {
  title   String
  content String
}
objectType({
    name: 'Post', // need to match Account.posts.type
    definition(t) {
        t.string('title')
        t.string('content')
    }
})

objectType({
    name: Account.$name,
    description: Account.$description,
    definition(t) {
        t.field(Account.username)
        t.field(Account.posts)
    }
})

extendType({
    type: 'Query',
    definition(t) {
        t.field('accounts', {
            type: list('Account'),
            resolve: (root, args, { prisma }) => {
                return prisma.account.findMany()
            }
        })
    }
})

this fails with

{
  "errors": [
    {
      "message": "Cannot read properties of undefined (reading 'fields')",
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ],
      "path": [
        "accounts",
        0,
        "posts"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read properties of undefined (reading 'fields')",
            "    at applyFluent (node_modules\\@prisma\\client\\runtime\\index.js:37215:39)",
            "    at Object.get (node_modules\\@prisma\\client\\runtime\\index.js:37229:16)",
            "    at node_modules\\nexus-prisma\\src\\generator\\models\\javascript.ts:304:18",
            "    at node_modules\\nexus-shield\\src\\plugin.ts:100:16"
          ]
        }
      }
    }
  ],
  "data": null
}

and to fix the issue i have to override the Account.posts resolver

objectType({
    name: Account.$name,
    description: Account.$description,
    definition(t) {
        t.field(Account.username)
        t.field({
            ...Account.posts,
            resolve: (root) => {
                return (root as any).posts
            }
        })
    }
})

this works and produce the expected result

{
  "data": {
    "accounts": [
      {
        "username": "name",
        "posts": [
          {
            "title": "t1",
            "content": "c1"
          },
          {
            "title": "t2",
            "content": "c2"
          }
        ]
      }
    ]
  }
}
@asaadmohammed74 asaadmohammed74 added the type/bug Something is not working the way it should label Mar 3, 2022
@flygomel
Copy link

t.field({...Account.posts, resolve: undefined}) working too and more type safely.

Is it possible to remove the resolver for embedded documents, since it is not needed there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is not working the way it should
Projects
None yet
Development

No branches or pull requests

2 participants