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

Schema relations triggers errors in generated sdl #780

Closed
leibowitz opened this issue Jun 30, 2020 · 1 comment
Closed

Schema relations triggers errors in generated sdl #780

leibowitz opened this issue Jun 30, 2020 · 1 comment

Comments

@leibowitz
Copy link
Contributor

leibowitz commented Jun 30, 2020

I have two models in my schema:

model User {
  id                                Int                  @default(autoincrement()) @id
  name                              String
}

model Post {
  id                                Int                  @default(autoincrement()) @id
  createdAt                         DateTime             @default(now())
  updatedAt                         DateTime             @default(now()) @updatedAt
  title                             String
  body                              String
}

Everything is working fine, until I add a relation between posts and users, and try to generate the sdl for them.

model User {
  id                                Int                  @default(autoincrement()) @id
  name                              String
}

model Post {
  id                                Int                  @default(autoincrement()) @id
  createdAt                         DateTime             @default(now())
  updatedAt                         DateTime             @default(now()) @updatedAt
  title                             String
  body                              String
  authorId                          Int
  author                            User                 @relation(fields: [authorId], references: [id])
}

When running the sdl or scaffold commands, everything looks fine.

# yarn rw g scaffold -f User
  ✔ Generating scaffold files...
    ✔ Writing `./api/src/graphql/users.sdl.js`...
    ✔ Writing `./api/src/services/users/users.test.js`...
    ✔ Writing `./api/src/services/users/users.js`...
    ✔ Writing `./web/src/layouts/UsersLayout/UsersLayout.js`...
    ✔ Writing `./web/src/pages/EditUserPage/EditUserPage.js`...
    ✔ Writing `./web/src/pages/UserPage/UserPage.js`...
    ✔ Writing `./web/src/pages/UsersPage/UsersPage.js`...
    ✔ Writing `./web/src/pages/NewUserPage/NewUserPage.js`...
    ✔ Writing `./web/src/components/EditUserCell/EditUserCell.js`...
    ✔ Writing `./web/src/components/User/User.js`...
    ✔ Writing `./web/src/components/UserCell/UserCell.js`...
    ✔ Writing `./web/src/components/UserForm/UserForm.js`...
    ✔ Writing `./web/src/components/Users/Users.js`...
    ✔ Writing `./web/src/components/UsersCell/UsersCell.js`...
    ✔ Writing `./web/src/components/NewUser/NewUser.js`...
  ✔ Adding scaffold routes...
  ✔ Adding scaffold asset imports...
# yarn rw g scaffold -f Post
  ✔ Generating scaffold files...
    ✔ Writing `./api/src/graphql/posts.sdl.js`...
    ✔ Writing `./api/src/services/posts/posts.test.js`...
    ✔ Writing `./api/src/services/posts/posts.js`...
    ✔ Writing `./web/src/layouts/PostsLayout/PostsLayout.js`...
    ✔ Writing `./web/src/pages/EditPostPage/EditPostPage.js`...
    ✔ Writing `./web/src/pages/PostPage/PostPage.js`...
    ✔ Writing `./web/src/pages/PostsPage/PostsPage.js`...
    ✔ Writing `./web/src/pages/NewPostPage/NewPostPage.js`...
    ✔ Writing `./web/src/components/EditPostCell/EditPostCell.js`...
    ✔ Writing `./web/src/components/Post/Post.js`...
    ✔ Writing `./web/src/components/PostCell/PostCell.js`...
    ✔ Writing `./web/src/components/PostForm/PostForm.js`...
    ✔ Writing `./web/src/components/Posts/Posts.js`...
    ✔ Writing `./web/src/components/PostsCell/PostsCell.js`...
    ✔ Writing `./web/src/components/NewPost/NewPost.js`...
  ✔ Adding scaffold routes...
  ✔ Adding scaffold asset imports...
✨  Done in 1.65s.

Until you start the server with:

yarn rw dev

Then this error appears in the logs:

api | error GraphQLError: Syntax Error: Expected Name, found [
api |     at syntaxError (node_modules/graphql/error/syntaxError.js:15:10)
api |     at Parser.expectToken (node_modules/graphql/language/parser.js:1404:40)
api |     at Parser.parseName (node_modules/graphql/language/parser.js:94:22)
api |     at Parser.parseFieldDefinition (node_modules/graphql/language/parser.js:857:21)
api |     at Parser.optionalMany (node_modules/graphql/language/parser.js:1497:28)
api |     at Parser.parseFieldsDefinition (node_modules/graphql/language/parser.js:846:17)
api |     at Parser.parseObjectTypeDefinition (node_modules/graphql/language/parser.js:798:23)
api |     at Parser.parseTypeSystemDefinition (node_modules/graphql/language/parser.js:696:23)
api |     at Parser.parseDefinition (node_modules/graphql/language/parser.js:146:23)
api |     at Parser.many (node_modules/graphql/language/parser.js:1518:26) {
api |   message: 'Syntax Error: Expected Name, found [',
api |   locations: [ { line: 7, column: 24 } ]

This seems to cause the /graphql endpoint to fail. I managed to find the culprit in api/src/graphql/users.sdl.js:

import gql from 'graphql-tag'

export const schema = gql`
  type User {
    id: Int!
    name: String!
    Post: Post[]
  }

  type Query {
    users: [User!]!
    user(id: Int!): User!
  }

  input CreateUserInput {
    name: String!
  }

  input UpdateUserInput {
    name: String
  }

  type Mutation {
    createUser(input: CreateUserInput!): User!
    updateUser(id: Int!, input: UpdateUserInput!): User!
    deleteUser(id: Int!): User!
  }
`

Notice this line:

Post: Post[]

Which should be formatted this way instead (from what I understand):

Post: [Post]

In fact, replacing it makes the error disappear and the web and api to serve requests properly again. I then get other errors, when trying to create a post though...

Is it not supported yet? I had a feeling it was, reading #327

Am I doing something wrong? I would love to contribute to the project and make this work, but would need a few pointers...

@leibowitz
Copy link
Contributor Author

Just found out the issue was already pointed out in #747

#741 (comment)

And apparently, already has a fix in this PR
#772

All is well :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant