graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 275 forks source link

[Feature] Add Support for Directive filters and Type filters to cover AWS AppSync use-cases #1143

Open malikfaik opened 2 years ago

malikfaik commented 2 years ago

Problem Statement

Current implementation of custom directives and scalars always outputs a definition for the directive/scalar with the developer having no control over it. for e.g. defining aws_lambda directive for certain fields which is a inherently defined / supported directive by AWS App Sync but not by the GraphQL standard will output the directive definition which AWS does not like 🥲, making this library in all it's glory very hard to use with AppSync.

An example of the current output:

directive @aws_lambda on OBJECT

scalar AWSTimestamp

type Query {
  ok: Boolean!
}

type Random @aws_lambda {
  expiry: AWSTimestamp
}

What AWS AppSync would like the output to be:

type Query {
  ok: Boolean!
}

type Random @aws_lambda {
  expiry: AWSTimestamp
}

Fix

To work around the issue and to give more control to the developer I added another optional field to the makeSchema method called filters, which allows the developer to define custom filters for the directives and types which gives them the ability to exclude them from the definition if needed.

makeSchema({
  types: [],
  filters: {    // new config field introduced
    isEnvironmentDefinedDirective: (directive) => {
      return directive.name === 'aws_lambda'
    },
    isEnvironmentDefinedType: (type) => {
      return type.name === 'AWSTimestamp'
    },
  },
  outputs: {
    schema: pathToSchema,
  },
})