ardatan / graphql-tools

:wrench: Utility library for GraphQL to build, stitch and mock GraphQL schemas in the SDL-first approach
https://www.graphql-tools.com
MIT License
5.35k stars 814 forks source link

VisitableSchemaType is no longer exported #837

Closed alvis closed 4 years ago

alvis commented 6 years ago

Due to https://github.com/apollographql/graphql-tools/commit/51d2ccf4953567267d69f505b752fba18a100254, VisitableSchemaType is no longer exported from index.ts. While it is not a problem for js users, it introduces an issue for typescript users.

The problem is that the constructor function of SchemaDirectiveVisitor takes a VisitableSchemaType as follow https://github.com/apollographql/graphql-tools/blob/248527d497aa1fe5ff5648e94bf38366121d9d92/src/schemaVisitor.ts#L668-L674

This lead to an extra import { VisitableSchemaType } from 'graphql-tools/dist/schemaVisitor'; for users who use SchemaDirectiveVisitor with typescript.

A simple fix is to re-export VisitableSchemaType. But a better fix is to create an interface for the config variable and export it.

aliok commented 5 years ago

Because of this problem, we're having trouble when writing tests for our directive.

export class HasRoleDirective extends SchemaDirectiveVisitor {...}

const directive = new HasRoleDirective({
  name: 'testHasRoleDirective',
  args: directiveArgs
})

Ends up:

error TS2674: Constructor of class 'SchemaDirectiveVisitor' is protected and only accessible within the class declaration.

Ok, let's add the constructor:

import { VisitableSchemaType } from 'graphql-tools/dist/schemaVisitor'

export class HasRoleDirective extends SchemaDirectiveVisitor {

  constructor (config: {
    name: string
    args: { [name: string]: any }
    visitedType: VisitableSchemaType
    schema: GraphQLSchema
    context: { [key: string]: any }
  }) {
    super(config)
  }
...
}

Output this time:

error TS2345: Argument of type '{ name: string; args: { [name: string]: any; }; visitedType: import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-keycloak/node_modules/graphql-tools/dist/schemaVisitor").VisitableSchemaType; schema: import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/pack...' is not assignable to parameter of type '{ name: string; args: { [name: string]: any; }; visitedType: import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-server/node_modules/graphql-tools/dist/schemaVisitor").VisitableSchemaType; schema: import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/node_m...'.
  Types of property 'visitedType' are incompatible.
    Type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-keycloak/node_modules/graphql-tools/dist/schemaVisitor").VisitableSchemaType' is not assignable to type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-server/node_modules/graphql-tools/dist/schemaVisitor").VisitableSchemaType'.
      Type 'GraphQLSchema' is not assignable to type 'VisitableSchemaType'.
        Type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-keycloak/node_modules/@types/graphql/type/schema").GraphQLSchema' is not assignable to type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/node_modules/@types/graphql/type/schema").GraphQLSchema'.
          Types of property 'astNode' are incompatible.
            Type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-keycloak/node_modules/@types/graphql/tsutils/Maybe").default<import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-keycloak/node_modules/@types/graphql/language/ast").SchemaDefiniti...' is not assignable to type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/node_modules/@types/graphql/tsutils/Maybe").default<import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/node_modules/@types/graphql/language/ast").SchemaDefinitionNode>'.
              Type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/packages/apollo-voyager-keycloak/node_modules/@types/graphql/language/ast").SchemaDefinitionNode' is not assignable to type 'import("/home/aliok/Desktop/code-redhat/aerogear/apollo-voyager-server/node_modules/@types/graphql/language/ast").SchemaDefinitionNode'.
                Types of property 'directives' are incompatible.
                  Type 'ReadonlyArray<DirectiveNode> | undefined' is not assignable to type 'ReadonlyArray<DirectiveNode>'.
                    Type 'undefined' is not assignable to type 'ReadonlyArray<DirectiveNode>'.

Please make the constructor public and also export VisitableSchemaType otherwise it is problematic to create an instance of a directive in typescript.

richiemccoll commented 5 years ago

@alvis I'm happy to pick this up if you're still looking to get this fixed.

yaacovCR commented 4 years ago

Closed by #1307, rolled into #1306