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

Support table relationships in service generators #412

Merged
merged 42 commits into from
Apr 10, 2020

Conversation

cannikin
Copy link
Member

@cannikin cannikin commented Apr 9, 2020

When generating an SDL, if any tables reference each other via prisma's @relation syntax, we will create the equivalent expected resolver syntax in the generated service.

Example

The following GraphQL query is not currently supported out of the box with Redwood's implementation of SDL + services. For the given schema.prisma:

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}

model Post {
  id     Int     @id @default(autoincrement())
  title  String  @unique
  body   String
  user   User    @relation(fields: [userId], references: [id])
  userId Int
}

You should be able to write the following GraphQL query:

posts {
  id
  title
  body
  user {
    name
    email
  }
}

We will create the following additional functions in the posts.js service:

export const Post = {
  user: (_obj, { root }) => db.post.findOne({ where: { id: root.id } }).user(),
}

And the following functions in the users.js service:

export const User = {
  posts: (_obj, { root }) => db.user.findOne({ where: { id: root.id } }).posts(),
}

Apollo will automatically use these relationships to make the GraphQL query above work.

Closes #316
Closes #327

peterp and others added 30 commits April 7, 2020 10:02
Babel flag --no-copy-ignored has a bug and does not respect our ignored config. For a short term fix, I'm using rimraf package (cross-platform support) to clean up __tests__ folders that shouldn't be in dist/
@cannikin
Copy link
Member Author

cannikin commented Apr 9, 2020

Hmm, I have a dilemma. While this does add relationships to the services, the scaffolds created when there's a relationship are busted. Two reasons:

  1. The foreign key field like userId is a string, when the GraphQL expects an Int (related to Cannot use scaffolded form to insert Float datatype into database #392)
  2. Even if the ID is cast properly, Prisma blows up when trying to assign userId a value. It expects you to connect to an actual User record (?????)

So we could release this along with a note that scaffolds won't work with it yet. Instead of relationships being 100% broken, they're now only 50% broken. :) Getting data OUT of a service works great, it's inserting that's killing us.

Or don't do anything until both problems above are fixed. That probably means more complaints about relationships not being supported in the meantime. :(

@cannikin cannikin changed the title WIP: Support table relationships in service generators Support table relationships in service generators Apr 10, 2020
@cannikin cannikin marked this pull request as ready for review April 10, 2020 23:40
@cannikin cannikin merged commit 4d392e8 into master Apr 10, 2020
@cannikin cannikin deleted the rc-service-relationships branch April 10, 2020 23:41
@gfpacheco
Copy link
Contributor

I was working on this but I got stuck because prisma's getDMMF doesn't give us the fields argument.

They just told me that it now does, but it's still in the alpha branch, and I thought Id$ wasn't a reliable way to do it, but I guess that's good enough, I'm glad someone with more time was able to finish it.

@weaversam8
Copy link

Great work getting this out the door @cannikin! 🎉

Can't wait to try it out!

mohsen1 pushed a commit to mohsen1/redwood that referenced this pull request Apr 12, 2020
* Remove global mocks, localize to test.

* Remove more global mocks.

* Make dbCommand tests work again.

* Write snapshot tests for generators.

* Add generate tests and snaps.

* Play around with snapshot testing.

* Update snapshots.

* Complete reorg of generator files and templates, adds text and fixture directories

Cell test working

* Component, layout, page and service tests

* Adds tests for generator helpers.js

* More tests

* Comment out some variables that aren't being used for now

* Adds multi-word cell tests

* Variable name change

* Adds multi-word component tests

* Component test fix

* Adds multi-word layout tests

* Adds multi-word page tests

* Creates multi-word and CRUD sdl tests

* Adds multi word tests for service

* Adds scaffold tests

* Count actual number of files returned

* Rename test directories to __tests__

* Adds tests for src/lib/index

* Ignore any files named "test" inside a fixtures directory

* Adds generateTemplate tests

* Ignore fixture files when linting

* fix babel build ignore error using rimraf (rm -rf)

Babel flag --no-copy-ignored has a bug and does not respect our ignored config. For a short term fix, I'm using rimraf package (cross-platform support) to clean up __tests__ folders that shouldn't be in dist/

* Converts SDL generator to use same sub-generator technique as scaffold generator

* Updates README with new generator file structure

* Removes .fixture extensions

* Remove unused argument

* Adds relations output to service generator

* Update SDL tests for service relation creation

* Gets scaffold to pass along relations, refactor relation lookup

* Reorganize services fixtures

* In scaffold cells, don't query for related objects, just foreign keys

* Adds helper for extracting foreign key names that are type Int

* Automatically cast foreign keys to Int in scaffold generated files

Co-authored-by: Peter Pistorius <peter.pistorius@gmail.com>
Co-authored-by: Rob Cameron <rob.cameron@fastmail.com>
Co-authored-by: David S Price <thedavid@thedavidprice.com>
@weaversam8
Copy link

@cannikin - This closed #327, but I think this actually should have closed #324. #327 was the issue for the generated UI stuff not working, while #324 was the issue for services not properly resolving relation fields.

If that's the case, #324 should definitely be closed by this issue, but #327 should probably be reopened, unless this PR is bigger than I've noticed so far.

@cannikin
Copy link
Member Author

#327 was was actually fixed a couple PRs ago. The generated query on the display pages excludes other objects now and only pulls scalar fields. So if you had a Post with a userId you would get that in your query, no user. You can add that yourself, of course, but the scaffolded pages will only include scalars for editing. https://github.com/redwoodjs/redwood/blob/master/packages/cli/src/commands/generate/scaffold/scaffold.js#L149

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

Successfully merging this pull request may close these issues.

Scaffold generator ignores relations Relation support
5 participants