benawad / type-graphql-series

Typescript GraphQL Server built with TypeGraphQL
326 stars 129 forks source link

The 'parent' element is declared, but its value is never read. #29

Closed whakaoo closed 2 years ago

whakaoo commented 2 years ago

Dear Ben Awad, I'm also trying the code in your second lesson (register resolver TypeGraphql) and I went in two different issues. One I solved, the other not yet. Solved: createConnection() in typeorm is deprecated so I removed the ormconfig.json file and inserted in index.ts the following code:

import { DataSource } from "typeorm" export const AppDataSource = new DataSource({ type: "postgres", host: "localhost", port: 5432, username: "postgres", password: "test", database: "nodedemo", synchronize: true, logging: true, entities: ["src/entity/."], subscribers: [], migrations: [], })

and therefore the following to initialize the connection

const main = async () => { await AppDataSource.initialize() .then(() => { // here you can start to work with your database }) .catch((error) => console.log(error))

(....etc)

Everything is good. Query and mutation are working fine with the db but if I have still a problem with the variable 'name' as I get the following error "The 'parent' element is declared, but its value is never read.

I used your code so that I have in Register.ts

@Resolver(User) export class RegisterResolver { @Query(() => String) async helloWorld() { return "Hello World!"; }

@FieldResolver() async name(@Root() parent: User) { return '${parent.firstName} ${parent.lastName}'; } (...etc...)

and in User.ts

@ObjectType() @Entity() export class User extends BaseEntity { @Field(() => ID) @PrimaryGeneratedColumn() id: number

@Field()
@Column()
firstName: string

@Field()
@Column()
lastName: string

@Field()
@Column("text", {unique: true})
email: string

@Field()
name: string;

@Column()
password: string

}

whakaoo commented 2 years ago

Solved! I was using ' ' and/or " " instead of using backtick, a symbol that is not available in the italian keyboard.