Graphql: return a array

Created on 22 Mar 2020  ·  7Comments  ·  Source: graphql-go/graphql

My return array:

"all": &graphql.Field{
            Type: graphql.NewNonNull(graphql.String),
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                if services, ok := p.Source.([]models.Services); ok == true {
                    return services, nil
                }
                return nil, nil

            },
        },

frontend gives me:

image

in getAllServices => all, is a string, with not Key, only values.

question

All 7 comments

Just checking if it's a typo or not - are you sure you want graphql.NewNonNull(graphql.String) as your type instead of graphql.NewList(..)?

Also you might want to use an Object, instead of graphql.String.
I'm not sure about your use case, so if you provide a little more context, it'll be easier to help

Update: asking for a little more context

My typen

package typen

import (
    "log"
    "sedhoQL_api/services/models"

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

var ServicesType = graphql.NewObject(graphql.ObjectConfig{
    Name: "Services",
    Fields: graphql.Fields{
        "key": &graphql.Field{
            Type: graphql.NewNonNull(graphql.String),
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                if services, ok := p.Source.(*models.Services); ok == true {
                    return services.Key, nil
                }
                return nil, nil
            },
        },
        "title": &graphql.Field{
            Type: graphql.NewNonNull(graphql.String),
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                if services, ok := p.Source.(*models.Services); ok == true {
                    return services.Title, nil
                }
                return nil, nil
            },
        },
        "description": &graphql.Field{
            Type: graphql.NewNonNull(graphql.String),
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                if services, ok := p.Source.(*models.Services); ok == true {
                    return services.Description, nil
                }
                return nil, nil

            },
        },
        "all": &graphql.Field{
            Type: graphql.NewList(ServicesType),
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
                if services, ok := p.Source.([]models.Services); ok == true {
                    return services, nil
                }
                return nil, nil

            },
        },
    },
})

My query

package sedho

import (
    //  "log"
    "sedhoQL_api/controllers"
    "sedhoQL_api/typen"

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

func GetAllServices(field *graphql.Object) {
    field.AddFieldConfig("getAllServices", &graphql.Field{
        Type: typen.ServicesType,
        Args: graphql.FieldConfigArgument{},
        Resolve: func(p graphql.ResolveParams) (interface{}, error) {

            d, err := controllers.GetAllServices()

            if err != nil {
                return nil, err
            }

            return d, err
        },
    })

}

And with this updated “all” field, with type graphql.NewList(ServicesType) does it return the same string instead of the array of objects?

i have "all" deleted

and the main function

func GetAllServices(field *graphql.Object) {
    field.AddFieldConfig("getAllServices", &graphql.Field{
        Type: graphql.NewList(typen.ServicesType),
        Args: graphql.FieldConfigArgument{},
        Resolve: func(p graphql.ResolveParams) (interface{}, error) {

            d, err := controllers.GetAllServices()

            if err != nil {
                return nil, err
            }

            return d, err
        },
    })

}

image

i see 2 rows on response

my request from frontend

image

Because you try to assert the p.Source to *models.Services instead of models.Services.

The array returned by the controller is []models.Services, so the source in the resolver will be models.Service, not a pointer.

yes... that's it

image

Great! 🎉 If you don’t have any more issue or question, feel free to close the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

schaze picture schaze  ·  4Comments

zbintliff picture zbintliff  ·  3Comments

titanous picture titanous  ·  3Comments

jiharal picture jiharal  ·  5Comments

conord33 picture conord33  ·  6Comments