Skip to content

Commit

Permalink
Add SDL schema generation test for graphql subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedSabthar committed Sep 12, 2023
1 parent 7746096 commit a1a70ff
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ stdlibHttpVersion=2.10.0-20230911-204400-1c092fe
stdlibWebsocketVersion=2.10.0-20230911-221500-317e2e7

# Level 07
stdlibGraphqlVersion=1.10.0-20230912-084000-2ef781d
stdlibGraphqlVersion=1.10.0-20230912-160300-47e43f5

# Ballerinax Observer
observeVersion=1.2.0-20230911-133500-b3d8db3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ public void testSdlGenerationWithDocumentation() {
}
}

@Test(description = "Test successful GraphQL command execution with graphql subgraph service")
public void testSdlGenerationForSubgraph() {
String[] args = {"-i", "valid/service_11.bal", "-o", this.tmpDir.toString()};
try {
executeCommand(args);
String fileName = "schema_product.graphql";
Assert.assertTrue(Files.exists(this.tmpDir.resolve(fileName)));
Path expectedSchemaFile = resourceDir.resolve(Paths.get("expectedSchemas", fileName));
String expectedSchema = readContentWithFormat(expectedSchemaFile);
String generatedSchema = readContentWithFormat(this.tmpDir.resolve(fileName));
Assert.assertEquals(expectedSchema, generatedSchema);
} catch (IOException | InterruptedException e) {
Assert.fail(e.toString());
}
}

@Test(description = "Test GraphQL command execution with service includes compilation errors")
public void testExecuteWithBalFileIncludeCompilationErrors() {
String[] args = {"-i", "invalid/service_1.bal", "-o", this.tmpDir.toString()};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"])

type Query {
product(id: String!): Product
products: [Product!]!
}

type Product @key(fields: "id") {
id: String!
title: String!
description: String!
price: Int!
category: Category!
}

type Category {
id: String!
title: String!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.org). All Rights Reserved.
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/graphql;
import ballerina/graphql.subgraph;

@subgraph:Entity {
key: "id",
resolveReference: resolveProduct
}
public type Product record {
string id;
string title;
string description;
int price;
Category category;
};

public type Category record {
string id;
string title;
};

function resolveProduct(subgraph:Representation representation) returns Product|error? {
string id = check representation["id"].ensureType();
return trap products.filter(product => product.id == id).pop();
}

@subgraph:Subgraph
service /product on new graphql:Listener(4001) {
resource function get product(string id) returns Product? {
var product = products.filter(product => id == product.id);
if (product.length() > 0) {
return product[0];
}
return ();
}

resource function get products() returns Product[] {
return products;
}
}

Category[] categories = [
{
id: "1",
title: "Kitchen appliances"
}
];

Product[] products = [
{
id: "1",
title: "Knife",
description: "A knife is a tool with a cutting edge.",
price: 100,
category: categories[0]
}
];

0 comments on commit a1a70ff

Please sign in to comment.