github.com/operandinc/gqlgen@v0.16.1/plugin/federation/testdata/allthethings/generated/federation.go (about)

     1  // Code generated by github.com/operandinc/gqlgen, DO NOT EDIT.
     2  
     3  package generated
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"strings"
    10  	"sync"
    11  
    12  	"github.com/operandinc/gqlgen/plugin/federation/fedruntime"
    13  )
    14  
    15  var (
    16  	ErrUnknownType  = errors.New("unknown type")
    17  	ErrTypeNotFound = errors.New("type not found")
    18  )
    19  
    20  func (ec *executionContext) __resolve__service(ctx context.Context) (fedruntime.Service, error) {
    21  	if ec.DisableIntrospection {
    22  		return fedruntime.Service{}, errors.New("federated introspection disabled")
    23  	}
    24  
    25  	var sdl []string
    26  
    27  	for _, src := range sources {
    28  		if src.BuiltIn {
    29  			continue
    30  		}
    31  		sdl = append(sdl, src.Input)
    32  	}
    33  
    34  	return fedruntime.Service{
    35  		SDL: strings.Join(sdl, "\n"),
    36  	}, nil
    37  }
    38  
    39  func (ec *executionContext) __resolve_entities(ctx context.Context, representations []map[string]interface{}) []fedruntime.Entity {
    40  	list := make([]fedruntime.Entity, len(representations))
    41  
    42  	repsMap := map[string]struct {
    43  		i []int
    44  		r []map[string]interface{}
    45  	}{}
    46  
    47  	// We group entities by typename so that we can parallelize their resolution.
    48  	// This is particularly helpful when there are entity groups in multi mode.
    49  	buildRepresentationGroups := func(reps []map[string]interface{}) {
    50  		for i, rep := range reps {
    51  			typeName, ok := rep["__typename"].(string)
    52  			if !ok {
    53  				// If there is no __typename, we just skip the representation;
    54  				// we just won't be resolving these unknown types.
    55  				ec.Error(ctx, errors.New("__typename must be an existing string"))
    56  				continue
    57  			}
    58  
    59  			_r := repsMap[typeName]
    60  			_r.i = append(_r.i, i)
    61  			_r.r = append(_r.r, rep)
    62  			repsMap[typeName] = _r
    63  		}
    64  	}
    65  
    66  	isMulti := func(typeName string) bool {
    67  		switch typeName {
    68  		default:
    69  			return false
    70  		}
    71  	}
    72  
    73  	resolveEntity := func(ctx context.Context, typeName string, rep map[string]interface{}, idx []int, i int) (err error) {
    74  		// we need to do our own panic handling, because we may be called in a
    75  		// goroutine, where the usual panic handling can't catch us
    76  		defer func() {
    77  			if r := recover(); r != nil {
    78  				err = ec.Recover(ctx, r)
    79  			}
    80  		}()
    81  
    82  		switch typeName {
    83  		case "ExternalExtension":
    84  			resolverName, err := entityResolverNameForExternalExtension(ctx, rep)
    85  			if err != nil {
    86  				return fmt.Errorf(`finding resolver for Entity "ExternalExtension": %w`, err)
    87  			}
    88  			switch resolverName {
    89  
    90  			case "findExternalExtensionByUpc":
    91  				id0, err := ec.unmarshalNString2string(ctx, rep["upc"])
    92  				if err != nil {
    93  					return fmt.Errorf(`unmarshalling param 0 for findExternalExtensionByUpc(): %w`, err)
    94  				}
    95  				entity, err := ec.resolvers.Entity().FindExternalExtensionByUpc(ctx, id0)
    96  				if err != nil {
    97  					return fmt.Errorf(`resolving Entity "ExternalExtension": %w`, err)
    98  				}
    99  
   100  				list[idx[i]] = entity
   101  				return nil
   102  			}
   103  		case "Hello":
   104  			resolverName, err := entityResolverNameForHello(ctx, rep)
   105  			if err != nil {
   106  				return fmt.Errorf(`finding resolver for Entity "Hello": %w`, err)
   107  			}
   108  			switch resolverName {
   109  
   110  			case "findHelloByName":
   111  				id0, err := ec.unmarshalNString2string(ctx, rep["name"])
   112  				if err != nil {
   113  					return fmt.Errorf(`unmarshalling param 0 for findHelloByName(): %w`, err)
   114  				}
   115  				entity, err := ec.resolvers.Entity().FindHelloByName(ctx, id0)
   116  				if err != nil {
   117  					return fmt.Errorf(`resolving Entity "Hello": %w`, err)
   118  				}
   119  
   120  				list[idx[i]] = entity
   121  				return nil
   122  			}
   123  		case "NestedKey":
   124  			resolverName, err := entityResolverNameForNestedKey(ctx, rep)
   125  			if err != nil {
   126  				return fmt.Errorf(`finding resolver for Entity "NestedKey": %w`, err)
   127  			}
   128  			switch resolverName {
   129  
   130  			case "findNestedKeyByIDAndHelloName":
   131  				id0, err := ec.unmarshalNString2string(ctx, rep["id"])
   132  				if err != nil {
   133  					return fmt.Errorf(`unmarshalling param 0 for findNestedKeyByIDAndHelloName(): %w`, err)
   134  				}
   135  				id1, err := ec.unmarshalNString2string(ctx, rep["hello"].(map[string]interface{})["name"])
   136  				if err != nil {
   137  					return fmt.Errorf(`unmarshalling param 1 for findNestedKeyByIDAndHelloName(): %w`, err)
   138  				}
   139  				entity, err := ec.resolvers.Entity().FindNestedKeyByIDAndHelloName(ctx, id0, id1)
   140  				if err != nil {
   141  					return fmt.Errorf(`resolving Entity "NestedKey": %w`, err)
   142  				}
   143  
   144  				list[idx[i]] = entity
   145  				return nil
   146  			}
   147  		case "VeryNestedKey":
   148  			resolverName, err := entityResolverNameForVeryNestedKey(ctx, rep)
   149  			if err != nil {
   150  				return fmt.Errorf(`finding resolver for Entity "VeryNestedKey": %w`, err)
   151  			}
   152  			switch resolverName {
   153  
   154  			case "findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo":
   155  				id0, err := ec.unmarshalNString2string(ctx, rep["id"])
   156  				if err != nil {
   157  					return fmt.Errorf(`unmarshalling param 0 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err)
   158  				}
   159  				id1, err := ec.unmarshalNString2string(ctx, rep["hello"].(map[string]interface{})["name"])
   160  				if err != nil {
   161  					return fmt.Errorf(`unmarshalling param 1 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err)
   162  				}
   163  				id2, err := ec.unmarshalNString2string(ctx, rep["world"].(map[string]interface{})["foo"])
   164  				if err != nil {
   165  					return fmt.Errorf(`unmarshalling param 2 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err)
   166  				}
   167  				id3, err := ec.unmarshalNInt2int(ctx, rep["world"].(map[string]interface{})["bar"])
   168  				if err != nil {
   169  					return fmt.Errorf(`unmarshalling param 3 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err)
   170  				}
   171  				id4, err := ec.unmarshalNString2string(ctx, rep["more"].(map[string]interface{})["world"].(map[string]interface{})["foo"])
   172  				if err != nil {
   173  					return fmt.Errorf(`unmarshalling param 4 for findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(): %w`, err)
   174  				}
   175  				entity, err := ec.resolvers.Entity().FindVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo(ctx, id0, id1, id2, id3, id4)
   176  				if err != nil {
   177  					return fmt.Errorf(`resolving Entity "VeryNestedKey": %w`, err)
   178  				}
   179  
   180  				entity.ID, err = ec.unmarshalNString2string(ctx, rep["id"])
   181  				if err != nil {
   182  					return err
   183  				}
   184  				entity.Hello.Secondary, err = ec.unmarshalNString2string(ctx, rep["hello"].(map[string]interface{})["secondary"])
   185  				if err != nil {
   186  					return err
   187  				}
   188  				list[idx[i]] = entity
   189  				return nil
   190  			}
   191  		case "World":
   192  			resolverName, err := entityResolverNameForWorld(ctx, rep)
   193  			if err != nil {
   194  				return fmt.Errorf(`finding resolver for Entity "World": %w`, err)
   195  			}
   196  			switch resolverName {
   197  
   198  			case "findWorldByFoo":
   199  				id0, err := ec.unmarshalNString2string(ctx, rep["foo"])
   200  				if err != nil {
   201  					return fmt.Errorf(`unmarshalling param 0 for findWorldByFoo(): %w`, err)
   202  				}
   203  				entity, err := ec.resolvers.Entity().FindWorldByFoo(ctx, id0)
   204  				if err != nil {
   205  					return fmt.Errorf(`resolving Entity "World": %w`, err)
   206  				}
   207  
   208  				list[idx[i]] = entity
   209  				return nil
   210  			case "findWorldByBar":
   211  				id0, err := ec.unmarshalNInt2int(ctx, rep["bar"])
   212  				if err != nil {
   213  					return fmt.Errorf(`unmarshalling param 0 for findWorldByBar(): %w`, err)
   214  				}
   215  				entity, err := ec.resolvers.Entity().FindWorldByBar(ctx, id0)
   216  				if err != nil {
   217  					return fmt.Errorf(`resolving Entity "World": %w`, err)
   218  				}
   219  
   220  				list[idx[i]] = entity
   221  				return nil
   222  			}
   223  
   224  		}
   225  		return fmt.Errorf("%w: %s", ErrUnknownType, typeName)
   226  	}
   227  
   228  	resolveManyEntities := func(ctx context.Context, typeName string, reps []map[string]interface{}, idx []int) (err error) {
   229  		// we need to do our own panic handling, because we may be called in a
   230  		// goroutine, where the usual panic handling can't catch us
   231  		defer func() {
   232  			if r := recover(); r != nil {
   233  				err = ec.Recover(ctx, r)
   234  			}
   235  		}()
   236  
   237  		switch typeName {
   238  
   239  		default:
   240  			return errors.New("unknown type: " + typeName)
   241  		}
   242  	}
   243  
   244  	resolveEntityGroup := func(typeName string, reps []map[string]interface{}, idx []int) {
   245  		if isMulti(typeName) {
   246  			err := resolveManyEntities(ctx, typeName, reps, idx)
   247  			if err != nil {
   248  				ec.Error(ctx, err)
   249  			}
   250  		} else {
   251  			// if there are multiple entities to resolve, parallelize (similar to
   252  			// graphql.FieldSet.Dispatch)
   253  			var e sync.WaitGroup
   254  			e.Add(len(reps))
   255  			for i, rep := range reps {
   256  				i, rep := i, rep
   257  				go func(i int, rep map[string]interface{}) {
   258  					err := resolveEntity(ctx, typeName, rep, idx, i)
   259  					if err != nil {
   260  						ec.Error(ctx, err)
   261  					}
   262  					e.Done()
   263  				}(i, rep)
   264  			}
   265  			e.Wait()
   266  		}
   267  	}
   268  	buildRepresentationGroups(representations)
   269  
   270  	switch len(repsMap) {
   271  	case 0:
   272  		return list
   273  	case 1:
   274  		for typeName, reps := range repsMap {
   275  			resolveEntityGroup(typeName, reps.r, reps.i)
   276  		}
   277  		return list
   278  	default:
   279  		var g sync.WaitGroup
   280  		g.Add(len(repsMap))
   281  		for typeName, reps := range repsMap {
   282  			go func(typeName string, reps []map[string]interface{}, idx []int) {
   283  				resolveEntityGroup(typeName, reps, idx)
   284  				g.Done()
   285  			}(typeName, reps.r, reps.i)
   286  		}
   287  		g.Wait()
   288  		return list
   289  	}
   290  }
   291  
   292  func entityResolverNameForExternalExtension(ctx context.Context, rep map[string]interface{}) (string, error) {
   293  	for {
   294  		var (
   295  			m   map[string]interface{}
   296  			val interface{}
   297  			ok  bool
   298  		)
   299  		_ = val
   300  		m = rep
   301  		if _, ok = m["upc"]; !ok {
   302  			break
   303  		}
   304  		return "findExternalExtensionByUpc", nil
   305  	}
   306  	return "", fmt.Errorf("%w for ExternalExtension", ErrTypeNotFound)
   307  }
   308  
   309  func entityResolverNameForHello(ctx context.Context, rep map[string]interface{}) (string, error) {
   310  	for {
   311  		var (
   312  			m   map[string]interface{}
   313  			val interface{}
   314  			ok  bool
   315  		)
   316  		_ = val
   317  		m = rep
   318  		if _, ok = m["name"]; !ok {
   319  			break
   320  		}
   321  		return "findHelloByName", nil
   322  	}
   323  	return "", fmt.Errorf("%w for Hello", ErrTypeNotFound)
   324  }
   325  
   326  func entityResolverNameForNestedKey(ctx context.Context, rep map[string]interface{}) (string, error) {
   327  	for {
   328  		var (
   329  			m   map[string]interface{}
   330  			val interface{}
   331  			ok  bool
   332  		)
   333  		_ = val
   334  		m = rep
   335  		if _, ok = m["id"]; !ok {
   336  			break
   337  		}
   338  		m = rep
   339  		if val, ok = m["hello"]; !ok {
   340  			break
   341  		}
   342  		if m, ok = val.(map[string]interface{}); !ok {
   343  			break
   344  		}
   345  		if _, ok = m["name"]; !ok {
   346  			break
   347  		}
   348  		return "findNestedKeyByIDAndHelloName", nil
   349  	}
   350  	return "", fmt.Errorf("%w for NestedKey", ErrTypeNotFound)
   351  }
   352  
   353  func entityResolverNameForVeryNestedKey(ctx context.Context, rep map[string]interface{}) (string, error) {
   354  	for {
   355  		var (
   356  			m   map[string]interface{}
   357  			val interface{}
   358  			ok  bool
   359  		)
   360  		_ = val
   361  		m = rep
   362  		if _, ok = m["id"]; !ok {
   363  			break
   364  		}
   365  		m = rep
   366  		if val, ok = m["hello"]; !ok {
   367  			break
   368  		}
   369  		if m, ok = val.(map[string]interface{}); !ok {
   370  			break
   371  		}
   372  		if _, ok = m["name"]; !ok {
   373  			break
   374  		}
   375  		m = rep
   376  		if val, ok = m["world"]; !ok {
   377  			break
   378  		}
   379  		if m, ok = val.(map[string]interface{}); !ok {
   380  			break
   381  		}
   382  		if _, ok = m["foo"]; !ok {
   383  			break
   384  		}
   385  		m = rep
   386  		if val, ok = m["world"]; !ok {
   387  			break
   388  		}
   389  		if m, ok = val.(map[string]interface{}); !ok {
   390  			break
   391  		}
   392  		if _, ok = m["bar"]; !ok {
   393  			break
   394  		}
   395  		m = rep
   396  		if val, ok = m["more"]; !ok {
   397  			break
   398  		}
   399  		if m, ok = val.(map[string]interface{}); !ok {
   400  			break
   401  		}
   402  		if val, ok = m["world"]; !ok {
   403  			break
   404  		}
   405  		if m, ok = val.(map[string]interface{}); !ok {
   406  			break
   407  		}
   408  		if _, ok = m["foo"]; !ok {
   409  			break
   410  		}
   411  		return "findVeryNestedKeyByIDAndHelloNameAndWorldFooAndWorldBarAndMoreWorldFoo", nil
   412  	}
   413  	return "", fmt.Errorf("%w for VeryNestedKey", ErrTypeNotFound)
   414  }
   415  
   416  func entityResolverNameForWorld(ctx context.Context, rep map[string]interface{}) (string, error) {
   417  	for {
   418  		var (
   419  			m   map[string]interface{}
   420  			val interface{}
   421  			ok  bool
   422  		)
   423  		_ = val
   424  		m = rep
   425  		if _, ok = m["foo"]; !ok {
   426  			break
   427  		}
   428  		return "findWorldByFoo", nil
   429  	}
   430  	for {
   431  		var (
   432  			m   map[string]interface{}
   433  			val interface{}
   434  			ok  bool
   435  		)
   436  		_ = val
   437  		m = rep
   438  		if _, ok = m["bar"]; !ok {
   439  			break
   440  		}
   441  		return "findWorldByBar", nil
   442  	}
   443  	return "", fmt.Errorf("%w for World", ErrTypeNotFound)
   444  }