graph-gophers / graphql-go

GraphQL server with a focus on ease of use
BSD 2-Clause "Simplified" License
4.64k stars 492 forks source link

"panic missing method for fields" when declaring non null query array #135

Open pierrebiver opened 6 years ago

pierrebiver commented 6 years ago

I have a query as such:

type Query {
        descriptors(): [Descriptor!]!
    }

    type Descriptor {
        id: ID!
        name: String!
        Description: String
        Example: String
        tags: [String!]!
    }

I declared properly the resolver but I got the following error:

"panic: graphql.descriptorResolver does not resolve "Descriptor": missing method for field "id" (hint: the method exists on the pointer type)
    returned by (*graphql.Resolver).Descriptors"

But If I remove the "!" to show that array and array's content shall not be empty, the error disappears. Any idea why it behaves this way?

vergenzt commented 6 years ago

What do your resolvers look like?

pierrebiver commented 6 years ago

There is the full code:

type descriptorResolver struct {
    Descriptor model.Descriptor
}

func (d *descriptorResolver) ID() graphql.ID {
    return d.Descriptor.ID
}

func (d *descriptorResolver) Name() string {
    return d.Descriptor.Name
}

func (d *descriptorResolver) Description() *string {
    return &d.Descriptor.Description
}

func (d *descriptorResolver) Example() *string {
    return &d.Descriptor.Example
}

func (d *descriptorResolver) Tags() []string {
    return d.Descriptor.Tags
}

func (r *Resolver) Descriptors() *[]*descriptorResolver {
    descriptors, _ := mongo.Descriptors()

    descriptorResolvers := make([]*descriptorResolver, 0)
    for _, d := range descriptors {
        descriptorResolvers = append(descriptorResolvers, &descriptorResolver{d})
    }

    return &descriptorResolvers
}
vergenzt commented 6 years ago

Hmm I wonder if the struct itself has to be exported in addition to the fields? I.e. would it need to be DescriptorResolver instead of descriptorResolver?

pierrebiver commented 6 years ago

I tried and there is no difference. In addition, when I don't put the result of the query as mandatory, it works perfectly fine.

AmanPatelcubastion commented 6 years ago

I am trying to create a many to many relation between "Products" and "RelatedProductGroup" These are the schema definitions of both the structures.

type Product { id: ID! name: String! group: String! relprogroups: [RelatedProductGroup!]! }

input ProductInput {
  id: ID!
  name: String!
  group: String!
  relprogroups: [RelatedProductGroupInput!]!

}

type RelatedProductGroup {
  id: ID!
  grouptype: String!
  product: [Product!]!

}

input RelatedProductGroupInput {
  id: ID!
  grouptype: String!
  product: [ProductInput!]!

}

//function for create Product

func (r Resolver) CreateProduct(args struct { Product productInput }) productResolver {

var createdRelatedproductgroup []*x_related_product_group
for _,v:=range args.Product.Relprogroups{
    createdRelatedproductgroup=append(createdRelatedproductgroup,&x_related_product_group{
        id:   v.ID,
        grouptype:   v.Grouptype,
                    //missing relatedproductgroup field            //  (ISSUE)
    },)
}

product := &x_product{
    id:   args.Product.ID,
    name: args.Product.Name,
    group: args.Product.Group,
    relprogroups: createdRelatedproductgroup,
}

productData[product.id] = product
return &productResolver{productData[product.id]}

}

Now when i am trying to create Products (considering 1 product has many (1-many) relatedProductGroups) with query:

mutation{ createProduct(product:{id:"1100",name:"Heater-2",group:"Electronics",relprogroups:{id:"2"}}){ id grouptype product{ id name group } } }

IT THROWS AN ERROR at RUNTIME: Argument \"product\" has invalid value {id: \"1100\", name: \"Heater-2\", group: \"Electronics\", relprogroups: {id: \"2\"}}.\nIn field \"relprogroups\": In field \"grouptype\": Expected \"String!\", found null. //since the relation is many to many , it is expecting further product in relprogroups in the query and so on.....

So basically how can i omit a field while creating a record in my query ??

RealYukiSan commented 1 year ago

Hi @pierrebiver are you have found the reasons behind this error?