【发布时间】:2022-10-23 20:39:13
【问题描述】:
我正在尝试参考钱输入我的大车键入我尝试了几种不同的方法并不断收到此错误:
:Ref function String() { [native code] } 尚未实现
在我的项目中,我将我的类型作为文档中推荐的类导入,
我的类型:
这与我的服务器位于单独的文件中
export class CartItem {
id: string;
name: string;
price: number;
quantity: number;
constructor(id: string, name: string, price: number, quantity: number) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
}
export class Cart {
id: string;
items?: CartItem[];
constructor(id: string, items?: CartItem[]) {
this.id = id;
this.items = items;
}
}
export class Money {
amount: number;
formatted: string;
constructor(amount: number, formatted: string) {
this.amount = amount;
this.formatted = formatted;
}
}
这是我的服务器:
我有两个 cmets 显示我可能失败的地方
- 在我的购物车中
- 常量 MoneyType
import { createServer } from '@graphql-yoga/node' import SchemaBuilder from "@pothos/core" import { CartItem, Cart, Money } from 'gql'; const CARTS = [ { id: '1', items: [ { id: '1', name: 'Item 1', price: 10, quantity: 1 }, { id: '2', name: 'Item 2', price: 20, quantity: 2 } ] }, { id: '2', items: [ { id: '3', name: 'Item 3', price: 30, quantity: 3 }, { id: '4', name: 'Item 4', price: 40, quantity: 4 } ] } ] const builder = new SchemaBuilder({}); builder.objectType(Cart, { name: "Cart", description: "A cart", fields: (t) => ({ id: t.exposeString('id', {}), items: t.field({ type: [CartItem], resolve: (cart) => cart.items ?? [], }), // This is the field that we want to USE TO REFERENCE // subTotal: t.field({ // type: Money, // resolve: (cart) => { // const total = cart.items?.reduce((acc, item) => acc + item.price * item.quantity, 0) ?? 0; // return new Money(total, `$${total}`); // } // }) }), }); builder.objectType(CartItem, { name: "CartItem", description: "A cart item", fields: (t) => ({ id: t.exposeString('id', {}), name: t.exposeString('name', {}), price: t.exposeInt('price', {}), quantity: t.exposeInt('quantity', {}), }), }); // make a reference to the Money type THAT DOESEN'T WORK const MoneyType = builder.objectRef<MoneyShape>("Money"); builder.objectType(Money, { name: "Money", description: "A money", fields: (t) => ({ amount: t.exposeInt('amount', {}), formatted: t.field({ type: String, resolve: (money) => new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(money.amount), }), }), }); builder.queryType({ fields: (t) => ({ cart: t.field({ type: Cart, nullable: true, args: { id: t.arg.id({ required: true, description: "the id of the cart" }), }, resolve: (_, { id }) => { const cart = CARTS.find((cart) => cart.id === id); if (!cart) { throw new Error(`Cart with id ${id} not found`) } return cart } }), carts: t.field({ type: [Cart], resolve: () => CARTS }), }), }) const server = createServer({ endpoint: '/api', schema: builder.toSchema(), }) export default server;
【问题讨论】:
标签: graphql apollo-server typegraphql