graphql-go icon indicating copy to clipboard operation
graphql-go copied to clipboard

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

Open pierrebiver opened this issue 7 years ago • 6 comments

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?

pierrebiver avatar Dec 01 '17 08:12 pierrebiver

What do your resolvers look like?

vergenzt avatar Dec 01 '17 15:12 vergenzt

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
}

pierrebiver avatar Dec 01 '17 15:12 pierrebiver

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?

vergenzt avatar Dec 01 '17 15:12 vergenzt

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.

pierrebiver avatar Dec 04 '17 08:12 pierrebiver

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 ??

AmanPatelcubastion avatar Dec 04 '17 09:12 AmanPatelcubastion

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

RealYukiSan avatar Jun 09 '23 06:06 RealYukiSan