[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Hi, thanks for the library, with the last version I think that the only way to test the resolvers are via e2e tests, there is no way to test only the schema and its validations.
In a few words, what's the buildSchema of @nestjs/graphql ?
I have seen in the code that it's generated here:
const apolloOptions = await this.graphqlFactory.mergeOptions({
...this.options,
typeDefs: mergedTypeDefs,
});
if (this.options.definitions && this.options.definitions.path) {
await this.graphqlFactory.generateDefinitions(
printSchema(apolloOptions.schema),
this.options,
);
}
Is it possible to have access to that apolloOptions.schema ?
I way to build the schema or get it from the TestBed.
Testing
Which approach do you use? Code first or schema first?
Hi @kamilmysliwiec, it's code first, I'm trying to create tests like in type-graphql, in the test bed I'm using the GraphQLModule, is there a way to get that schema object so I can make queries with graphql(schema, mutation) ?
UPDATE:
As I mentioned in the first comment, I found the place where the schema is generated, so after reading a bit I was able to achieve my goal, though is a bit tricky, I think that a better way is to provide this executable schema via a token.
I assigned the apolloOptions to a global variable.
const apolloOptions = yield this.graphqlFactory.mergeOptions(Object.assign(Object.assign({}, this.options), { typeDefs: mergedTypeDefs }));
global.apolloOptions = apolloOptions;
if (this.options.definitions && this.options.definitions.path) {
Then in my tests, though it isn't an e2e test, after compiling I need to start the application to trigger the onModuleInit() module's methods that for GraphQLModule will internally trigger the schema generation setting the apolloOptions global variable, its schema property is the output of graph-tools makeExecutableSchema() and it could interact with the graphql method 馃榿.
```ts
//...
import { graphql } from 'graphql';
import { GqlModuleOptions, GraphQLModule } from '@nestjs/graphql';
describe('integration', () => {
let schema;
let app: INestApplication;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
AuthModuleMock,
GraphQLModule.forRoot({
debug: true,
playground: true,
introspection: true,
autoSchemaFile: 'schema-test.gql',
context: ({ req }) => ({ req })
}),
]
}).compile();
app = module.createNestApplication();
await app.init();
});
it('should return the created user', async () => {
const mutation = `mutation {
signUp(data: {
name: "test"
email: "test"
password: "test"
}) {
user {
email
name
username
}
}
}`;
const apolloOptions: GqlModuleOptions = (global as any).apolloOptions;
const result = await graphql(apolloOptions.schema, mutation);
// `result` is an object with the response data that
// the resolvers inside AuthModuleMock returns.
});
});
````
If you update to the latest version, you will be able to use such a construction:
const { schema } = app.get(GraphQLSchemaHost);
I'll update the docs shortly :)
@kamilmysliwiec I think you forgot to add the GraphQLSchemaHost in the GraphQLFederationFactory
Most helpful comment
@kamilmysliwiec I think you forgot to add the GraphQLSchemaHost in the GraphQLFederationFactory