newAdminResolver creates a GraphQL request resolver for the /admin endpoint.
( defaultGqlServer IServeGraphQL, fns *resolve.ResolverFns, withIntrospection bool, epoch map[uint64]*uint64, closer *z.Closer)
| 614 | |
| 615 | // newAdminResolver creates a GraphQL request resolver for the /admin endpoint. |
| 616 | func newAdminResolver( |
| 617 | defaultGqlServer IServeGraphQL, |
| 618 | fns *resolve.ResolverFns, |
| 619 | withIntrospection bool, |
| 620 | epoch map[uint64]*uint64, |
| 621 | closer *z.Closer) *resolve.RequestResolver { |
| 622 | |
| 623 | adminSchema, err := schema.FromString(graphqlAdminSchema, x.RootNamespace) |
| 624 | if err != nil { |
| 625 | x.Panic(err) |
| 626 | } |
| 627 | |
| 628 | rf := newAdminResolverFactory() |
| 629 | |
| 630 | server := &adminServer{ |
| 631 | rf: rf, |
| 632 | resolver: resolve.New(adminSchema, rf), |
| 633 | fns: fns, |
| 634 | withIntrospection: withIntrospection, |
| 635 | globalEpoch: epoch, |
| 636 | gqlSchemas: worker.NewGQLSchemaStore(), |
| 637 | gqlServer: defaultGqlServer, |
| 638 | } |
| 639 | adminServerVar = server // store the admin server in package variable |
| 640 | |
| 641 | prefix := x.DataKey(x.AttrInRootNamespace(worker.GqlSchemaPred), 0) |
| 642 | // Remove uid from the key, to get the correct prefix |
| 643 | prefix = prefix[:len(prefix)-8] |
| 644 | // Listen for graphql schema changes in group 1. |
| 645 | go worker.SubscribeForUpdates([][]byte{prefix}, x.IgnoreBytes, func(kvs *badgerpb.KVList) { |
| 646 | |
| 647 | kv := x.KvWithMaxVersion(kvs, [][]byte{prefix}) |
| 648 | glog.Infof("Updating GraphQL schema from subscription.") |
| 649 | |
| 650 | // Unmarshal the incoming posting list. |
| 651 | pl := &pb.PostingList{} |
| 652 | err := proto.Unmarshal(kv.GetValue(), pl) |
| 653 | if err != nil { |
| 654 | glog.Errorf("Unable to unmarshal the posting list for graphql schema update %s", err) |
| 655 | return |
| 656 | } |
| 657 | |
| 658 | // There should be only one posting. |
| 659 | if len(pl.Postings) != 1 { |
| 660 | glog.Errorf("Only one posting is expected in the graphql schema posting list but got %d", |
| 661 | len(pl.Postings)) |
| 662 | return |
| 663 | } |
| 664 | |
| 665 | pk, err := x.Parse(kv.GetKey()) |
| 666 | if err != nil { |
| 667 | glog.Errorf("Unable to find uid of updated schema %s", err) |
| 668 | return |
| 669 | } |
| 670 | ns, _ := x.ParseNamespaceAttr(pk.Attr) |
| 671 | |
| 672 | newSchema := &worker.GqlSchema{ |
| 673 | ID: query.UidToHex(pk.Uid), |
no test coverage detected