首页 > 解决方案 > graphqlj-js 联合类型,访问参数

问题描述

我试图弄清楚如何访问args. retrieveRowsWithoutLocations但是,我什至不确定UnionType 函数中的value参数是什么。resolveType

我正在查看文档https://graphql.org/graphql-js/type/#graphqluniontype但它非常简短,并没有详细说明从哪里获取该信息。我试过查看其他来源,但它们不是 graphql-js。

我要做的是访问args.type并检查它的值,然后允许联合决定它应该返回哪种类型。

let rows_union =
    new graphql.GraphQLUnionType({
        name: 
            "rows_union",
        types: 
        [
            many_item,
            many_list,
            many_display_list, 
        ],
        resolveType(value) 
        {
            switch(value)
            {
                case "item":
                    return many_item
                case "list":
                    return many_list
                case "display_list":
                    return many_display_list
            }
        }
    })

// Define the Query type
var query =
    new graphql.GraphQLObjectType({
        name: "Query",
        fields:
        {
            retrieveRowsWithoutLocations:
            {
                type:
                    rows_union,
                args:
                {
                    _id:
                    {
                        type:
                            nonNullId()
                    },
                    page:
                    {
                        type:
                            nonNullInt()
                    },
                    page_length:
                    {
                        type:
                            nonNullInt()
                    },
                    type:
                    {
                        type:
                            nonNullString()
                    },
                },
                async resolve ()
                {

                }
            },
        }
    })


let many_type =
    new graphql.GraphQLObjectType({
        name: "many_"+name,
        fields: {
            results: {
                type: graphql.GraphQLList(type)
            },
            total: {
                type: graphql.GraphQLNonNull(graphql.GraphQLInt)
            },
        }
    })

type是另一个 ObjectType

标签: javascriptnode.jsgraphql-js

解决方案


您不能直接访问resolveType(或isTypeOf)内部的任何解析器参数。当一个字段被解析时,解析器返回一些值,或者一个将解析为该值的 Promise。对于返回输出类型、接口或联合的字段,此值应该是 JavaScript 对象。然后将这个值传递给resolveType,用于确定运行时响应中实际返回的类型。

给定一个类似的模式

union Animal = Bird | Fish

type Bird {
  numberOfWings: Int
}

type Fish {
  numberOfFins: Int
}

type Query {
  animal: Animal
}

您可以想象该animal字段的解析器返回 JavaScript 对象,例如{ numberOfWings: 2 } { numberOfFins: 4 }. 在这里,我们可以利用一个简单的启发式方法来确定类型:

resolveType: (value) => {
  if (value.numberOfWings !== undefined) {
    return 'Bird'
  } else if (value.numberOfFins !== undefined) {
    return 'Fish'
  }
  throw new TypeError(`Unable to resolve type for Animal with value: ${value}`)
}

如果我们不返回简单的对象,而是返回特定类的实例,我们可以做得更好:

resolveType: (value) => {
  if (value instanceof BirdModel) {
    return 'Bird'
  } else if (value instanceof FishModel) {
    return 'Fish'
  }
  throw new TypeError(`Unable to resolve type for Animal with value: ${value}`)
}

无论我们的条件逻辑是什么样的,只要记住我们总是只是测试解析器返回的值,不管它是什么。

如果您不使用类并且两个或多个类型共享相同的结构,事情会变得有点棘手。或者,就像您的情况一样,当区分属性 ( results) 是一个数组时,因为检查其中一个元素是不行的。想象一下我们的工会看起来像这样:

union Animal = Cat | Dog

type Cat {
  numberOfPaws: Int
}

type Dog {
  numberOfPaws: Int
}

不幸的是,在这里,我们不得不依靠我们的解析器来提供一些额外的信息。例如,我们可以返回一些任意字段来识别类型:

// Resolver for animal field
resolve: () => {
  return {
    numberOfPaws: 4,
    kind: 'Dog',
  }
}

// Union
resolveType: (value) => {
  return value.kind
}

resolveType但我们实际上可以通过依赖and的默认实现做得更好isTypeOf

resolve: () => {
  return {
    numberOfPaws: 4,
    __typename: 'Dog',
  }
}

通过显式返回__typename这样的内容,我们实际上可以完全省略定义resolveType。但是,请记住,这再次创建了对解析器的依赖。在可能的情况下,您应该更喜欢使用resolveTypewithinstanceof检查来与resolveType您的解析器逻辑分离。


推荐阅读