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

Extend prismaWhere generated Input #837

Open
durdenx opened this issue Mar 14, 2023 · 5 comments
Open

Extend prismaWhere generated Input #837

durdenx opened this issue Mar 14, 2023 · 5 comments

Comments

@durdenx
Copy link

durdenx commented Mar 14, 2023

How can I change the type of the generated input when I use builder.prismaWhere, for example if I want to change (remove or add) a field in the input? Currently I have types issues if I do this, and I have to use exactly the same input type as Prisma.

@durdenx durdenx changed the title Extend prismaWhere generated Input Extend prismaWhere generated Input Mar 14, 2023
@hayes
Copy link
Owner

hayes commented Mar 14, 2023

The prisma-utils package is still pretty early in it's development. If you need to customize the fields beyond what's currently possible, you will likely need to create your own input types manually.

@durdenx
Copy link
Author

durdenx commented Mar 14, 2023

Ok, thanks, I don't need to look any further then! I'll create my own!

@MPiccinato
Copy link

Just ran into this same issue.

  const ThingWhere = t.builder.prismaWhere("Thing", {
    fields: {
      tags: t.builder.prismaFilter(["String"], {
        ops: ["hasSome"],
        name: "TagsFilter",
      }),
      category: GraphQLStringFilter,
      authorId: t.builder.prismaFilter("String", {
        ops: [GraphQLFilterOp.equals],
        name: "AuthorFilter",
      }),
      privacy: t.builder.prismaFilter(Privacy, {
        ops: [GraphQLFilterOp.equals],
        name: "PrivacyFilter",
      }),
      favorited: t.builder.prismaFilter("Boolean", {
        ops: [GraphQLFilterOp.equals],
        name: "FavoritedFilter",
      }),
    },
  });

The favorited field is calculated based on the relationship being present; there is favorites relationship it looks at, using directly made the GraphQL calls a bit awkward.

@hayes
Copy link
Owner

hayes commented Mar 26, 2023

Yeah, this makes sense, and I think it could make sense to support additional fields. It's a little tricky because a big part of these helpers is to ensure that the types are compatible with prisma.

Additional fields can be pulled out, but if you want to add a field that matches an existing prisma field (but use a custom type) there are 2 options which both seem bad:

Either allow you to define any field name as being any type (this would mean losing all type-safety from the prisma schema) or just don't allow you to create custom fields that match properties in your prisma types.

The alternative I've been considering is to create a new method that allows you to extend or modify input types after definition. I don't have any API in mind yet for exactly how this would work.

There are a couple of other ideas I've been considering that might also help:

  • Adding the ability to alias/rename input fields (the plugin would automatically map the alias field to the right prisma property
  • Adding the ability to flatten filters
// this would likely not be the actual API, but roughly shows the ideas from above
 const ThingWhere = t.builder.prismaWhere("Thing", {
    fields: {
      favorites: {
        alias: 'favorite',
        type: t.builder.prismaFlatFilter("Boolean", {
          op: GraphQLFilterOp.equals,
          name: "FavoritedFilter",
        }),
      } 
    },
  });

I'd be interested in what your ideal API would be:

  • What do the GraphQL types look like (GraphQL sdl)
  • What would the arguments/input values look like in the resolver
  • How would you want to define those types with the builder

Another question:

If you are customizing the input types already to not match exactly the shape that prisma expects, what benefit are you hoping for from using prismaWhere over just using a normal input type. The main one I can think of is converting nulls to undefined, but interested to hear if there are other benefits you are looking for

@MPiccinato
Copy link

I might also just be attempting to misuse this API. And I am glad they keep compatibility with Prisma, has been helpful on the client side when trying to implement filtering as I was used to with Hasura.

In order to mix and match as needed I suppose I should just create use builder.inputType and then call implement?

ThingWhere.implement("Thing", {
    fields: (t) => ({
      otherField: t.string({ required: true }),
      favorited: {
        type: t.builder.prismaWhereField("Boolean", {
          op: GraphQLFilterOp.equals,
          name: "FavoritedFilter",
        }),
      } 
    }),
  });

Again I just might be going about this all the wrong way. I am mainly looking for a way to keep the filters strict to a Prisma objects field where I need it and then allow custom inputs along side.

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

No branches or pull requests

3 participants