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

Hiding certain fields #216

Open
tonyfromundefined opened this issue Sep 9, 2019 · 3 comments
Open

Hiding certain fields #216

tonyfromundefined opened this issue Sep 9, 2019 · 3 comments
Labels
needs/discussion Open-ended conversation about something (ideation, design, analysis, ...) scope/backing-types type/feat Add a new capability or enhance an existing one

Comments

@tonyfromundefined
Copy link

tonyfromundefined commented Sep 9, 2019

Hello. I wanted to discuss a new feature and created an issue.

Currently Nexus seems to be optimized for creating schema following the table model in the DB. But, I want to hide certain fields in table to users.

I'll briefly explain it through code.

import { objectType } from 'nexus'

const Image = objectType({
  name: 'Image',
  definition(t) {
    t.id('id')
    t.string('filepath')
  }
})

const User = objectType({
  name: 'User',
  definition(t) {
    t.id('id')
    t.int('imageId')
    t.field('image', {
      type: 'Image',
      resolve: (parent) => {
        // get image item in db by parent.imageId
      },
    })
  }
})

If I want to hide imageId in User type,

import { objectType } from 'nexus'

const Image = objectType({
  name: 'Image',
  definition(t) {
    t.id('id')
    t.string('filepath')
  }
})

const User = objectType({
  name: 'User',
  definition(t) {
    t.id('id')
    // t.int('imageId')
    t.field('image', {
      type: 'Image',
      resolve: (parent) => {
        // parent.imageId -> **TypeError**
        // get image item in db by parent.imageId
      },
    })
  }
})

There is a TypeError when get property from parent although the property exist.
So I want to discuss some new features that can hide certain fields.

I'll briefly explain it through code.

import { objectType } from 'nexus'

const Image = objectType({
  name: 'Image',
  definition(t) {
    t.id('id')
    t.string('filepath')
  }
})

const User = objectType({
  name: 'User',
  definition(t) {
    t.id('id')
    t.int('imageId', {
       /* feature request */
      hide: true,
    })
    t.field('image', {
      type: 'Image',
      resolve: (parent) => {
        // get image item in db by parent.imageId
      },
    })
  }
})

And we can safely get parent.imageId. And also, can hide certain fields that users should not know.

If there is a solution that can solve this problem, please let me know.

@jasonkuhrt jasonkuhrt added the type/feat Add a new capability or enhance an existing one label Sep 10, 2019
@jasonkuhrt
Copy link
Member

jasonkuhrt commented Sep 10, 2019

If there is a solution that can solve this problem, please let me know.

There is a solution now in the form of configuring so-called root types. Take a look here https://nexus.js.org/docs/type-generation#root-types.

I think we should treat this issue as a discussion about having a backing types DSL, which would be a new feature.

@jasonkuhrt jasonkuhrt added scope/backing-types needs/discussion Open-ended conversation about something (ideation, design, analysis, ...) labels Sep 10, 2019
@tonyfromundefined
Copy link
Author

I found and solved by the typegenAutoConfig setting. I think this feature is so good. However, this part seems to be the most powerful feature on Nexus, which I didn't find in Documentation. In that sense, the table of contents in the Documentation should be clearer.

And the issue of hiding certain fields to support backing types would be better covered in the @Weakky's issue of Backing types.

@opensourcekam
Copy link

opensourcekam commented Nov 30, 2019

from objectType.d.ts in nexus Prisma source

 /**
     * Pick, filter or customize the fields of the underlying object type
     * @param inputFields The fields you want to pick/filter/customize
     *
     * @example Exposes all fields
     *
     * t.prismaField(['*'])
     *
     * @example Exposes only the `id` and `name` field
     *
     * t.prismaField(['id', 'name'])
     *
     * @example Exposes only the `id` and `name` field (idem-potent with above example)
     *
     * t.prismaFields({ pick: ['id', 'name'] })
     *
     * @example Exposes all fields but the `id` and `name`
     *
     * t.prismaFields({ filter: ['id', 'name'] })
     *
     * @example Exposes the only the `users` field, and alias it to `customers`
     *
     * t.prismaFields([{ name: 'users', alias: 'customers' }])
     *
     * @example Exposes only the `users` field, and only the `first` and `last` args
     *
     * t.prismaFields([{ name: 'users', args: ['first', 'last'] }])
     *
     */
    prismaFields(inputFields: AddFieldInput<'objectTypes', TypeName>): void;

For those looking to hide fields in resolvers

The Prisma api supports t.prismaFields({ filter: [ excluded ] })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs/discussion Open-ended conversation about something (ideation, design, analysis, ...) scope/backing-types type/feat Add a new capability or enhance an existing one
Projects
None yet
Development

No branches or pull requests

3 participants