graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
828 stars 118 forks source link

Read-only fields for update mutation #599

Open dkozma opened 4 years ago

dkozma commented 4 years ago

This may be related to #90, however the API has changed quite a bit since that conversation took place. Essentially what I would like is to be able to set certain fields as readonly if they should not be able to be updated by clients.

One of the things I've tried is using computedInputs to set the input for the protected field to undefined, which properly removes it from the input and preserves the value, however it also has the unfortunate side-effect of not letting me use that field in the where condition. For instance:

const Mutation = mutationType({
  definition(t) {
    t.crud.updateOneUser({
      computedInputs: {
        readOnlyID: ({ args, ctx, info }) => {
          return undefined; // preserve the original value
        },
      },
    });
  },
});

results in a message such as:

Variable \"$where\" got invalid value { readOnlyID: \"abcdef-12345\" }; Field \"readOnlyID\" is not defined by type UserWhereUniqueInput."

when I submit mutation data such as:

{
  "data": {"firstName":"John"},
  "where": {"readOnlyID":"abcdef-12345"}
}

I would also rather avoid duplicating all of my writable User fields into arguments in a custom UpdateUser mutation that calls ctx.prisma.user.update(). However, if that is the only way, I would be open to writing a conversion function for this as long as I could get a list of the fields in the User schema programmatically, so I don't have to keep multiple places updated in code.

I would imagine this is a common use case, however the only examples I could find with modifying input were with create mutations. Is there something I am missing?

Edit: https://slack.prisma.io/ is now working for me.

dkozma commented 4 years ago

It also seems like the solution proposed in #584 would also help with providing a way for resolving this issue if there is no built-in functionality available at the moment.

sudall commented 4 years ago

I would be interested in something like this:

schema.mutationType({
  definition(t) {
    t.field(t.crud.createOneUser.fieldName, {
      type: t.crud.createOneUser.type,
      args: t.crud.createOneUser.args,
      resolve: t.crud.createOneUser.resolve,
    });
  },
});

this way you could customize the args (or other properties for that matter) pretty easily

_.pick(t.crud.createOneUser.args, ['whatever', 'columns', 'i', 'want']);
const {columnIWantToExclude, ...rest} = t.crud.createOneUser.args;
const args = {
   ...rest,
   someAdditionalInputField: stringArg(),
};