1.0.3 • Published 6 years ago

graphql-get-fields v1.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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(if: true)
//               id
//               title
//               completed @skip(if: true) @include(if: false)
//               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(if: true)
//               id
//               title
//               completed @skip(if: true) @include(if: false)
//               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