[英]Implementing GraphQL Global Object Identification in NestJS (code-first approach)
I am trying to implement Global Object Identification described in GraphQL's documentation in NestJS.我正在尝试实现 NestJS 中 GraphQL文档中描述的 Global Object Identification。
1.) I started by creating a Node
interface: 1.) 我首先创建了一个
Node
接口:
import { ID, InterfaceType, Field } from '@nestjs/graphql'
@InterfaceType()
export abstract class Node {
@Field(type => ID)
id: number
}
2.) I implemented it in my model: 2.) 我在我的 model 中实现了它:
import { Table } from "sequelize-typescript";
import { ObjectType } from "@nestjs/graphql";
import { Node } from "src/node/node-interface";
@ObjectType({
implements: Node
})
@Table
export class User extends Model {
// [Class body here...]
}
3.) Then I created a Query that would return users: 3.) 然后我创建了一个返回用户的查询:
import { Resolver, Query} from "@nestjs/graphql";
import { User } from "./user-model";
@Resolver(of => User)
export class UserResolver {
@Query(returns => [Node])
async users() {
let users = await User.findAll();
console.log(users);
return users;
}
}
4.) Then I performed the test query from the documentation: 4.) 然后我执行了文档中的测试查询:
{
__schema {
queryType {
fields {
name
type {
name
kind
}
args {
name
type {
kind
ofType {
name
kind
}
}
}
}
}
}
}
5.) But instead of receiving the proper response: 5.) 但没有收到正确的回复:
{
"__schema": {
"queryType": {
"fields": [
// This array may have other entries
{
"name": "node",
"type": {
"name": "Node",
"kind": "INTERFACE"
},
"args": [
{
"name": "id",
"type": {
"kind": "NON_NULL",
"ofType": {
"name": "ID",
"kind": "SCALAR"
}
}
}
]
}
]
}
}
}
6.) I get this: 6.) 我明白了:
{
"data": {
"__schema": {
"queryType": {
"fields": [
{
"name": "users",
"type": {
"name": null,
"kind": "NON_NULL"
},
"args": []
}
]
}
}
}
}
I have no clue what I am doing wrong.我不知道我做错了什么。 I'd appreciate any help with this.
我将不胜感激任何帮助。
Maybe it's too late, but I'm at Node Resolver node must be nullable也许为时已晚,但我在 Node Resolver node must be nullable
import * as GQL from '@nestjs/graphql';
@GQL.Resolver(() => Node, {})
export class NodeResolver {
@GQL.Query(() => Node, {
name: 'node',
defaultValue: [],
nullable: true,
})
node(
@GQL.Args('id', { type: () => GQL.ID } as GQL.ArgsOptions)
id: Scalars['ID'],
): Promise<Node> {
// Implement
return null;
}
}
result:结果:
{
"name": "node",
"type": {
"name": "Node",
"kind": "INTERFACE",
},
"args": [
{
"name": "id",
"type": {
"kind": "NON_NULL",
"ofType": {
"name": "ID",
"kind": "SCALAR"
}
}
}
]
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.