Skip to content

Commit

Permalink
🏗️ Add api for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mbaye THIAM committed Apr 15, 2024
1 parent 93a28b5 commit a03a267
Show file tree
Hide file tree
Showing 26 changed files with 3,312 additions and 6 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Run Integration Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
integration-tests:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host

- name: Copy patches for Docker Buildx
run: cp -r patches/* packages/graphql-mesh/patches

- name: Build and push on local registry
id: docker_build
uses: docker/build-push-action@v5
with:
context: ./packages/graphql-mesh
push: true
tags: localhost:5000/test/graphql-mesh:latest
platforms: linux/amd64

- name: Setup services for testing purpose
run: export IMAGE_TAG=localhost:5000/test/graphql-mesh:latest && cd ./test/integration && docker compose up -d
2 changes: 0 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
node-version: ${{ matrix.node-version }}

- name: Set up Node.js
uses: actions/setup-node@v3
Expand Down
1 change: 0 additions & 1 deletion packages/directive-spl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"build:cjs": "tsc --project tsconfig-cjs.json",
"build:esm": "tsc --project tsconfig-esm.json",
"build": "rm -rf _build && npm run build:esm && npm run build:cjs && node ./scripts/prepare-package-json",
"dev": "vite",
"pack": "npm run build && npm pack --pack-destination ../graphql-mesh/local-pkg",
"test": "vitest"
},
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-mesh/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ VOLUME /app/sources
VOLUME /app/config.yaml
VOLUME /app/transforms
VOLUME /app/plugins
VOLUME /app/resolvers

CMD [ "npm", "run", "serve" ]
Binary file modified packages/graphql-mesh/local-pkg/directive-spl-1.0.0.tgz
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/graphql-mesh/utils/ConfigFromSwaggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export default class ConfigFromSwaggers {
spec,
availableTypes,
this.getInterfacesWithChildren(),
this.catalog
this.catalog,
this.config
)
acc.typeDefs += typeDefs
acc.resolvers = mergeObjects(acc.resolvers, resolvers)
Expand Down
7 changes: 5 additions & 2 deletions packages/graphql-mesh/utils/swaggers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Spec, ConfigExtension, Resolvers } from '../../types'
import { getSourceName } from '../config'
import { trimLinks, anonymizePathAndGetParams } from '../helpers'
/**
* This function creates, for a Swagger file, the additional typeDefs for each schema having at least one x-link, and one resolver for each x-link
Expand All @@ -10,7 +11,8 @@ export const generateTypeDefsAndResolversFromSwagger = (
spec: Spec,
availableTypes: string[],
interfacesWithChildren: { [key: string]: string[] },
catalog: { [key: string]: [string, string, string] }
catalog: { [key: string]: [string, string, string] },
config: any
): ConfigExtension => {
if (!spec.components) {
return {
Expand Down Expand Up @@ -93,7 +95,7 @@ export const generateTypeDefsAndResolversFromSwagger = (

const query = targetedOperationName
const type = targetedOperationType
const source = targetedSwaggerName
const source = getSourceName(targetedSwaggerName, config)

if (
targetedOperationType !== 'TYPE_NOT_FOUND' &&
Expand Down Expand Up @@ -124,6 +126,7 @@ export const generateTypeDefsAndResolversFromSwagger = (
root = { ...root, followLink: hateoasLink.href }
}

// @TODO: Fix type checking for interger params
if (paramsToSend.length) {
paramsToSend.forEach((param, i) => {
args[param] = root[param] || root[paramsFromLink[i]] || ''
Expand Down
2 changes: 2 additions & 0 deletions test/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
21 changes: 21 additions & 0 deletions test/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# Use a base image with Node.js
FROM node:18-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the code to the working directory
COPY . .

# Expose the port on which your API will run
#EXPOSE 5000

# Define the command to start your API
CMD ["npm", "run", "start"]
82 changes: 82 additions & 0 deletions test/api/api-docs/products.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
openapi: 3.0.0
info:
title: Products API
description: API to manage products with HATEOAS.
version: "1.0"
servers:
- url: http://localhost:3000/
paths:
/products:
get:
operationId: getProducts
summary: List all products
responses:
"200":
description: A list of products with HATEOAS links
content:
application/json:
schema:
$ref: "#/components/schemas/Products"
/products/{id}:
get:
operationId: getProductById
summary: Get a product by ID
parameters:
- name: id
in: path
required: true
schema:
type: integer
description: The product ID
responses:
"200":
description: A list of products with HATEOAS links
content:
application/json:
schema:
$ref: "#/components/schemas/Product"
components:
schemas:
Product:
type: object
properties:
_links:
$ref: "#/components/schemas/ProductLinks"
id:
type: integer
name:
type: string
price:
type: number
supplierId:
type: integer
ProductLinks:
type: object
required:
- self
- supplier
properties:
self:
$ref: "#/components/schemas/Link"
supplier:
$ref: "#/components/schemas/Link"
x-links:
- rel: self
hrefPattern: "/products/{id}"
- rel: supplier
hrefPattern: "/suppliers/{id}"

Products:
type: object
properties:
items:
type: array
items:
$ref: "#/components/schemas/Product"
Link:
type: object
required:
- href
properties:
href:
type: string
60 changes: 60 additions & 0 deletions test/api/api-docs/suppliers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
openapi: 3.0.0
info:
title: Suppliers API
description: API to manage suppliers with HATEOAS.
version: "1.0"
servers:
- url: http://localhost:3000/
paths:
/suppliers:
get:
operationId: getSuppliers
summary: List all suppliers
responses:
"200":
description: A list of suppliers with HATEOAS links
content:
application/json:
schema:
$ref: "#/components/schemas/Suppliers"
/suppliers/{id}:
get:
operationId: getSupplierById
summary: Get a supplier by ID
parameters:
- name: id
in: path
required: true
schema:
type: integer
description: The supplier ID
responses:
"200":
description: A list of suppliers with HATEOAS links
content:
application/json:
schema:
$ref: "#/components/schemas/Supplier"
components:
schemas:
Supplier:
type: object
properties:
id:
type: integer
name:
type: string
Link:
type: object
required:
- href
properties:
href:
type: string
Suppliers:
type: object
properties:
items:
type: array
items:
$ref: "#/components/schemas/Supplier"
39 changes: 39 additions & 0 deletions test/api/models/products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type Product = {
id: number
name: string
price: number
supplierId: number
_links: {
supplier: {
href: string
}
self?: {
href: string
}
}
}

function generateProducts() {
const _products: Product[] = []
for (let i = 1; i <= 50; i++) {
const supplierId = Math.floor(Math.random() * 10) + 1
const product: Product = {
id: i,
name: `Product ${i}`,
price: Math.floor(Math.random() * 100),
supplierId,
_links: {
self: {
href: `/products/${i}`
},
supplier: {
href: `/suppliers/${supplierId}`
}
}
}
_products.push(product)
}
return _products
}

export const products = generateProducts()
18 changes: 18 additions & 0 deletions test/api/models/suppliers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type Supplier = {
id: number
name: string
}

const generateSuppliers = () => {
const _suppliers: Supplier[] = []
for (let i = 1; i <= 10; i++) {
const supplier: Supplier = {
id: i,
name: `Supplier ${i}`
}
_suppliers.push(supplier)
}
return _suppliers
}

export const suppliers = generateSuppliers()
Loading

0 comments on commit a03a267

Please sign in to comment.