drizzle-team / drizzle-graphql

Automatically generate GraphQL schema or customizable schema config fields from Drizzle ORM schema
https://www.npmjs.com/package/drizzle-graphql
Apache License 2.0
34 stars 1 forks source link

buildSchema error when using DrizzleD1Database #2

Closed PyroOtter closed 2 months ago

PyroOtter commented 2 months ago

Hello,

I wasn't sure if this was supposed to be supported yet, but I noticed when attempting to use a DrizzleD1Database which extends BaseSQLiteDatabase, it fails in generateSchemaData on line 345 due to trying to use: const schema = db._.fullSchema within the buildSchema method. db._.fullSchema does not appear to exist on the db object.

[wrangler:err] TypeError: Cannot convert undefined or null to object at [object Object] at generateSchemaData (***/node_modules/src/util/builders/sqlite.ts:345:31) at buildSchema (***/node_modules/src/index.ts:19:21)

Let me know if I can provide any other information.

Thanks

Sukairo-02 commented 2 months ago

Which version of Drizzle ORM and are you using, and which version of drizzle-graphql? Current version of drizzle-graphql is meant to work with future version of Drizzle ORM, which is currently in beta, for Drizzle ORM 0.30.8 and earlier use drizzle-graphql 0.2.5.

PyroOtter commented 2 months ago

Thanks for the quick response. I was using drizzle-orm 0.30.8 and drizzle-graphql 0.3.0. I've updated to the latest for both and that seems to have solved the issue.

Chiji1108 commented 2 months ago

Does buildSchema support the D1 adapter? I get the following error with buildSchema.

Error: Unable to extract tables from drizzle instance.
Did you forget to pass tables to graphql schema constructor?

  14 | export async function GET() {
  15 |   const db = drizzle(getRequestContext().env.DB);
> 16 |   const { schema } = buildSchema(db);
     |                                  ^
  17 |   return new Response("OK");
  18 | }
  19 |
Sukairo-02 commented 2 months ago

@Chiji1108 it is as the error says, you didn't pass the schema to the drizzle constructor. So do it like this:

import * as schema from 'db/schema';

export async function GET() {
   const db = drizzle(getRequestContext().env.DB, { schema });
   const { schema: gqlSchema } = buildSchema(db);
   return new Response("OK");
}

then it will work.

Chiji1108 commented 2 months ago

@Sukairo-02 Oh what an elementary mistake I had made. Thank you very much. I fixed it and it works fine.