Skip to content

Commit

Permalink
chore: add basic schema handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasZeissner committed Sep 13, 2024
1 parent 20c222b commit f24bd66
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/api/subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,21 @@ func (p PlatformClient) GetSubgraph(ctx context.Context, name, namespace string)

return subgraph, nil
}

func (p PlatformClient) PublishSubgraph(ctx context.Context, name, namespace, schema string) (*platformv1.PublishFederatedSubgraphResponse, error) {
request := connect.NewRequest(&platformv1.PublishFederatedSubgraphRequest{
Name: name,
Namespace: namespace,
Schema: schema,
})
response, err := p.Client.PublishFederatedSubgraph(ctx, request)
if err != nil {
return nil, err
}

if response.Msg == nil {
return nil, fmt.Errorf("failed to publish subgraph: %s, the server response is nil", name)
}

return response.Msg, nil
}
5 changes: 5 additions & 0 deletions internal/service/subgraph/data_source_cosmo_subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SubgraphDataSourceModel struct {
IsEventDrivenGraph types.Bool `tfsdk:"is_event_driven_graph"`
IsFeatureSubgraph types.Bool `tfsdk:"is_feature_subgraph"`
Headers types.List `tfsdk:"headers"`
Schema types.String `tfsdk:"schema"`
}

func (d *SubgraphDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down Expand Up @@ -101,6 +102,10 @@ func (d *SubgraphDataSource) Schema(ctx context.Context, req datasource.SchemaRe
MarkdownDescription: "Labels for the subgraph.",
ElementType: types.StringType,
},
"schema": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The schema for the subgraph.",
},
},
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/service/subgraph/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
ErrRetrievingSubgraph = "Error Retrieving Subgraph"
ErrUpdatingSubgraph = "Error Updating Subgraph"
ErrDeletingSubgraph = "Error Deleting Subgraph"
ErrPublishingSubgraph = "Error Publishing Subgraph"
ErrUnexpectedDataSourceType = "Unexpected Data Source Configure Type"
ErrInvalidNamespace = "Invalid Namespace"
)
24 changes: 24 additions & 0 deletions internal/service/subgraph/resource_cosmo_subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type SubgraphResourceModel struct {
UnsetLabels types.Bool `tfsdk:"unset_labels"`
Headers types.List `tfsdk:"headers"`
Labels types.Map `tfsdk:"labels"`
Schema types.String `tfsdk:"schema"`
}

func NewSubgraphResource() resource.Resource {
Expand Down Expand Up @@ -123,6 +124,10 @@ func (r *SubgraphResource) Schema(ctx context.Context, req resource.SchemaReques
MarkdownDescription: "Labels for the subgraph.",
ElementType: types.StringType,
},
"schema": schema.StringAttribute{
Optional: true,
MarkdownDescription: "The schema for the subgraph.",
},
// TODO: re-enable this once Graph Feature Flags are implementd
// "base_subgraph_name": schema.StringAttribute{
// Optional: true,
Expand Down Expand Up @@ -164,6 +169,14 @@ func (r *SubgraphResource) Create(ctx context.Context, req resource.CreateReques
return
}

if data.Schema.ValueString() != "" {
_, err := r.client.PublishSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.Schema.ValueString())
if err != nil {
utils.AddDiagnosticError(resp, ErrPublishingSubgraph, fmt.Sprintf("Could not publish subgraph '%s': %s", data.Name.ValueString(), err))
return
}
}

data.Id = types.StringValue(subgraph.GetId())
data.Name = types.StringValue(subgraph.GetName())
data.Namespace = types.StringValue(subgraph.GetNamespace())
Expand Down Expand Up @@ -216,6 +229,17 @@ func (r *SubgraphResource) Update(ctx context.Context, req resource.UpdateReques
}
}

if data.Schema.ValueString() != "" {
apiResponse, err := r.client.PublishSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.Schema.ValueString())
if err != nil {
utils.AddDiagnosticError(resp, ErrPublishingSubgraph, fmt.Sprintf("Could not publish subgraph '%s': %s", data.Name.ValueString(), err))
return
}
if apiResponse.HasChanged != nil && *apiResponse.HasChanged {
resp.Diagnostics.AddWarning("Subgraph schema has changed", fmt.Sprintf("The schema for subgraph '%s' has changed and was published.", data.Name.ValueString()))
}
}

var unsetLabels *bool
if data.UnsetLabels.ValueBool() {
unsetLabels = &[]bool{true}[0]
Expand Down

0 comments on commit f24bd66

Please sign in to comment.