graphql-get-fields

1.0.3 • Public • Published

graphql-get-fields

Transform fields from GraphQL to Mongoose project object

Installation

npm install graphql-get-fields

Usage

with excludeFields

const getFields = require('graphql-get-fields');
 
// Example query
//      query getTodos {
//          todos {
//               userId @include(iftrue)
//               id
//               title
//               completed @skip(iftrue) @include(iffalse)
//               details {
//                    description
//                    createAt
//                    updateAt
//               }
//          }
//      }
 
const fieldNames = getFields(info, {
    excludeFields: ['details.createAt''details.updateAt']
})
 
// Output
// fieldNames = { userId: 1, id: 1, title: 1, 'details.description': 1 }

with onlyFields

const getFields = require('graphql-get-fields');
 
// Example query
//      query getTodos {
//          todos {
//               userId @include(iftrue)
//               id
//               title
//               completed @skip(iftrue) @include(iffalse)
//               details {
//                    description
//                    createAt
//                    updateAt
//               }
//          }
//      }
 
const fieldNames = getFields(info, {
    excludeFields: ['id'],
    onlyFields: ['id''details''details.description']
})
 
// Output
// fieldNames = { id: 1, details: 1, 'details.description': 1 }

Params

  • info: graphql resolve info object (required)
  • options:
    • excludeFields (default: [])
    • onlyFields (default: [])

License

MIT

Package Sidebar

Install

npm i graphql-get-fields

Weekly Downloads

3

Version

1.0.3

License

MIT

Unpacked Size

7.71 kB

Total Files

4

Last publish

Collaborators

  • hoaphatnp