From 2031ef057e370285a938f6c512e9d060843add9a Mon Sep 17 00:00:00 2001 From: Nicolas Maquet Date: Wed, 7 Jul 2021 14:48:04 +1200 Subject: [PATCH] Meta plugin: make BrambleService a boundary type --- docs/plugins.md | 7 ++++--- plugins/meta.go | 33 +++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/docs/plugins.md b/docs/plugins.md index 9f09884f..5f059561 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -116,7 +116,8 @@ With the Meta plugin, you can programmatically query Bramble's federation inform The Meta plugin federates the following GraphQL API in your graph: ```graphql -type BrambleService { +type BrambleService @boundary { + id: ID! name: String! version: String! schema: String! @@ -143,7 +144,7 @@ type BrambleEnumValue { description: String } -type BrambleType { +type BrambleType @boundary { kind: String! name: String! directives: [String!]! @@ -167,7 +168,7 @@ extend type Query { } ``` -Note that the Meta plugin offers an extensible schema since `BrambleMetaQuery` is a namespace and `BrambleField` and `BrambleType` are a boundary types. +Note that the Meta plugin offers an extensible schema since `BrambleMetaQuery` is a namespace and `BrambleField`, `BrambleType`, and `BrambleService` are all boundary types. ## Playground diff --git a/plugins/meta.go b/plugins/meta.go index c1164826..2e516094 100644 --- a/plugins/meta.go +++ b/plugins/meta.go @@ -26,7 +26,8 @@ type Service { version: String! schema: String! } -type BrambleService { +type BrambleService @boundary { + id: ID! name: String! version: String! schema: String! @@ -69,8 +70,9 @@ type BrambleMetaQuery @namespace { type Query { service: Service! meta: BrambleMetaQuery! - field(id: ID!): BrambleField @boundary - type(id: ID!): BrambleType @boundary + getField(id: ID!): BrambleField @boundary + getType(id: ID!): BrambleType @boundary + getService(id: ID!): BrambleService @boundary } ` @@ -204,7 +206,22 @@ func strToPtr(s string) *string { return &s } -func (p *metaPluginResolver) Type(ctx context.Context, args struct{ ID graphql.ID }) (*brambleType, error) { +func (r *metaPluginResolver) GetService(ctx context.Context, args struct{ ID graphql.ID }) *brambleService { + for _, service := range r.executableSchema.Services { + if service.Name == string(args.ID) { + return &brambleService{ + Name: service.Name, + Version: service.Version, + Schema: service.SchemaSource, + Status: service.Status, + ServiceURL: service.ServiceURL, + } + } + } + return nil +} + +func (p *metaPluginResolver) GetType(ctx context.Context, args struct{ ID graphql.ID }) (*brambleType, error) { typeName := string(args.ID) var typeDef *ast.Definition for _, def := range p.executableSchema.MergedSchema.Types { @@ -270,6 +287,10 @@ func (r *metaPluginResolver) brambleType(name string, def *ast.Definition) bramb } func (p *metaPluginResolver) Field(ctx context.Context, args struct{ ID graphql.ID }) (*brambleField, error) { + return p.GetField(ctx, args) +} + +func (p *metaPluginResolver) GetField(ctx context.Context, args struct{ ID graphql.ID }) (*brambleField, error) { splitFieldName := strings.Split(string(args.ID), ".") if len(splitFieldName) != 2 { return nil, errors.New("invalid ID passed to query") @@ -319,6 +340,10 @@ type brambleService struct { ServiceURL string } +func (s brambleService) Id() graphql.ID { + return graphql.ID(s.Name) +} + type externalBrambleServices []brambleService func (s externalBrambleServices) Len() int {