我正在创建一个简单的CRUD应用程序来学习GraphQL,并使用Supabase实例。所有查询和突变都正常工作,除了一件事外,我无法从我的模式中获得id字段,因为它们在Supabase上是int8类型的,而且GraphQL只支持Int。
当我试图使用类型defs:id中的gql Int类型获取行的Int时,我会得到这个错误
我知道这个解决方案涉及创建自定义标量类型,就像在这个例子中一样,但我不知道如何实现这种类型。而且,我不能在Supabase的一侧更改它,所以我必须找到一种在gql中处理这个问题的方法。如何在GraphQL中处理这种类型?
TypeDefs:
export const typeDefs = `#graphql
type User {
id: Int!
name: String!
email: String!
age: Int!
verified: Boolean!
}
type Todo {
id: Int!
title: String!
description: String!
}
type Query {
# users queries
getAllUsers: [User]
getUser(email: String!): User
# todo queries
getAllTodos: [Todo]
getTodo(id: String!): Todo
}
type Mutation {
createUser(name: String!, email: String!, age: Int!): User
createTodo(title: String!, description: String!): Todo
}
`;解决方案:
import { GraphQLScalarType } from 'graphql';
import { prisma } from '../lib/db.js';
const BigInt = new GraphQLScalarType({
// how do I implement this type?
});
export const resolvers = {
BigInt,
Query: {
getAllUsers() {
return prisma.user.findMany();
},
getUser(parent, args) {
return prisma.user.findUnique({
where: {
email: args.email,
},
});
},
getAllTodos() {
return prisma.todo.findMany();
},
getTodo(parent, args) {
return prisma.todo.findUnique({
where: {
id: args.id,
},
});
},
},
// parent, arge are other arguments that get passes to resolvers automatically
Mutation: {
createUser(parent, args) {
return prisma.user.create({
data: args,
});
},
createTodo(parent, args) {
return prisma.todo.create({
data: args,
});
},
},
};发布于 2022-10-26 01:03:22
通过使用石墨型包解决了这个问题。您只需安装它,然后将所需的类型添加到架构和解析器中。不过,我不太明白我们为什么要这么做。如果有人能解释为什么Supabase使用int8,这不符合graphql的Int,我会很感激的。
https://stackoverflow.com/questions/74201579
复制相似问题