diff --git a/gradle.properties b/gradle.properties index 14706858..bef745ce 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/graphql-cli/src/test/java/io/ballerina/graphql/cmd/SdlSchemaGenerationTest.java b/graphql-cli/src/test/java/io/ballerina/graphql/cmd/SdlSchemaGenerationTest.java index e53abd9b..bf1ae82f 100644 --- a/graphql-cli/src/test/java/io/ballerina/graphql/cmd/SdlSchemaGenerationTest.java +++ b/graphql-cli/src/test/java/io/ballerina/graphql/cmd/SdlSchemaGenerationTest.java @@ -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()}; diff --git a/graphql-cli/src/test/resources/expectedSchemas/schema_product.graphql b/graphql-cli/src/test/resources/expectedSchemas/schema_product.graphql new file mode 100644 index 00000000..03d1e123 --- /dev/null +++ b/graphql-cli/src/test/resources/expectedSchemas/schema_product.graphql @@ -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! +} diff --git a/graphql-cli/src/test/resources/graphqlServices/valid/service_11.bal b/graphql-cli/src/test/resources/graphqlServices/valid/service_11.bal new file mode 100644 index 00000000..8e5525ff --- /dev/null +++ b/graphql-cli/src/test/resources/graphqlServices/valid/service_11.bal @@ -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] + } +];