-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
0242fe1
commit 2d7d6da
Showing
10 changed files
with
214 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# DTC GraphQl Plugin | ||
|
||
Interact with GraphQl client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as GraphQlPlugin from './graphql-call-plugin.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
}, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters