我们可以在解析时在 grapqhl 中返回一个字符串或对象吗?

问题描述

我想检查用户是否存在于数据库中...如果用户存在于数据库中则发送

type user{
  name
  email
  password
  username
}

或者如果用户在数据库中不存在,则发送一个字符串,告诉“用户在数据库中不存在”

喜欢....如果我在写tyepDefs

type Query{
 show_data(username: String!): #if user exists then# ***user!*** or #if user doesnot exists then# ***String!***

如何制作这种类型的解析器和 typeDefs ?

解决方法

您可以创建一个新类型以在查询中使用:

type UserPayload {
  state: String! # success or other value
  user: User # if state was not success this must be null
}

然后直接通过您的查询使用该类型,如下所示:

type Query{
 show_data(username: String!):UserPayload 
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...