Nexus: Hiding certain fields

Created on 9 Sep 2019  路  3Comments  路  Source: graphql-nexus/nexus

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.

needdiscussion scopbacking-types typfeat

Most helpful comment

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.

All 3 comments

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.

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.

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 ] })

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StevenLangbroek picture StevenLangbroek  路  6Comments

hatchli picture hatchli  路  5Comments

saostad picture saostad  路  3Comments

capaj picture capaj  路  5Comments

ecwyne picture ecwyne  路  6Comments