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
52 stars 5 forks source link

Issue: Error When Extending GraphQL Types with drizzleEntities(entities) #20

Open jacksonkasi1 opened 2 months ago

jacksonkasi1 commented 2 months ago

Description:

When attempting to extend a GraphQLObjectType using fields from drizzleEntities.types.UsersItem.getFields() in drizzle-graphql, the following error occurs:

Unhandled exception in handler 'server'.
✖ ExtendedUsersItem.id args must be an object with argument names as keys.
Error: ExtendedUsersItem.id args must be an object with argument names as keys.
    at devAssert (path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/jsutils/devAssert.js:12:11)
    at path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/type/definition.js:793:32
    at mapValue (path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/jsutils/mapValue.js:16:19)
    at defineFieldMap (path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/type/definition.js:772:33)
    at GraphQLObjectType._fields (path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/type/definition.js:691:26)
    at GraphQLObjectType.getFields (path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/type/definition.js:710:27)
    at collectReferencedTypes (path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/type/schema.js:387:51)
    at new GraphQLSchema (path-to-project/node_modules/.pnpm/graphql@16.9.0/node_modules/graphql/type/schema.js:179:7)
    at <anonymous> (path-to-project/src/schema.ts:28:16)
✖ ExtendedUsersItem.id args must be an object with argument names as keys.

This issue appears when trying to spread the fields from drizzleEntities.types.UsersItem.getFields() into a new GraphQLObjectType to extend it.

Steps to Reproduce:

  1. Set up a basic GraphQL schema using drizzle-graphql.
  2. Attempt to extend an existing type using drizzleEntities.types.UsersItem.getFields() as follows:
import { env } from "@/env";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { buildSchema } from "drizzle-graphql";

import * as schema from "./schema";

const client = postgres(env.DATABASE_URL);
export const db = drizzle(client, { schema, logger: true });

const { entities } = buildSchema(db);

export const drizzleEntities = entities;
   import { GraphQLObjectType, GraphQLInt, GraphQLString } from "graphql";
   import { drizzleEntities } from "@/db";

   const ExtendedUsersItem = new GraphQLObjectType({
     name: "ExtendedUsersItem",
     fields: {
       ...drizzleEntities.types.UsersItem.getFields(), // Include all default fields

       // Add custom fields
       totalPoints: {
         type: GraphQLInt,
         resolve: (user) => user.totalPoints || 0,
       },
     },
   });

   export { ExtendedUsersItem };
  1. Start the GraphQL server and observe the error.

Expected Behavior:

The ExtendedUsersItem type should be successfully created by extending the fields from UsersItem and adding custom fields like totalPoints without causing any errors.

Actual Behavior:

The server throws an error indicating that the id field (or other fields) "must be an object with argument names as keys." This suggests that getFields() may not be handling or returning the fields in a way that is compatible with the expected GraphQLFieldConfigMap.

Environment:

Additional Context:

Temporary Workaround:

Manually adding the fields from UsersItem instead of using getFields() works without errors, but this is not ideal for maintaining the schema as it requires manual updates when fields change.

Request:

Please investigate whether there is a compatibility issue with getFields() when using drizzle-graphql, or if there are specific steps that should be taken to avoid this error.

jacksonkasi1 commented 2 months ago

Please check this to reproduce the issue: https://github.com/jacksonkasi1/drizzle-graphql-lambda

Sukairo-02 commented 2 months ago

Seems like it's not the intended way to use .getFields() in the first place, but I'll investigate if there's something I can do about it from my side, sure.

jacksonkasi1 commented 2 months ago

Thanks for looking into it, @Sukairo-02