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 Loading error on Generate SDL or Scaffold with Relation with Type Generation #4762

Closed
dthyresson opened this issue Mar 15, 2022 · 4 comments · Fixed by #4849
Closed
Labels
bug/confirmed We have confirmed this is a bug

Comments

@dthyresson
Copy link
Contributor

dthyresson commented Mar 15, 2022

  1. Create new models with relation (at the same time in a single migration).

Here is a Book that is on a Shelf. And a Shelf has many Books.

model Book {
  id      Int    @id @default(autoincrement())
  title   String @unique
  Shelf   Shelf? @relation(fields: [shelfId], references: [id])
  shelfId Int?
}

model Shelf {
  id    Int    @id @default(autoincrement())
  name  String @unique
  books Book[]
}
  1. yarn rw prisma migrate dev

  2. Now, I want sdls and services.

yarn rw g sdl Book --crud

  1. The sdl and services do generate
 ✔ Generating SDL files...
    ✔ Successfully wrote file `./api/src/graphql/books.sdl.js`
    ✔ Successfully wrote file `./api/src/services/books/books.scenarios.js`
    ✔ Successfully wrote file `./api/src/services/books/books.test.js`
    ✔ Successfully wrote file `./api/src/services/books/books.js`
  1. BUT when generating types, the schema fails to load as Book needs Shelf.

Note: Ignore BigInt warning as that is a separate issue.

  ⠙ Generating types ...
Failed to load schema
Failed to load schema
AggregateError: Something went wrong Failed to load schema for "/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/.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: "Shelf".
        Error: Unknown type: "Shelf".
    at getNamedType (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/utilities/extendSchema.js:427:13)
    at getWrappedType (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/utilities/extendSchema.js:442:12)
    at buildFieldMap (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/utilities/extendSchema.js:483:17)
    at fields (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/utilities/extendSchema.js:672:25)
    at resolveObjMapThunk (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/type/definition.js:504:40)
    at defineFieldMap (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/type/definition.js:760:20)
    at GraphQLObjectType._fields (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/type/definition.js:685:26)
    at GraphQLObjectType.getFields (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/type/definition.js:704:27)
    at collectReferencedTypes (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/type/schema.js:387:51)
    at new GraphQLSchema (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/graphql/type/schema.js:174:9)
    
        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/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/@graphql-codegen/cli/index.js:1078:28)
    at async generate (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/@graphql-codegen/cli/index.js:1388:25)
    at async generateGraphQLSchema (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/@redwoodjs/internal/dist/generate/graphqlSchema.js:27:15)
    at async Task.generate [as task] (/Users/dthyresson/Dropbox/Code/redwoodjs/service-validation-test-project/node_modules/@redwoodjs/internal/dist/generate/generate.js:25:22)

You can get out of this by

yarn rw g sdl Shelf --crud
yarn rw g types

  1. But we could instead catch the error, and make a friendlier message that says there is a relationship dependency and go view this docs link for more info.

This also impact scaffolding.

See also: #4394

@dthyresson
Copy link
Contributor Author

I've done some digging, and because type gen is using the cli version of the codgen, we cannot catch the error and see the type -- the cli outs the error message details.

The option would be to suppress and do some string parsing but that seems impractical.

Eventually, we could use the core parts of codeine instead -- and then it's more likely to be able to detect specific types of errors.

I still want to see if we can have some link to an explanation.

So, I think I will start with a document describing the issue and how you can get out of it (though it is tricky as we know).

@Tobbe
Copy link
Member

Tobbe commented Mar 18, 2022

Eventually, we could use the core parts of codeine instead

I'm going to have a go at that over here: #4728 I plan to start working on that tomorrow

@Tobbe
Copy link
Member

Tobbe commented Mar 20, 2022

I'm going to have a go at that over here: #4728 I plan to start working on that tomorrow

I was mistaken. I switched to using graphql-codegen/core for gql documents. This PR needs it for the schema, which is another function.

@twodotsmax
Copy link
Contributor

twodotsmax commented Apr 6, 2022

Thanks for working on this. One of the core selling points of Redwood is that code gen is easier than DIY. Ideally you don't have to add things one by one carefully checkpointing with git to avoid the scenario where the black box stops working and customers expect the software to work, etc.

Would be nice to have a command that completely wipes out any generated stuff and rebuilds a valid schema and types. Even if that means automating removal of relations, one by one adding of relations, etc. in a command that is prominently documented as part of the CLI such as "If you have an intractable type error in GraphQL that makes no sense please try running yarn rw prisma migrate rebuild"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/confirmed We have confirmed this is a bug
Projects
None yet
4 participants