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

[Bug?]: Prisma Model with Bytes field fails SDL generation in Service Test template #9773

Closed
1 task
dthyresson opened this issue Dec 28, 2023 · 4 comments · Fixed by #9847
Closed
1 task

Comments

@dthyresson
Copy link
Contributor

dthyresson commented Dec 28, 2023

What's not working?

Given the following Prisma schema for a Postgres datasource:

model Key {
  id        Int   @id @default(autoincrement())
  publicKey Bytes
}

after migrating, when generating the sdl:

yarn rw g sdl Key  

The command fails:

Reverted because: Error applying template at .../dist/commands/generate/service/templates/test.ts.template for
Key: Cannot read properties of undefined (reading 'replace')

Likely due to use of replace in transformValue of the packages/cli/src/commands/generate/service/templates/test.ts.template file:

// Transforms an object or single value into something that's more suitable
   // for generating test cases
   // If a `type` is passed in, the string for creating an object of that type
   // will be generated
   // If no type, or a type we don't support, is passed in we'll default to
   // generating regular strings
   // Looks for quoted strings, either by single (') or double (") quotes.
   // When found
   // - Removes the quotes around `scenario` variables.
   // - Removes the quotes around `BigInt` fields.
  const transformValue = (obj, type) => {
    if (type === 'DateTime') {
      return `new Date('${obj.toISOString()}')`
    } else if (type === 'Decimal') {
      return `new Prisma.Decimal(${obj})`
    }

    return JSON.stringify(obj).replace(/['"].*?['"]/g, (string) => {
      if (string.match(/scenario\./)) {
        return string.replace(/['"]/g, '')
      }

      // BigInt
      if (string.match(/^\"\d+n\"$/)) {
        return string.substr(1, string.length - 2)
      }

      return string
    })
  } %>

Also, interestingly:

yarn rw g sdl Key --no-tests

Still attempts to generate the test file for the service and gives same error.

How do we reproduce the bug?

  1. Setup a Postgres datasource (needed for Bytes type)
  2. Use the above Key model
  3. yarn rw prima migrate dev
  4. yarn rw g sdl Key

See error when attempting the create the service test template.

Related Issues

See also #5841

What's your environment? (If it applies)

No response

Are you interested in working on this?

  • I'm interested in working on this
@chuckcarpenter
Copy link

Is there a workaround recommended or do I just serialize this as a String?

@Tobbe
Copy link
Member

Tobbe commented Jan 18, 2024

I took a look at fixing the test generation logic. And I did get a bit further. But it's still not working. This is where I'm at now:

Screenshot for easier reading (you know, colors and stuff)
image

Text for easier searching (SEO and stuff)

❯ yarn rw g sdl Key                                       
✔ Generating SDL files...
  ✔ Successfully wrote file `./api/src/graphql/keys.sdl.ts`
  ✔ Successfully wrote file `./api/src/services/keys/keys.scenarios.ts`
  ✔ Successfully wrote file `./api/src/services/keys/keys.test.ts`
  ✔ Successfully wrote file `./api/src/services/keys/keys.ts`
✔ Generating types ...

Schema loading failed. Unknown type: "Bytes".
Error: Unknown type: "Bytes".
    at getNamedType (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/utilities/extendSchema.js:427:13)
    at getWrappedType (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/utilities/extendSchema.js:442:12)
    at getWrappedType (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/utilities/extendSchema.js:439:45)
    at buildInputFieldMap (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/utilities/extendSchema.js:548:22)
    at fields (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/utilities/extendSchema.js:757:25)
    at resolveObjMapThunk (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/type/definition.js:504:40)
    at defineInputFieldMap (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/type/definition.js:1313:20)
    at GraphQLInputObjectType.getFields (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/type/definition.js:1276:27)
    at collectReferencedTypes (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/type/schema.js:395:51)
    at new GraphQLSchema (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/type/schema.js:174:9)
    at buildASTSchema (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/graphql/utilities/buildASTSchema.js:93:10)
    at makeExecutableSchema (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@graphql-tools/schema/cjs/makeExecutableSchema.js:73:47)
    at mergeSchemas (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@graphql-tools/schema/cjs/merge-schemas.js:32:63)
    at getSchemaFromSources (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@graphql-tools/load/cjs/schema.js:56:46)
    at loadSchema (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@graphql-tools/load/cjs/schema.js:19:12)
    at async generateGraphQLSchema (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/internal/dist/generate/graphqlSchema.js:68:20)
    at async generate (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/internal/dist/generate/generate.js:20:7)
    at async _Task.task [as taskFn] (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/cli/dist/commands/generate/sdl/sdl.js:306:13)
    at async _Task.run (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/listr2/dist/index.cjs:2049:11)
Error: Could not generate GraphQL type definitions (api)
Error: 
      Unable to find any GraphQL type definitions for the following pointers:
        
          - /Users/tobbe/tmp/rw-prisma-bytes-sdl/.redwood/schema.graphql
          
    at prepareResult (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@graphql-tools/load/cjs/load-typedefs.js:93:15)
    at loadTypedefsSync (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@graphql-tools/load/cjs/load-typedefs.js:79:20)
    at loadSchemaSync (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@graphql-tools/load/cjs/schema.js:28:61)
    at getCodegenOptions (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/internal/dist/generate/graphqlCodeGen.js:328:41)
    at runCodegenGraphQL (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/internal/dist/generate/graphqlCodeGen.js:163:19)
    at async generateTypeDefGraphQLApi (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/internal/dist/generate/graphqlCodeGen.js:87:21)
    at async generateTypeDefs (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/internal/dist/generate/typeDefinitions.js:55:7)
    at async generate (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/internal/dist/generate/generate.js:24:7)
    at async _Task.task [as taskFn] (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/@redwoodjs/cli/dist/commands/generate/sdl/sdl.js:306:13)
    at async _Task.run (/Users/tobbe/tmp/rw-prisma-bytes-sdl/node_modules/listr2/dist/index.cjs:2049:11)

@Tobbe
Copy link
Member

Tobbe commented Jan 18, 2024

The original issue description says "Setup a Postgres datasource (needed for Bytes type)", but I think this applies to Sqlite too. The Bytes type is supported across all engines: https://www.prisma.io/docs/orm/reference/prisma-schema-reference#bytes

(Might be worth noting that when the linked issue, #5841, was created back in 2022 Prisma's docs said Bytes was unsupported for sqlite, so maybe that's where this comes from)

I still need to test with sqlite too though, so I'm not 100% sure where RW's support stands yet as regards to Bytes in Sqlite.

@Tobbe
Copy link
Member

Tobbe commented Jan 18, 2024

Tested with SQLite. Same error/issue
image

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

Successfully merging a pull request may close this issue.

3 participants