简体   繁体   English

GraphQL-有条件地确定架构中字段的类型

[英]GraphQL- conditionally determine the type of a field in a schema

I have the following mongoose schema: 我有以下猫鼬模式:

const MessageSchema = new Schema({
    author: {
        account:{
           type:String,
           enum:['employee','admin'],
        },
    id: String,
    }
//other fields
})

Then in my graphql-schemas file, I have the following schema types: 然后在我的graphql-schemas文件中,我具有以下架构类型:

const MessageType = new GraphQLObjectType({
     name: 'Message',
     fields: () => ({
        account: {
           type: AuthorType,
        //resolve method 
        },
        id: {type: GraphQLString},
   })
})

const AuthorType= new GraphQLObjectType({
   name: 'Author',
   fields: () => ({
     account: {
        type://This will either be AdminType or EmployeeType depending on the value of account in db (employee or admin),
        //resolve method code goes here
        }
})

}) })

As indicated in the comments of AuthorType , I need the account field to resolve to Admin or Employee depending on the value of the account field in the database. 正如在本评论表示AuthorType ,我需要的account域来解决,以AdminEmployee取决于价值account场在数据库中。 How do I conditionally determine the type of a field in a schema on the fly? 如何有条件地动态确定架构中的字段类型?

Instead of determining the type on the fly, I restructured my code as shown below: 我没有立即确定类型,而是重新构建了代码,如下所示:

const MessageType = new GraphQLObjectType({
    name: 'Message',
    fields: () => ({
       id:{type:GraphQLString},
        author: {
          type: AuthorType,
          async resolve(parent, args) {
            if (parent.author.account === 'guard') {
                return await queries.findEmployeeByEmployeeId(parent.author.id).then(guard => {
                    return {
                        username: `${guard.first_name} ${guard.last_name}`,
                        profile_picture: guard.profile_picture
                    }
                })
            } else if (parent.author.account === 'admin') {
                return {
                    username: 'Administrator',
                    profile_picture: 'default.jpg'
                }
            }
        }
    },
//other fields
   })
})

const AuthorType = new GraphQLObjectType({
    name: 'Author',
    fields: () => ({
       username: {type: GraphQLString},
       profile_picture: {type: GraphQLString},
  })
 })

Since all I need from the AuthorType is the author's username and profile picture, both employee and administrator have these fields, which I pass to AuthorType . 由于我需要的AuthorType只是作者的用户名和个人资料图片,因此员工和管理员都具有这些字段,这些字段我传递给AuthorType In MessageType , I apply the logic to determine account type in the resolve method of author , then construct custom object out of the logic, to match AuthorType . MessageType ,我将逻辑应用于authorresolve方法中确定帐户类型,然后从逻辑中构造自定义对象,以匹配AuthorType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GraphQL-“类型为“查询”的字段“allNews”上的“未知参数“国家/地区”。”, - GraphQL-"Unknown argument \"country\" on field \"allNews\" of type \"Query\".", Graphql-如何根据字段的条件获取结果? - Graphql- How to fetch result based on the condition of a field? Graphql:完全更改架构中的字段类型 - Graphql: completely change field type in schema 如何在 GraphQL 模式中使用“十进制”作为字段类型? - How to use 'Decimal' as a field type in GraphQL schema? GraphQL - 有条件地获取字段 - GraphQL - fetch a field conditionally GraphQL / Relay架构无法在“CreateLinkPayload”类型上查询字段“store” - GraphQL/Relay Schema Cannot query field “store” on type “CreateLinkPayload” graphql-具有不同参数的相同查询 - graphql- same query with different arguments 在 GraphQL 模式中创建类型时是否可以重命名字段? - Is it possible to rename a field when creating a type within a GraphQL schema? GraphQL 模式 - 是否可以在自定义类型字段上设置约束 - GraphQL schema - is it possible to set constraints on a custom type field NestJs - Graphql 错误无法确定 GraphQL 输入类型<field> . 确保您的 class 已使用合适的装饰器进行装饰</field> - NestJs - Graphql Error Cannot determine a GraphQL input type for the <Field>. Make sure your class is decorated with an appropriate decorator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM