登录
首页 >  Golang >  Go问答

Golang graphql schema中无法返回数组类型的字段

来源:stackoverflow

时间:2024-03-03 23:39:25 484浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《Golang graphql schema中无法返回数组类型的字段》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

尝试从 graphql 查询获取数组时收到此错误:

"user error: expected iterable, but did not find one for field rootquery.blogpostsbetween."

这很奇怪,因为就在几周前,我的 field 配置如下,它运行得非常好:

"blogpostsbetween": &graphql.field{
    type: graphql.newlist(blogposttype),
    resolve: func(p graphql.resolveparams) (interface{}, error) {
                       // ... some more code ...
                      // what i return here was placed to demonstrate the error.
        return [5]blogpost{}, err
    },
    args: graphql.fieldconfigargument{
        "from": &graphql.argumentconfig{
            type: graphql.string,
        },
        "to": &graphql.argumentconfig{
            type: graphql.string,
        },
    },
},

我真的很困惑为什么这不再起作用,因为正如我所说,我很确定我没有改变任何东西。但我很乐意犯错。

编辑:blogposttype 的详细信息

var blogpostType = graphql.NewObject(graphql.ObjectConfig{
    Name: "Blogpost",
    Fields: graphql.Fields{
        "id": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.id, nil
            },
        },
        "title": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.title, nil
            },
        },
        "body": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.body, nil
            },
        },
        "created": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.created, nil
            },
        },
        "author": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.author, nil
            },
        },
    },
})

解决方案


根据您的问题,我假设并添加了一些类型以使解决方案按如下方式工作。本示例仅查询选定字段 id,title,body,author

package main

import (
    "encoding/json"
    "fmt"
    "log"

    "github.com/graphql-go/graphql"
)

var blogpostType = graphql.NewObject(graphql.ObjectConfig{
    Name: "Blogpost",
    Fields: graphql.Fields{
        "id": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.id, nil
            },
        },
        "title": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.title, nil
            },
        },
        "body": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.body, nil
            },
        },
        "created": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.created, nil
            },
        },
        "author": &graphql.Field{
            Type: graphql.String,
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                m := p.Source.(Blogpost)
                return m.author, nil
            },
        },
    },
})

type Blogpost struct {
    id      string
    title   string
    body    string
    created string
    author  string
}

func main() {
    // Schema
    fields := graphql.Fields{
        "blogpostsBetween": &graphql.Field{
            Type: graphql.NewList(blogpostType),
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                // ... some more code ...
                // what I return here was placed to demonstrate the error.
                return []Blogpost{
                    Blogpost{
                        id:      "new-post",
                        title:   "new post",
                        body:    "Lorem ipsum",
                        author:  "John Doe",
                        created: "12.12.2012",
                    },
                }, nil
            },
            Args: graphql.FieldConfigArgument{
                "from": &graphql.ArgumentConfig{
                    Type: graphql.String,
                },
                "to": &graphql.ArgumentConfig{
                    Type: graphql.String,
                },
            },
        },
    }
    rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
    schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
    schema, err := graphql.NewSchema(schemaConfig)
    if err != nil {
        log.Fatalf("failed to create new schema, error: %v", err)
    }

    // Query
    query := `
            {
                blogpostsBetween {
                    id,
                    title,
                    body,
                    author
                }
            }
        `
    params := graphql.Params{Schema: schema, RequestString: query}
    r := graphql.Do(params)
    if len(r.Errors) > 0 {
        log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
    }
    rJSON, _ := json.Marshal(r)
    fmt.Printf("%s \n", rJSON)
}

本篇关于《Golang graphql schema中无法返回数组类型的字段》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>