Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeing undefined arg data when using imports making some resolvers useless #1243

Closed
smolinari opened this issue Jul 22, 2020 · 11 comments
Closed

Comments

@smolinari
Copy link

smolinari commented Jul 22, 2020

Hi,

As noted in Discord, I think I've possibly found a bug, or my usage of both TypeGraphQL and GraphQL-Modules isn't as it should be. Either way, I'd like someone with much more experience to have a look.

Here is the reproduction: https://github.com/smolinari/gql-modules-test

Follow the readme.md of that repo to get the server running.

Then run this query:

query{getOneRecipe(id: 1){
  id
  title
  description
}}

You can see the error: Cannot return null for non-nullable field Query.getOneRecipe. ( I realize this should be nullable field, however, if I add the null check, then it will just return null, when it should return the recipe.) This error happens because the argument id is undefined getting to the resolver. You'll see, in BaseResolver, I'm console.logging out the id argument and it shows in the terminal as undefined.

If you go to index.ts and comment out the UserModule within the imports property of the GraphQLModule config parameter, then the query will work, which I believe proves the collision happening with importing.

Scott

@smolinari
Copy link
Author

I've changed direction and also found it might be an issue with Apollo too. Closing.

Scott

@smolinari
Copy link
Author

Actually, I'm reopening, as GraphQL-Modules seems to be broken with Type-GraphQL and I'd like to see the two working together. Thanks.

Scott

@smolinari smolinari reopened this Jul 27, 2020
@agustif
Copy link

agustif commented Jul 27, 2020

Actually, I'm reopening, as GraphQL-Modules seems to be broken with Type-GraphQL and I'd like to see the two working together. Thanks.

Scott

Hey @smolinari you have a working implementation by typegraphql author.
I used it to see how to work out graphql+modules + typegraphql+typeorm, it works great as I have only a model file which both declare the db entity (typeorm) and graphql type:https://github.com/MichalLytek/type-graphql/tree/master/examples/graphql-modules

and another example by ardatan from the guild (gql-modules contributor) https://github.com/ardatan/TypeGraphQLModules

Example of my document model/entity/type

import {
  Field,
  ID,
  ObjectType,
} from 'type-graphql';
import {
  BaseEntity,
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
  VersionColumn,
} from 'typeorm';

/**
 * @export
 * @class Document
 * @extends {BaseEntity}
 */
@Entity()
@ObjectType()
export default class Document extends BaseEntity {
  @Field(() => String)
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Field(() => String)
  @Column()
  title: string;

  @Field(() => Boolean)
  @Column({ default: false })
  isPublished: boolean;

  @Field(() => ID)
  @Column()
  authorId: string;
  @Field()
  @Column()
  content: string;
}

@smolinari
Copy link
Author

Yeah, that template used to work. It doesn't since TypeGraphQL went 1.0. See this issue too.

ardatan/TypeGraphQLModules#31

And I see you commented in that issue too, so you are aware of the incompatibility.

Scott

@agustif
Copy link

agustif commented Jul 27, 2020

Yeah, that template used to work. It doesn't since TypeGraphQL went 1.0. See this issue too.

ardatan/TypeGraphQLModules#31

And I see you commented in that issue too, so you are aware of the incompatibility.

Scott

Checkout the other one which is updated for type-graphql 1.0.0rc3 https://github.com/MichalLytek/type-graphql/tree/master/examples/graphql-modules

I can confirm I have typegraphql v1 and graphql-modules "^0.7.18-alpha-6c93635.67", working together

@smolinari
Copy link
Author

smolinari commented Jul 28, 2020

And I can confirm that it doesn't work ( I upgraded to the same version you are using). My use case might be different than yours, so 🤷

Btw, are you using Apollo Server with Express? Or just graphql-express? That might be the key difference too.

Scott

@agustif
Copy link

agustif commented Jul 28, 2020

And I can confirm that it doesn't work ( I upgraded to the same version you are using). My use case might be different than yours, so 🤷

Btw, are you using Apollo Server with Express? Or just graphql-express? That might be the key difference too.

Scott

Yes I'm using Apollo-Server-Express

Your resolvers and mine are very different though

@smolinari
Copy link
Author

They are just using resolver inheritance also taken from the TypeGraphQL examples.

What do your resolvers look like? Can you show an example or link to your code?

Scott

@agustif
Copy link

agustif commented Jul 28, 2020

import {
  Arg,
  Mutation,
  Query,
  Resolver,
} from 'type-graphql';

import { Injectable } from '@graphql-modules/di';

import { CreateDocumentInput } from '../inputs/create.document.input';
import { UpdateDocumentInput } from '../inputs/update.document.input';
import Document from '../models/document.model';
import DocumentProvider from '../providers/document.provider';

/**
 *
 *
 * @export
 * @class DocumentResolver
 */
@Injectable()
@Resolver((of) => Document)
export default class DocumentResolver {
  constructor(private readonly documentProvider: DocumentProvider) {}

  /**
   *
   *
   * @returns
   * @memberof DocumentResolver
   */
  @Query(() => [Document])
  documents() {
    return this.documentProvider.getAll();
  }

  /**
   *
   *
   * @param {string} id
   * @returns
   * @memberof DocumentResolver
   */
  @Query(() => Document)
  document(@Arg("id") id: string) {
    return this.documentProvider.findDocumentById(id);
  }

  /**
   *
   *
   * @param {CreateDocumentInput} data
   * @returns
   * @memberof DocumentResolver
   */
  @Mutation(() => Document)
  createDocument(@Arg("data") data: CreateDocumentInput) {
    return this.documentProvider.create(data);
  }

  /**
   *
   *
   * @param {string} id
   * @param {UpdateDocumentInput} data
   * @returns
   * @memberof DocumentResolver
   */
  @Mutation(() => Document)
  async updateDocument(
    @Arg("id") id: string,
    @Arg("data") data: UpdateDocumentInput
  ) {
    return this.documentProvider.update(data, id)
  }

  /**
   *
   *
   * @param {string} id
   * @returns
   * @memberof DocumentResolver
   */
  @Mutation(() => Boolean)
  async deleteDocument(@Arg("id") id: string) {
    return this.documentProvider.delete(id)
  }

}

@smolinari
Copy link
Author

Thanks for that. Is Document one of many types? So, for each other type, you have to also build those same crud resolvers?

Scott

@smolinari
Copy link
Author

Closing once more. My new direction seems to be working now, so this isn't an issue for me anymore.

Scott

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants