paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
684 stars 54 forks source link

generate:nexus failing for apollo-nexus-schema starter project #122

Closed willwillis closed 3 years ago

willwillis commented 4 years ago

I attempted to create a new pal apollo-nexus-schema app with mysql. All other steps from the README completed successfully. Only src/nexusSchema is failing with this error:

> pal --version
@paljs/cli/1.1.8 win32-x64 node-v12.18.2
> yarn generate:nexus
yarn run v1.22.4
$ ts-node --transpile-only src/nexusSchema

C:\Users\Me\apollo-nexus\node_modules\graphql\error\syntaxError.js:15
  return new _GraphQLError.GraphQLError("Syntax Error: ".concat(description), undefined, source, [position]);
         ^
GraphQLError: Syntax Error: Expected Name, found "}".
    at syntaxError (C:\Users\Me\apollo-nexus\node_modules\graphql\error\syntaxError.js:15:10)
    at Parser.expectToken (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:1423:40)
    at Parser.parseName (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:92:22)
    at Parser.parseInputValueDef (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:901:21)
    at Parser.optionalMany (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:1516:28)
    at Parser.parseInputFieldsDefinition (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:1066:17)
    at Parser.parseInputObjectTypeDefinition (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:1050:23)
    at Parser.parseTypeSystemDefinition (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:714:23)
    at Parser.parseDefinition (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:144:23)
    at Parser.many (C:\Users\Me\apollo-nexus\node_modules\graphql\language\parser.js:1537:26)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
> yarn dev
yarn dev
yarn run v1.22.4
$ ts-node-dev --no-notify --respawn --transpile-only src/server
Using ts-node version 8.10.2, typescript version 3.9.7
[ERROR] 12:35:58 GraphQLError: Syntax Error: Expected Name, found "}".
> prisma --version
Environment variables loaded from prisma\.env
@prisma/cli          : 2.3.0
Current platform     : windows
Query Engine         : query-engine e11114fa1ea826f9e7b4fa1ced34e78892fe8e0e (at ..\..\..\..\..\AppData\Local\Yarn\Data\global\node_modules\@prisma\cli\query-engine-windows.exe)
Migration Engine     : migration-engine-cli e11114fa1ea826f9e7b4fa1ced34e78892fe8e0e (at ..\..\..\..\..\AppData\Local\Yarn\Data\global\node_modules\@prisma\cli\migration-engine-windows.exe)
Introspection Engine : introspection-core e11114fa1ea826f9e7b4fa1ced34e78892fe8e0e (at ..\..\..\..\..\AppData\Local\Yarn\Data\global\node_modules\@prisma\cli\introspection-engine-windows.exe)
Format Binary        : prisma-fmt e11114fa1ea826f9e7b4fa1ced34e78892fe8e0e (at ..\..\..\..\..\AppData\Local\Yarn\Data\global\node_modules\@prisma\cli\prisma-fmt-windows.exe)

package.json

 "dependencies": {
    "@nexus/schema": "0.14.0",
    "@prisma/client": "2.3.0",
    "@paljs/nexus": "1.1.7",
    "apollo-server": "2.16.0",
    "graphql": "15.3.0"
  },
  "devDependencies": {
    "@prisma/cli": "2.3.0",
    "@types/node": "12.12.51",
    "prettier": "2.0.5",
    "ts-node": "8.10.2",
    "ts-node-dev": "1.0.0-pre.52",
    "typescript": "3.9.7"
},

The prisma client seems OK, I can use the client and launch studio just fine.

vimutti77 commented 4 years ago

I am also getting a GraphQLError: Syntax Error: Expected Name, found "}" error after changing PrismaSelect import

from

import { PrismaSelect } from '@prisma-tools/select'

to

import { PrismaSelect } from '@paljs/plugins'

my query

import { extendType, intArg } from '@nexus/schema'
import prisma from '@database/prisma'
import { PrismaSelect } from '@paljs/plugins'

export const userQuery = extendType({
  type: 'Query',
  definition: (t) => {
    t.field('user', {
      type: 'User',
      args: {
        userId: intArg(),
      },
      resolve: async (root, args, ctx, info): Promise<any> => {
        const user = await prisma.user.findOne({
          where: {
            id: args.userId,
          },
          select: new PrismaSelect(info).value.select,
        })

        return user
      },
    })
  },
})

my package.json

  "dependencies": {
    "@nexus/schema": "^0.15.0",
    "@paljs/plugins": "^1.1.9",
    "apollo-server-express": "^2.16.1",
    "cross-env": "^7.0.2",
    "dotenv-flow": "^3.2.0",
    "express": "^4.17.1",
    "graphql": "^15.3.0",
    "nexus-plugin-prisma": "^0.17.0"
  },
  "devDependencies": {
    "ts-node-dev": "^1.0.0-pre.59",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.0.2"
  }
AhmedElywa commented 4 years ago

@vimutti77 can you provide a minimal reproduction for the issue?

vimutti77 commented 4 years ago

@AhmedElywa This is minimal reproduction https://github.com/vimutti77/Test-PrismaSelect

This is what I found when doing minimal reproduction.

  1. The error will occur when there are many-to-many relations in schema.prisma.

model Post { id Int @id @default(autoincrement()) name String postCategories PostCategory[] }

model Category { id Int @id @default(autoincrement()) name String postCategories PostCategory[] }

model PostCategory { postId Int post Post @relation(fields: [postId], references: [id]) categoryId Int category Category @relation(fields: [categoryId], references: [id])

@@id([postId, categoryId]) }

2. If I add one field in a relation table, the error will disappear.

model PostCategory { foo String // <--- add this field postId Int post Post @relation(fields: [postId], references: [id]) categoryId Int category Category @relation(fields: [categoryId], references: [id])

@@id([postId, categoryId]) }


3. If I remove `new PrismaSelect(info).value`, the error will disappear.
AhmedElywa commented 4 years ago

Yes this because when we create input types for your models this model PostCategory will be empty and this gives an errors and when you add any field the input type not be empty so the issues are gone

I will look to good fix for this thanks

ziimakc commented 3 years ago

Same problem i think, is there any solutions?

AhmedElywa commented 3 years ago

@ZiiMakc you also have many to many relation models not have any scalar fields?

AhmedElywa commented 3 years ago

Hi all, I hope you all review the new version 2.0.0.

ziimakc commented 3 years ago

@AhmedElywa thanks, works fine now.

AhmedElywa commented 3 years ago

Thanks, @ZiiMakc for the review