graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Selectively exclude aggregate and connection query types

Open Moodycomputer opened this issue 3 years ago • 2 comments

I'm using this library (and loving it by the way) to develop an API that will have a wide user base that's not directly related to my team. In the interest of both restricting some user activity and keeping the introspected schema relatively light and straight-forward, I'd like to be able to selectively exclude some of the more complex query types from my generated schema.

Today,

type User {
    name: String
}

generates

type Query {
  users
  usersAggregate
  usersConnection
}

Tomorrow,

I'd like to be able to use the @exclude directive to exclude the aggregate and connection types from the query, so that perhaps

type User @exclude(operations: [READ:AGGREGATE, READ:CONNECTION]) {
    name: String
}

would generate

type Query {
  users
}

Thanks!

Moodycomputer avatar Jul 02 '22 17:07 Moodycomputer

@Moodycomputer While you wait for the team to get back to you, you can accomplish what you want by using graphql-tools to filter out unnecessary fields.

A rudimentary filter to exclude all connection and aggregate fields could be like below. You can modify this to be as specific or generic as you want -- i.e. you may want connections if there are relationship properties on the edge.

import { wrapSchema, FilterTypes } from '@graphql-tools/wrap'

const filter = (fieldName: string) => {
  if (fieldName.endsWith("Connection") || fieldName.endsWith("Aggregate")) {
    return false
  }
  return true
}

const schema = wrapSchema({
  schema: originalSchema,
  transforms: [
    new FilterRootFields((operationName, fieldName, fieldConfig) => filter(fieldName)),
    new FilterObjectFields((typeName, fieldName, fieldConfig) => filter(fieldName)),
    new FilterInterfaceFields((typeName, fieldName, fieldConfig) => filter(fieldName))
  ]
})

You could also check to see if the typename as "Connection" or "Aggregate" in it, but this would remove the ability to search using aggregates in the applicable where field.

litewarp avatar Jul 03 '22 20:07 litewarp

Thank you very much @litewarp! This will definitely tide me over while the request is considered.

Moodycomputer avatar Jul 06 '22 01:07 Moodycomputer

Hey @Moodycomputer, in the current 4.0.0 beta and upcoming stable release, we have added a new @query directive which allows the selective configuration of aggregate queries: https://neo4j.com/docs/graphql-manual/4.0/reference/directives/schema-configuration/type-configuration/#_query

It does not allow the configuration of the connection queries, because that goes against our long term vision of connections becoming the first-class citizen of the Neo4j GraphQL Library, given the way their structure so naturally supports relationships.

So going to close this one now because we have taken this as far as we want to right now. 🙂

darrellwarde avatar Aug 18 '23 06:08 darrellwarde