sqlmancer icon indicating copy to clipboard operation
sqlmancer copied to clipboard

Missing directives (NestJS integration)

Open unlight opened this issue 4 years ago • 6 comments

Following 'Getting Started' documentation, I'm getting error

(node:868) UnhandledPromiseRejectionWarning: Error: Unknown argument "dialect" on directive "@sqlmancer".

Unknown argument "table" on directive "@model".

Unknown argument "pk" on directive "@model".
import { schemaDirectives} from 'sqlmancer';
console.log('schemaDirectives', schemaDirectives);

image

Looks like it's not all from https://sqlmancer.netlify.app/directives At least model, sqlmancer are missing.

[email protected]

unlight avatar Jun 14 '20 18:06 unlight

Hi @unlight Thanks for the bug report.

The directives included in schemaDirectives will not include all possible directives -- only the ones that actually transform the schema as opposed to just conveying some information about your database. So what you're seeing in the console is accurate.

The errors you're seeing indicates a possible issue with the SDL type definitions for the directives. If you're not using makeSqlmancerSchema, then the type definitions have to be added manually as shown here

import { typeDefs, schemaDirectives } from 'sqlmancer';
import { ApolloServer } from 'apollo-server';

const apollo = new ApolloServer({
  typeDefs: [yourTypeDefs, typeDefs],
  resolvers: yourResolvers,
  schemaDirectives: { ...yourSchemaDirectives, ...schemaDirectives },
});

Can you share a more complete example of how you're creating your schema, including any imports?

danielrearden avatar Jun 14 '20 19:06 danielrearden

I'm using NestJS with schema first approach (I tried code first, but seems it's not yet supported).

Main file looks like

import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { schemaDirectives } from 'sqlmancer';

import { UserModule } from './user/user.module';

@Module({
    imports: [
        UserModule,
        GraphQLModule.forRoot({
            typePaths: [`${__dirname}/**/*.graphql`],
            schemaDirectives: {
                ...schemaDirectives,
            },
        }),
    ],
})
export class AppModule {}

So, there is no typeDefs or resolvers mentions in NestJS tutorials, however GraphQLModule.forRoot can accept them according to signature. typeDefs, resolvers is generated somewhere under the hood of NestJs from decorated classes/methods.

unlight avatar Jun 14 '20 20:06 unlight

I'm not that familiar with NestJS, but I believe if you provide both typePaths and typeDefs, the resulting type definitions should be merged together. So you should be able to do:

import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { schemaDirectives, typeDefs } from 'sqlmancer';

import { UserModule } from './user/user.module';

@Module({
    imports: [
        UserModule,
        GraphQLModule.forRoot({
            typePaths: [`${__dirname}/**/*.graphql`],
            schemaDirectives,
            typeDefs,
        }),
    ],
})
export class AppModule {}

danielrearden avatar Jun 14 '20 22:06 danielrearden

Thank you, that's worked. But NestJS's GraphQLModule typeDefs expects string

typeDefs: typeDefs.loc!.source.body,

unlight avatar Jun 15 '20 07:06 unlight

@unlight As a temporary workaround, you can do

import { print } from 'graphql'
...
typeDefs: print(typeDefs), 

I can export the typeDefs as a string in the next release.

danielrearden avatar Jun 15 '20 07:06 danielrearden

It would also be worthwhile to document NestJS integration alongside any other commonly-used libraries

danielrearden avatar Jun 15 '20 08:06 danielrearden