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

Failed to load schema from scalar BigInt #4394

Closed
Tobbe opened this issue Feb 6, 2022 · 12 comments · Fixed by #4849
Closed

Failed to load schema from scalar BigInt #4394

Tobbe opened this issue Feb 6, 2022 · 12 comments · Fixed by #4849
Assignees

Comments

@Tobbe
Copy link
Member

Tobbe commented Feb 6, 2022

I'm getting errors when trying to (re-) generate sdls, and when trying to generate types

Here's how to reproduce:

yarn create redwood-app --ts my-app
cd my-app
yarn upgrade --tag canary

Replace the content in schema.prisma with this:

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider      = "prisma-client-js"
  binaryTargets = "native"
}

model Product {
  id                String              @id @default(uuid())
  createdAt         DateTime            @default(now())
  updatedAt         DateTime            @default(now()) @updatedAt
  name              String
  // CategoryToProduct CategoryToProduct[]
}

model Category {
  id                 Int                 @id @default(autoincrement())
  name               String
  parent             Int?
  parentCategory     Category?           @relation("CategoryParent", fields: [parent], references: [id])
  childrenCategories Category[]          @relation("CategoryParent")
  // CategoryToProduct  CategoryToProduct[]
}

// model CategoryToProduct {
//   product    Product  @relation(fields: [productId], references: [id])
//   productId  String
//   category   Category @relation(fields: [categoryId], references: [id])
//   categoryId Int

//   @@id([productId, categoryId])
// }
yarn rw prisma migrate dev --name 'initial'
yarn rw g sdl --crud product
yarn rw g sdl --crud category

So far everything works fine.

Now, un-comment everything in schema.prisma, and then migrate again

yarn rw prisma migrate dev --name 'category_to_product'

Now try to generate sdl crud for product again and you'll see a bunch of errors.

$ yarn rw g sdl --crud -f product
yarn run v1.22.17
$ /Users/tobbe/tmp/ctp/node_modules/.bin/rw g sdl --crud -f product
(node:52971) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
  ✔ Generating SDL files...
    ✔ Successfully wrote file `./api/src/graphql/products.sdl.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.scenarios.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.test.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.ts`
  ⠙ Generating types ...
Failed to load schema
Failed to load schema
AggregateError: Something went wrong Failed to load schema for "/Users/tobbe/tmp/ctp/.redwood/schema.graphql"
        Failed to load schema from scalar BigInt

scalar Date

scalar Time

scalar DateTime

scalar JSON

scalar JSONObject

type Redwood {
  version: String
  currentUser: JSON
  prismaVersion: String
}

type Query {
  redwood: Redwood
},graphql/**/*.sdl.{js,ts},directives/**/*.{js,ts}:

        Unknown type: "CategoryToProduct".
        Error: Unknown type: "CategoryToProduct".
    at getNamedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:427:13)
    at getWrappedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:442:12)
    at getWrappedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:435:42)
    at getWrappedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:439:45)
    at buildFieldMap (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:483:17)
    at fields (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:672:25)
    at resolveObjMapThunk (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:504:40)
    at defineFieldMap (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:760:20)
    at GraphQLObjectType._fields (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:685:26)
    at GraphQLObjectType.getFields (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:704:27)
    
        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export "schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
          - String in config file
    
        Try to use one of above options and run codegen again.
    
      
    at executeCodegen (/Users/tobbe/tmp/ctp/node_modules/@graphql-codegen/cli/index.js:1042:28)
    at async generate (/Users/tobbe/tmp/ctp/node_modules/@graphql-codegen/cli/index.js:1349:25)
    at async generateGraphQLSchema (/Users/tobbe/tmp/ctp/node_modules/@redwoodjs/internal/dist/generate/graphqlSchema.js:27:15)
    at async Task.generate [as task] (/Users/tobbe/tmp/ctp/node_modules/@redwoodjs/internal/dist/generate/generate.js:25:22)

Error: Failed to load schema
    at loadSchema (/Users/tobbe/tmp/ctp/node_modules/@graphql-codegen/cli/index.js:421:15)
    at async /Users/tobbe/tmp/ctp/node_modules/@graphql-codegen/cli/index.js:804:33
    at async /Users/tobbe/tmp/ctp/node_modules/@graphql-codegen/cli/index.js:934:52
    at async /Users/tobbe/tmp/ctp/node_modules/@graphql-codegen/cli/index.js:821:21
Error parsing SDLs or Schema

        Failed to load schema from scalar BigInt

scalar Date

scalar Time

scalar DateTime

scalar JSON

scalar JSONObject

type Redwood {
  version: String
  currentUser: JSON
  prismaVersion: String
}

type Query {
  redwood: Redwood
},graphql/**/*.sdl.{js,ts},directives/**/*.{js,ts}:

        Unknown type: "CategoryToProduct".
        Error: Unknown type: "CategoryToProduct".
    at getNamedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:427:13)
    at getWrappedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:442:12)
    at getWrappedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:435:42)
    at getWrappedType (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:439:45)
    at buildFieldMap (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:483:17)
    at fields (/Users/tobbe/tmp/ctp/node_modules/graphql/utilities/extendSchema.js:672:25)
    at resolveObjMapThunk (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:504:40)
    at defineFieldMap (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:760:20)
    at GraphQLObjectType._fields (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:685:26)
    at GraphQLObjectType.getFields (/Users/tobbe/tmp/ctp/node_modules/graphql/type/definition.js:704:27)
    
        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export "schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
  ✔ Generating SDL files...
    ✔ Successfully wrote file `./api/src/graphql/products.sdl.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.scenarios.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.test.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.ts`
  ⠹ Generating types ...

      Unable to find any GraphQL type definitions for the following pointers:
        
          - ./web/src/**/!(*.d).{ts,tsx,js,jsx}
          

      Unable to find any GraphQL type definitions for the following pointers:
  ✔ Generating SDL files...
    ✔ Successfully wrote file `./api/src/graphql/products.sdl.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.scenarios.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.test.ts`
    ✔ Successfully wrote file `./api/src/services/products/products.ts`
  ✔ Generating types ...
✨  Done in 2.63s.
yarn rw g types

Generating types as above also gives the same error message

@Tobbe
Copy link
Member Author

Tobbe commented Feb 8, 2022

Don't know who's best suited for this. @dac09, I'll assign it to you. Feel free to pass it on if you want.

The easy to way to work around my issue is to add an id field to CategoryToProduct. Generate sdl for CtP and then you can force-generate the product and category sdls.

Ideally doing what I'm doing above should just work. But if that's not feasible, at least provide better messaging and/or docs around this. And even if our generators don't support this, there should be a way to get this working manually, imho.

@dac09
Copy link
Contributor

dac09 commented Feb 8, 2022

This issue here is that you're generating things in a strange order, no?

It makes sense that the generators don't know what CategoryToProduct is if you've not generated an sdl for C2P first.

I understand it being confusing, but I'm not sure what the expected behaviour would be? Message to say "do things in the right order please?" doesn't seem helpful to me.

@Tobbe
Copy link
Member Author

Tobbe commented Feb 8, 2022

I agree. It's difficult to say what the expected behaviour should be here. And there are a few different things going on, and they all add to the confusion.

The error message starts with

Failed to load schema
Failed to load schema
AggregateError: Something went wrong Failed to load schema for "/Users/tobbe/tmp/ctp/.redwood/schema.graphql"
        Failed to load schema from scalar BigInt

Reading that it sounds like the problem is with "scalar BigInt". And if you've mainly done frontend stuff you might not even know what that means.

So the thought process might go something like "Ehh, ok, I don't really know what that means, but let's keep reading, see if I find something I recognise". And so you find this

        Unknown type: "CategoryToProduct".
        Error: Unknown type: "CategoryToProduct".

And you're like "Ohh, it's complaining about types. Let's run the types generator" And so you run yarn rw g types, but that doesn't help. You still have the same error message. "Ok, that didn't help. Maybe I need to run the sdl generator for c2p". yarn rw g sdl CategoryToProduct. And now you get a new error (because there is no id Int @id @default(autoincrement())in the c2p model). Whatever you do either doesn't help, or doesn't work (throws errors in your face). And so you get stuck and frustrated.

Something like that ☝️ is the experience for someone new to this. I want to make this experience better for our users.

Another thing about this is even I don't know how to manually solve this. Where do I need to go and add the CategoryToProduct type?

@mbush92
Copy link

mbush92 commented Mar 3, 2022

I am having the same issue and I have run the scaffold for all of the models and still have missing types. When I try to run yarn rw g types I get the same error as @Tobbe. Not sure what to do to manually fix this either. I have been away from the ecosystem for a while and trying to get back started on a new project and can't get the basic framework scaffolded out like I used to do...eons ago in RWJS time.

@Tobbe
Copy link
Member Author

Tobbe commented Mar 3, 2022

@mbush92 Thanks for chiming in here! I've added this to our DX-improvement board and will make sure this gets some more love soon.

https://github.com/orgs/redwoodjs/projects/9/views/1

@mbush92
Copy link

mbush92 commented Mar 3, 2022

@Tobbe any idea how to fix this manually so that I can generate the types?

My schema.prisma file looks like this

datasource db {
  provider = "postgres"
  url      = env("DATABASE_URL")
}

generator client {
  provider      = "prisma-client-js"
  binaryTargets = "native"
}


model Item {
  id          String @id @default(uuid())
  name        String
  description String
  eoq         Float  @default(1)
  moq         Float  @default(1)
  leadtime    Int    @default(14)
  cost        Float  @default(0)
  Vendor      Vendor @relation(fields: [vendorId], references: [id])
  vendorId    String

  uom        UOM       @relation(fields: [uomId], references: [id])
  uomId      String
  Status     Status?   @relation(fields: [statusId], references: [id])
  statusId   String?
  ItemType   ItemType? @relation(fields: [itemTypeId], references: [id])
  itemTypeId String?

  createdAt DateTime  @default(now())
  updatedAt DateTime?
}

model Vendor {
  id   String @id @default(uuid())
  name String
  street String
  city String
  state String
  zipcode Int

  Items Item[]
}

model UOM {
  id   String @id @default(uuid())
  name String

  Items Item[]
}

model RW_DataMigration {
  version    String   @id
  name       String
  startedAt  DateTime
  finishedAt DateTime
}

model Status {
  id   String @id @default(uuid())
  name String

  Items Item[]
}

model ItemType {
  id   String @id @default(uuid())
  name String

  Items Item[]
}

I don't see anything that would make me think there would be an issue.

@Tobbe
Copy link
Member Author

Tobbe commented Mar 4, 2022

@dthyresson Tagging you because I know you've been through this issue recently too. Please let us know if you found a better way to do this than what I'm about to describe.

@mbush92 This is how I got your schema models scaffolded:

1. Comment out all relations

datasource db {
  provider = "postgres"
  url      = env("DATABASE_URL")
}

generator client {
  provider      = "prisma-client-js"
  binaryTargets = "native"
}

model Item {
  id          String @id @default(uuid())
  name        String
  description String
  eoq         Float  @default(1)
  moq         Float  @default(1)
  leadtime    Int    @default(14)
  cost        Float  @default(0)
  // Vendor      Vendor @relation(fields: [vendorId], references: [id])
  // vendorId    String

  // uom        Uom       @relation(fields: [uomId], references: [id])
  // uomId      String
  // Status     Status?   @relation(fields: [statusId], references: [id])
  // statusId   String?
  // ItemType   ItemType? @relation(fields: [itemTypeId], references: [id])
  // itemTypeId String?

  createdAt DateTime  @default(now())
  updatedAt DateTime?
}

model Vendor {
  id      String @id @default(uuid())
  name    String
  street  String
  city    String
  state   String
  zipcode Int

  // Items Item[]
}

model Uom {
  id   String @id @default(uuid())
  name String

  // Items Item[]
}

model RW_DataMigration {
  version    String   @id
  name       String
  startedAt  DateTime
  finishedAt DateTime
}

model Status {
  id   String @id @default(uuid())
  name String

  // Items Item[]
}

model ItemType {
  id   String @id @default(uuid())
  name String

  // Items Item[]
}

2. Scaffold all models

yarn rw g scaffold ItemType
yarn rw g scaffold Status
yarn rw g scaffold Uom
yarn rw g scaffold Vendor
yarn rw g scaffold Item

3. Uncomment one relation at a time

model Item {
  id          String @id @default(uuid())
  name        String
  description String
  eoq         Float  @default(1)
  moq         Float  @default(1)
  leadtime    Int    @default(14)
  cost        Float  @default(0)
  // Vendor      Vendor @relation(fields: [vendorId], references: [id])
  // vendorId    String

  // uom        Uom       @relation(fields: [uomId], references: [id])
  // uomId      String
  // Status     Status?   @relation(fields: [statusId], references: [id])
  // statusId   String?
  ItemType   ItemType? @relation(fields: [itemTypeId], references: [id])
  itemTypeId String?

  createdAt DateTime  @default(now())
  updatedAt DateTime?
}

// ...

model ItemType {
  id   String @id @default(uuid())
  name String

  Items Item[]
}

4. Scaffold Item and the uncommented model

yarn rw g scaffold -f Item
yarn rw g scaffold -f ItemType

5. Repeat for all other models

I couldn't get UOM working, so I had to rename that one to Uom

6. Migrate your database

I did all of this on a fresh RW project, so at this point I ran a yarn rw prima migrate dev. Depending on what state your project/db is in YMMV

7. Generate types

Finally it should be possible to generate all your types

yarn rw g types

8. Done

Sorry this is so difficult.
Please let me know if this works for you or not 🙂

@dthyresson dthyresson self-assigned this Mar 4, 2022
@mbush92
Copy link

mbush92 commented Mar 4, 2022

@Tobbe thanks! That worked. I have now been able to scaffold everything and get the backend working.

@dthyresson
Copy link
Contributor

dthyresson commented Mar 16, 2022

I think this BigInt error is a by-product of the entire schema not loading properly due to the dependency on types. The main issue is: #4762

So, while the error happens -- it isn't a cause, but an effect

Will test that can still use BigInts in a schema with a single table and also in some table with relations.

@dthyresson dthyresson moved this from Todo to In Progress in v1 DX Improvements Mar 21, 2022
Repository owner moved this from In Progress to Done in v1 DX Improvements Apr 23, 2022
@nickpdemarco
Copy link
Contributor

Suggestion: this ticket is marked as closed by #4849 but that's really just a workaround (better error messages). Can we reopen this ticket in favor of a fix that doesn't require @Tobbe's laborious (but extremely helpful) workaround above?

@dac09
Copy link
Contributor

dac09 commented May 9, 2022

Can do @nickpdemarco, but I'm not sure there's an obvious path to resolving this. We'd have to somehow detect the order of relations to generate, then step through that.

My worry with this is the extra complexity it might introduce, but open to ideas.

And extra complexity === more things that break

@Tobbe
Copy link
Member Author

Tobbe commented May 9, 2022

@dac09 For my original problem, that I have in the issue description, no matter what order you generate stuff in, it won't work. Our generators simply can't handle this.

@nickpdemarco Two questions for you. 1) There are a few different issues mentioned across the posts here. Could you please post a reproduction/description of the issue you're thinking of? 2) How would you like it to work?/What would you like our generators to do?

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