Webtomizer / typeorm-loader

A database-aware data-loader for use with GraphQL and TypeORM.
82 stars 8 forks source link

RequestError when using this package #3

Closed Dastari closed 5 years ago

Dastari commented 5 years ago

I get the following error whenever I use any of the load functions:

error: { RequestError: Requests can only be made in the LoggedIn state, not the SentClientRequest state at Request.tds.Request.err [as userCallback] (C:\Dev\gema\api\node_modules\mssql\lib\tedious.js:614:19) at Request._this.callback (C:\Dev\gema\api\node_modules\tedious\lib\request.js:61:27) at Connection.makeRequest (C:\Dev\gema\api\node_modules\tedious\lib\connection.js:1606:17) at Connection.execSql (C:\Dev\gema\api\node_modules\tedious\lib\connection.js:1369:12) at Immediate.parent.acquire [as _onImmediate] (C:\Dev\gema\api\node_modules\mssql\lib\tedious.js:838:63) at runCallback (timers.js:697:11) at tryOnImmediate (timers.js:667:5) at processImmediate (timers.js:649:5) at process.topLevelDomainCallback (domain.js:121:23) code: 'EINVALIDSTATE', number: 'EINVALIDSTATE', state: undefined, originalError: { RequestError: Requests can only be made in the LoggedIn state, not the SentClientRequest state at RequestError (C:\Dev\gema\api\node_modules\tedious\lib\errors.js:34:12) at Connection.makeRequest (C:\Dev\gema\api\node_modules\tedious\lib\connection.js:1606:26) at Connection.execSql (C:\Dev\gema\api\node_modules\tedious\lib\connection.js:1369:12) at Immediate.parent.acquire [as _onImmediate] (C:\Dev\gema\api\node_modules\mssql\lib\tedious.js:838:63) at runCallback (timers.js:697:11) at tryOnImmediate (timers.js:667:5) at processImmediate (timers.js:649:5) at process.topLevelDomainCallback (domain.js:121:23) message: 'Requests can only be made in the LoggedIn state, not the SentClientRequest state', code: 'EINVALIDSTATE' }, name: 'RequestError', precedingErrors: [] } query: COMMIT

index.js

import "reflect-metadata";
import { ApolloServer } from "apollo-server-express";
import * as Express from "express";
import { createConnection } from "typeorm";
import { createSchema } from "./utils/createSchema";

import { GraphQLDatabaseLoader } from "typeorm-loader";

const main = async () => {
  const schema = await createSchema();
  const connection = await createConnection(/* loaded from ormconfig.json */);
  const loader = new GraphQLDatabaseLoader(connection);

  const apolloServer = new ApolloServer({
    context: ({ request, response }: any) => ({
      request,
      response,
      loader
    }),
    schema
  });

  const app = Express();

  apolloServer.applyMiddleware({ app });

  app.listen(4000, () => {
    console.log("Server Started on http://localhost:4000/graphql");
  });
};
main();

Then in my Entity:

  @Field(() => [Job])
  async AssignedJobs(
    @Ctx() { loader }: BaseContext,
  ): Promise<Job[]> {
    return loader.loadMany(Job, { NameNo: this.CardNo }) as Promise<
      Job[]
    >;
  }

Any thoughts? Is this a unique issue with using the mssql driver?

voodooattack commented 5 years ago

Hi! Does the issue persist if you load the data using TypeORM directly? It sounds more like a TypeORM connection issue.

This could be related, by the way: tediousjs/tedious#458

Dastari commented 5 years ago

Hi! Does the issue persist if you load the data using TypeORM directly? It sounds more like a TypeORM connection issue.

This could be related, by the way: tediousjs/tedious#458

It does work loading data using TypeORM directly.

I think the issue may be related to TypeORM still using the connection when using the loader directly in the entity @Field. If I move the loader into a @FieldResolver in the @Resolver the issue goes away.

voodooattack commented 5 years ago

What happens if you do something like this?

  @Field(() => [Job])
  async AssignedJobs(
    @Ctx() { loader }: BaseContext,
  ): Promise<Job[]> {
    return new Promise((resolve, reject) => {
        setTimeout(() => loader.loadMany(Job, { NameNo: this.CardNo }).then(resolve, reject), 0); 
    });
  }
voodooattack commented 5 years ago

Assuming this resolved because there was not response.