drizzle-graphql icon indicating copy to clipboard operation
drizzle-graphql copied to clipboard

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

Open jacksonkasi1 opened this issue 6 months ago • 3 comments

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/[email protected]/node_modules/graphql/jsutils/devAssert.js:12:11)
    at path-to-project/node_modules/.pnpm/[email protected]/node_modules/graphql/type/definition.js:793:32
    at mapValue (path-to-project/node_modules/.pnpm/[email protected]/node_modules/graphql/jsutils/mapValue.js:16:19)
    at defineFieldMap (path-to-project/node_modules/.pnpm/[email protected]/node_modules/graphql/type/definition.js:772:33)
    at GraphQLObjectType._fields (path-to-project/node_modules/.pnpm/[email protected]/node_modules/graphql/type/definition.js:691:26)
    at GraphQLObjectType.getFields (path-to-project/node_modules/.pnpm/[email protected]/node_modules/graphql/type/definition.js:710:27)
    at collectReferencedTypes (path-to-project/node_modules/.pnpm/[email protected]/node_modules/graphql/type/schema.js:387:51)
    at new GraphQLSchema (path-to-project/node_modules/.pnpm/[email protected]/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:

  • drizzle-graphql version: ^0.8.4
  • drizzle-orm version: ^0.33.0
  • GraphQL version: ^16.9.0
  • Node.js version: v20.16.0
  • Operating System: Win 11

Additional Context:

  • The error seems related to how the fields are being spread into the ExtendedUsersItem type.
  • When manually specifying the fields, the issue does not occur, which suggests there may be an issue with how getFields() interacts with drizzle-graphql.

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 avatar Aug 17 '24 09:08 jacksonkasi1