Skip to content

Commit

Permalink
Merge pull request #49 from movio/improve-meta-plugin
Browse files Browse the repository at this point in the history
Meta plugin: make BrambleService a boundary type
  • Loading branch information
Nicolas Maquet authored Jul 7, 2021
2 parents c235815 + 2031ef0 commit 0e5bf81
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
7 changes: 4 additions & 3 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -143,7 +144,7 @@ type BrambleEnumValue {
description: String
}

type BrambleType {
type BrambleType @boundary {
kind: String!
name: String!
directives: [String!]!
Expand All @@ -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

Expand Down
33 changes: 29 additions & 4 deletions plugins/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Service {
version: String!
schema: String!
}
type BrambleService {
type BrambleService @boundary {
id: ID!
name: String!
version: String!
schema: String!
Expand Down Expand Up @@ -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
}
`

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0e5bf81

Please sign in to comment.