Set Parent filed value after resolving a child type values in GraphQL - graphql

There are two sub types as follows,
type Test1 {
id: String
flag: Boolean
}
type Test2 {
id: String
flag: Boolean
}
The main defined type as follows,
type Test {
id: String
flag: Boolean
test1: Test1
test2: Test2
}
I have wrote two separate resolvers for the two sub types above and a resolver for main type.
I want to set the flag field value inside main type (Test) by using Test1 and Test2 type's flag fields as a OR condition.
I would like to appreciate if someone can help me to resolve this.

Related

How populate custom struct type from interface{} returned from reflections.GetField()?

My configuration (populated via viper) includes the following:
Puppet struct {
Tasks struct {
Task1 struct {
FullTaskName string
}
Task2 struct {
FullTaskName string
}
}
}
My code seeks to extract attributes for the appropriate task dynamically.
reflections.GetField() returns an empty interface, so in order to access the 'FullTaskName' attribute later on (say with 'pt.FullTaskName') I therefore need to either convert it, or assert, or something, to my custom type (as I poorly understand it). Here I'm trying to convert.
type pTask struct {
FullTaskName string
}
taskName := "Task1"
allTasks := conf.Puppet.Tasks
t, _ := reflections.GetField(allTasks, taskName)
pt := pTask(t)
But I can't figure out how to do it. I've confirmed via the debugger that the value of 't' is as expected prior to my attempted assignment:
t = {interface{} | struct {...}}
FullTaskName = {string} "foobar"
However I get the following error when trying to assign & convert 't' to 'pt':
"Cannot convert an expression of the type 'interface{}' to the type 'pTask'"
I've tried asserting it as well but it also fails ('ok' == false).
It looks to me like my custom type matches the values in the returned interface{}, in that they have the same attributes with the same names. There are no methods attached to my 'pTask' type. What am I not understanding?

Golang _ struct {} inside a struct. Whats the purpose of it? [duplicate]

I've seen two pieces of Go code using this pattern:
type SomeType struct{
Field1 string
Field2 bool
_ struct{} // <-- what is this?
}
Can anyone explain what this code accomplishes?
This technique enforces keyed fields when declaring a struct.
For example, the struct:
type SomeType struct {
Field1 string
Field2 bool
_ struct{}
}
can only be declared with keyed fields:
// ALLOWED:
bar := SomeType{Field1: "hello", Field2: true}
// COMPILE ERROR:
foo := SomeType{"hello", true}
One reason for doing this is to allow additional fields to be added to the struct in the future without breaking existing code.

How to hide the default type constructor in golang?

The reason I have this question is that I often make mistakes that I forgot to specify a value to a struct's field, then the compiler is fine, but the zero value causes bugs.
Here is an example. Say I have a type Foo defined like this in a package:
package types
type Foo struct {
RequiredField1 string
RequiredField2 string
}
It is exposed and has 2 fields that I'd like both of them to be specified when the Foo struct is created.
Then I can import and use it in my main function like this:
package main
import (
"github.com/foo/bar/types"
)
func main() {
f := &Foo{
RequiredField1: "fff",
}
fmt.Printf("f.RequiredField2: %v", f.RequiredField2)
}
This causes a bug because I forgot to specify RequiredField2 which is required. And I often make this kind of bugs :p
Now, in order to leverage the compile and prevent this mistake, I made a constructor function for Foo and asks for the required values.
func NewFoo(field1 string, field2 string) *Foo {
return &Foo{Field1: field1, Field2: field2}
}
So that if I forgot to pass field2, the compiler won't compile my code:
func main() {
f := NewFoo("foo")
fmt.Printf("f.Field2: %v", f.Field2)
}
The build will fail:
./prog.go:18:13: not enough arguments in call to NewFoo
have (string)
want (string, string)
Now the question is: how to stop the foreign callers (callers from other namespaces) from calling the default constructor of Foo, that is &Foo, and force them to use the NewFoo?
If I can, then I'm safe since NewFoo is the only way to create the Foo type and the compiler helps me ensure all fields are present when calling it.
You can avoid this problem by making Foo type unexported.
You can make an interface that is Exported and has all the methods that Foo type performs.
Example:-
type Foo interface{
DoSomething()
}
type foo struct{
RequiredField1 string
RequiredField2 string
}
func NewFoo(requiredField1 string,requiredField2 string)*foo{
return &foo{
RequiredField1:requiredField1,
RequiredField2:requiredField2,
}
}
func (f *foo)DoSomething(){
// your implementation
}
In this way all the caller will be able to access it but cannot create foo without NewFoo function.
How to hide the default type constructor in golang?
You cannot for an exported type.
Get used to it, it is not a problem in real life.
(For unexported types, just provide a func NewUnexportedType(...) unexportedType. )
Effective Go
it's helpful to arrange when designing your data structures that the
zero value of each type can be used without further initialization.
we often need to change the type definition, like adding more fields
That is why you should make the zero value for struct fields significant. The behavior is by design. For example, it is used to maintain the Go 1 compatibility guarantee.

What is the purpose of a field named "_" (underscore) containing an empty struct?

I've seen two pieces of Go code using this pattern:
type SomeType struct{
Field1 string
Field2 bool
_ struct{} // <-- what is this?
}
Can anyone explain what this code accomplishes?
This technique enforces keyed fields when declaring a struct.
For example, the struct:
type SomeType struct {
Field1 string
Field2 bool
_ struct{}
}
can only be declared with keyed fields:
// ALLOWED:
bar := SomeType{Field1: "hello", Field2: true}
// COMPILE ERROR:
foo := SomeType{"hello", true}
One reason for doing this is to allow additional fields to be added to the struct in the future without breaking existing code.

Golang, variable with type from string

Is it possible to create variable with type from string?
Example:
I have two types:
type FirstType struct {
...
}
type SecondType struct {
...
}
// also I have a string variable
var1 := "Second"
I want to create variable with type - String value + "Type":
var variable = []var1+"Type" // slice of "SecondType"
Expected result is like in this case:
var variable = []SecondType
Thanks!
This is not possible. Go does not provide functionality to create variables of types that are not known statically. The type of a variable is always known statically. Consider using interfaces instead.

Resources