Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript TypeGraphQL为关系返回空值_Typescript_Postgresql_Graphql_Typeorm_Typegraphql - Fatal编程技术网

Typescript TypeGraphQL为关系返回空值

Typescript TypeGraphQL为关系返回空值,typescript,postgresql,graphql,typeorm,typegraphql,Typescript,Postgresql,Graphql,Typeorm,Typegraphql,我创建了一个简单的测试项目,试图确定为什么@ManyToMany关系返回空值,而不是关系另一端的对象中的值。我使用的是TypeGraphql、TypeORM和Postgresql 如果我记录查询结果,正确的值将显示在日志中。如果字段设置为nullable,则返回“null”。如果设置为不可为null,则会出现一个错误--“无法为不可为null的字段集合返回null.name。” /收藏 @ObjectType() @Entity() export default class Collection

我创建了一个简单的测试项目,试图确定为什么@ManyToMany关系返回空值,而不是关系另一端的对象中的值。我使用的是TypeGraphql、TypeORM和Postgresql

如果我记录查询结果,正确的值将显示在日志中。如果字段设置为nullable,则返回“null”。如果设置为不可为null,则会出现一个错误--“无法为不可为null的字段集合返回null.name。”

/收藏

@ObjectType()
@Entity()
export default class Collection extends BaseEntity {
  @Field(() => ID, { nullable: true })
  @PrimaryGeneratedColumn()
  id: number;

  @Field()
  @Column()
  name: string;

  @Field(() => [Photo], { nullable: true })
  @OneToMany(() => PhotoCollection, (pc) => pc.collection, { nullable: true })
  photos: Promise<Photo[]>;
}
@ObjectType()
@Entity()
export default class PhotoCollection extends BaseEntity {
  @Field(() => Collection)
  @PrimaryColumn()
  collectionId: number;
  @ManyToOne(() => Collection, (collection) => collection.photos)
  @JoinColumn({ name: "collectionId" })
  collection: Promise<Collection>;

  @Field(() => Photo)
  @PrimaryColumn()
  photoId: number;
  @ManyToOne(() => Photo, (photo) => photo.collections)
  @JoinColumn({ name: "photoId" })
  photo: Promise<Photo>;
}
查询此一对多关系工作正常,所有字段通过以下查询解决:

query {
  photos {
    id
    title
    location {
      id
      name
    }
  }
}
查询多对多将通过以下查询双向解析为null:

query {
  collections {
    id
    name
    photos {
      id
      title
    }
  }
}
尽管日志显示:

collections: [
  {
    "id": 1,
    "name": "first collection",
    "photos": [
      {
        "collectionId": 1,
        "photoId": 1,
        "photo": {
          "id": 1,
          "title": "First Photo"
        }
      }
    ]
  }
]
我意识到一对多和多对多之间的区别在于,我想要在多对多中解析的数据除了被包装在数组中之外,还被包装在一个对象中。我将研究如何解决这个问题

对于其他偶然发现这一点的人来说,这篇文章非常透彻和有用:

什么查询??日志“无法为不可为null的字段集合返回null。名称。”。。。记录的数据(“正确值”)是否包含>>>名称
type PhotoCollection {
  collectionId: Collection!
  photoId: Photo!
}

type Location {
  id: ID!
  name: String!
  photos: [Photo!]!
}

type Photo {
  id: ID!
  title: String!
  location: Location!
  collections: [Collection!]!
}

type Collection {
  id: ID!
  name: String!
  photos: [Photo!]!
}

type Query {
  collections: [Collection!]!
  collection(id: Int!): Collection!
  locations: [Location!]!
  photos: [Photo!]!
}
query {
  photos {
    id
    title
    location {
      id
      name
    }
  }
}
query {
  locations {
    id
    name
    photos {
      id
      title
    }
  }
}
query {
  collections {
    id
    name
    photos {
      id
      title
    }
  }
}
collections: [
  {
    "id": 1,
    "name": "first collection",
    "photos": [
      {
        "collectionId": 1,
        "photoId": 1,
        "photo": {
          "id": 1,
          "title": "First Photo"
        }
      }
    ]
  }
]