2.0.3 • Published 5 years ago

graphql-fields v2.0.3

Weekly downloads
42,632
License
MIT
Repository
github
Last release
5 years ago

graphql-fields

Turns GraphQLResolveInfo into a map of the requested fields. Flattens all fragments and duplicated fields into a neat object to easily see which fields were requested at any level. Takes into account any @include or @skip directives, excluding fields/fragments which are @include(if: $false) or @skip(if: $true).

Usage

Schema Type definition

const graphqlFields = require('graphql-fields');
const graphql = require('graphql')

const UserType = new graphql.GraphQLObjectType({
    name: 'User',
    fields: {
        profile: {type: new graphql.GraphQLObjectType({
          name: 'Profile',
          fields: {
            firstName: {type: graphql.GraphQLString},
            lastName: {type: graphql.GraphQLString},
            middleName: {type: graphql.GraphQLString},
            nickName: {type: graphql.GraphQLString},
            maidenName: {type: graphql.GraphQLString}
          }
        }),
        email: {type: graphql.GraphQLString},
        id: {type: graphql.GraphQLID}
    }
});

module.exports = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: 'Query',
        fields: () =>
            Object.assign({
                user: {
                    type: UserType,
                    resolve(root, args, context, info) {
                        console.log(
                            JSON.stringify(graphqlFields(info), null, 2);
                        );
                        ...
                    }
                }
            })
    })
})

Query

{
  user {
    ...A
    profile {
      ...B
      firstName
    }
  }
}

fragment A on User {
  ...C
  id,
  profile {
    lastName
  }
}

Fragment B on Profile {
  firstName
  nickName @skip(if: true)
}

Fragment C on User {
  email,
  profile {
    middleName
    maidenName @include(if: false)
  }
}

will log

{
  "profile": {
    "firstName": {},
    "lastName": {},
    "middleName": {}
  },
  "email": {},
  "id": {}
}

subfields arguments

To enable subfields arguments parsing, you'll have to provide an option object to the function. This feature is disable by default.

const graphqlFields = require('graphql-fields');
const fieldsWithSubFieldsArgs = graphqlFields(info, {}, { processArguments: true });

For each subfield w/ arguments, a __arguments property will be created. It will be an array with the following format:

[
    {
        arg1Name: {
            kind: ARG1_KIND,
            value: ARG1_VALUE,
        },
    },
    {
        arg2Name: {
            kind: ARG2_KIND,
            value: ARG2_VALUE,
        }
    }
]

The kind property is here to help differentiate value cast to strings by javascript clients, such as enum values.

Exclude specific fields

Most of the time we don't need __typename to be sent to backend/rest api, we can exclude __typename using this:

const graphqlFields = require('graphql-fields');
const fieldsWithoutTypeName = graphqlFields(info, {}, { excludedFields: ['__typename'] });

Why

An underlying REST api may only return fields based on query params.

{
  user {
    profile {
      firstName
    },
    id
  }
}

should request /api/user?fields=profile,id

while

{
  user {
    email
  }
}

should request /api/user?fields=email

Implement your resolve method like so:

resolve(root, args, context, info) {
    const topLevelFields = Object.keys(graphqlFields(info));
    return fetch(`/api/user?fields=${topLevelFields.join(',')}`);
}

Tests

npm test
@caravinci/arranger-serversufferfest-api@prisma-tools/select@stefancfuchs/apolar-webcrawlersanar-residencia-app-bff@coderich/autographqlenterprise-access-api@goodcastle/reaction-dummy-data@coderich/autograph-serverprisma-selectnexus-schema-prisma@jakeblaxon-graphql-mesh/mysql@jakeblaxon-graphql-mesh/transform-cachegraphql-mongodb-adapter@iharob/graphql-mesh-transform-cache@iharob/graphql-mesh-mysql@bongusagi/api-core@rainbunny/api-core@kaviar/novacompass-eth-db@eavios/nestjs-query-graphql@infinitebrahmanuniverse/nolb-graphql-f@goodcastle/api-plugin-catalogs@uponcommerce/core@everything-registry/sub-chunk-1785@iwci/novaxr3ngineyonderbox-graphql-mongodb-adapteryugipedia-gqlwarthogwertik-jstypeorm-graphql-crud@eventiva/databaseclasses@eventiva/internals@enigmatis/polaris@geckorent/api-plugin-catalog-groups@geckorent/api-plugin-catalogs@geckorent/api-plugin-referrals@geckorent/api-utils@ferlab/arranger-mapping-utils@inst-app/apollo-api@homiesglobal/ck@graphql-mesh/transport-mysql@graphql-mesh/transport-rest@itleadopencommerce/api-plugin-catalogs@itleadopencommerce/api-plugin-favorites@itleadopencommerce/api-utils@itleadopencommerce/reaction-marketplace@ericpereiraglobalsys/core@gseller/corescraper-utils2streaming-api@metmirr/warthog@omnigraph/mysql@kfarranger/mapping-utils@idigi/reaction-plugin-catalogs@loveappio/api-plugin-catalogs-arteiras@outgrowio/reaction-dummy-data@outgrowio/reaction-marketplace@overture-stack/arranger-server@oxwazz/create-simple-be@moogs/query-graphql@oridune/epic-plugin-core@oridune/epic-plugin-graphqlxrengine@goopil/nestjs-query-graphql@goodcastle/api-utils@joystream/warthog@nestjs-query/query-graphql@nerjs/gql@neon.id/nova@nexys/koa-lib@nexys/core@nexys/fetchr@pie-api-gcp/graphql@project-alcatraz/graphql-mesh-mysql@project-alcatraz/graphql-mesh-transform-cache@ptc-org/nestjs-query-graphql@razee/razeedash-api@reactioncommerce/api-utils@reactioncommerce/api-plugin-catalogs@roshanreacts/mercury@nebo.digital/query-graphql@prisma-generators/nestjs@vendure/core@vizorous/nestjs-query-graphqlexpress-graphql-fields@ssquid/warthog@sturdyexchange/nestjs-query-graphql@subsquid/warthog@idigi/api-utils@mercury-js/core@livemarketplace/api-plugin-catalogs@medicallicensing/nestjs-query-graphql@tienpvse/query-graphql@snapshot-labs/checkpoint@sithswap/checkpoint@rezonate/nestjs-query-graphql@sigmast/core@renanhangai/miniservices
2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.3.0

5 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago