Skip to content

Commit

Permalink
Graphql plugin (#23)
Browse files Browse the repository at this point in the history
* graphql call plugin

* add graphql plugin

* add graphql plugin

* add graphql plugin

* add graphql plugin

* add graphql plugin

---------

Co-authored-by: Ab <[email protected]>
  • Loading branch information
fantazista and abdala authored Nov 14, 2024
1 parent 0242fe1 commit 2d7d6da
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dtc-aws-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/nock-aws": "^0.8.0",
"@aws-sdk/client-dynamodb": "^3.645.0",
"@aws-sdk/client-eventbridge": "^3.645.0",
"@aws-sdk/client-lambda": "^3.645.0",
"@aws-sdk/client-sns": "^3.645.0",
"@aws-sdk/lib-dynamodb": "^3.645.0",
"@aws-sdk/util-dynamodb": "^3.645.0"
"@aws-sdk/util-dynamodb": "^3.645.0",
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/nock-aws": "^0.8.0"
},
"scripts": {
"build": "rm -rf dist && tsc",
Expand Down
3 changes: 3 additions & 0 deletions dtc-graphql-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DTC GraphQl Plugin

Interact with GraphQl client
28 changes: 28 additions & 0 deletions dtc-graphql-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@cgauge/dtc-graphql-plugin",
"version": "0.8.2",
"description": "GraphQl plugin for Declarative TestCases",
"repository": {
"type": "git",
"url": "git+https://github.com/cgauge/packages.git"
},
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"author": "Salam Suleymanov",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"graphql-request": "^7.1.2"
},
"scripts": {
"build": "rm -rf dist && tsc",
"test": "tsx --test test/*.test.ts",
"test:only": "tsx --test --test-only test/*.test.ts",
"test:coverage": "tsx --experimental-test-coverage --test test/*.test.ts"
}
}
37 changes: 37 additions & 0 deletions dtc-graphql-plugin/src/graphql-call-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import extraAssert from '@cgauge/assert'
import {isRecord} from '@cgauge/dtc'
import {GraphQLClient} from 'graphql-request'

type GraphQlCall = {url: string; query: string; variables: {}; headers?: Record<string, string>}

const isGraphQlCall = (v: unknown): v is GraphQlCall => isRecord(v) && 'url' in v

let response: Response

export const act = async (args: unknown) => {
if (!isGraphQlCall(args)) {
return
}

const graphQLClient = new GraphQLClient(args.url, {
headers: {
...args.headers,
authorization: process.env.AUTHORIZATION_TOKEN || '',
},
...args,
})

response = await graphQLClient.request(args.query, args.variables)
}

export const assert = async (args: unknown) => {
if (!isRecord(args)) {
return
}

if (!args.graphql) {
return
}

extraAssert.objectContains(response, args.graphql)
}
1 change: 1 addition & 0 deletions dtc-graphql-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as GraphQlPlugin from './graphql-call-plugin.js'
48 changes: 48 additions & 0 deletions dtc-graphql-plugin/test/graphql-call-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {afterEach, test} from 'node:test'
import {act, assert} from '../src/graphql-call-plugin'
import nock from 'nock'
import {appsync, checkForPendingMocks} from './nock-appsync'

nock.disableNetConnect()
afterEach(() => {
checkForPendingMocks()
})

test('It calls a graphql endpoint with the token', async () => {
process.env.AUTHORIZATION_TOKEN = 'token'
const response = {data: {key: 'value'}}
const query = `myQuery { items: {name, status} }`
const variables = {id: 1}

const expectedHeaders = {
authorization: process.env.AUTHORIZATION_TOKEN,
}

appsync({query, variables}, expectedHeaders, response)

await act({
url: `https://appsync.eu-west-1.amazonaws.com`,
query,
variables: {id: 1},
})

await assert({graphql: response})
})

test('It calls a graphql endpoint without the token', async () => {
delete process.env.AUTHORIZATION_TOKEN
const response = {data: {key: 'value'}}
const query = `myQuery { items: {name, status} }`
const expectedHeaders = {
authorization: '',
}

appsync({query}, expectedHeaders, response)

await act({
url: `https://appsync.eu-west-1.amazonaws.com`,
query,
})

await assert({graphql: response})
})
40 changes: 40 additions & 0 deletions dtc-graphql-plugin/test/nock-appsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import nock from 'nock'
import {RequestDocument} from 'graphql-request'
import nodeAssert from 'node:assert/strict'
import extraAssert from '@cgauge/assert'

nock.disableNetConnect()

export interface AppSyncRequest {
query: string | RequestDocument
variables?: Record<string, unknown>
}

export const partialBodyCheck = (expected: AppSyncRequest) => (body: Record<string, unknown>) => {
if (typeof expected === 'string') {
nodeAssert.equal(body, expected)
return true
}

extraAssert.objectContains(body, expected)
return true
}

export const appsync = (
request: AppSyncRequest,
expectedHeaders: Record<string, string>,
response: string | Record<string, unknown>,
): void => {
nock('https://appsync.eu-west-1.amazonaws.com')
.post('/', partialBodyCheck(request))
.matchHeader('authorization', expectedHeaders.authorization)
.reply(200, response)
}

export const checkForPendingMocks = (): void => {
if (!nock.isDone()) {
const error = nock.pendingMocks()
nock.cleanAll()
throw new Error('Not all nock interceptors were used!')
}
}
6 changes: 6 additions & 0 deletions dtc-graphql-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
},
}
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dtc-aws-plugin",
"dtc-mysql-plugin",
"dtc-playwright-plugin",
"dtc-graphql-plugin",
"nock-aws",
"yaml"
],
Expand Down

0 comments on commit 2d7d6da

Please sign in to comment.