-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🦄 Add first version with basic features (#8)
- Loading branch information
Cuong Duy Nguyen
authored
Dec 30, 2019
1 parent
9332acb
commit 5a61684
Showing
6 changed files
with
601 additions
and
1,042 deletions.
There are no files selected for viewing
This file was deleted.
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
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,25 @@ | ||
import { giin } from '../src'; | ||
|
||
test('giin Test', async() => { | ||
const data = await giin({ | ||
option: { | ||
url: 'https://graphqlzero.almansi.me/api' | ||
}, | ||
query: ` | ||
query { | ||
post(id: $id) { | ||
id | ||
title | ||
body | ||
} | ||
} | ||
`, | ||
variables: { | ||
id: 1 | ||
} | ||
}); | ||
const { post } = data; | ||
expect(post.id).toEqual('1'); | ||
expect(typeof post.title).toEqual('string'); | ||
expect(typeof post.body).toEqual('string'); | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,41 @@ | ||
export function giin(): string { | ||
return 'giin'; | ||
// tslint:disable | ||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'; | ||
|
||
function formatVariables(variables: any): any { | ||
let formatedVariables = JSON.stringify(variables); | ||
formatedVariables = formatedVariables.replace(/"([^"]+)"\s*:\s*/g, '$1:'); | ||
return formatedVariables; | ||
} | ||
function formatQuery(query: string, variables: any): string { | ||
let formatedQuery = query; | ||
for (const key in variables) { | ||
const regex = new RegExp(`\\$${key}`, 'g'); | ||
formatedQuery = formatedQuery.replace(regex, formatVariables(variables[key])); | ||
} | ||
return formatedQuery; | ||
} | ||
|
||
interface Option { | ||
url: string; | ||
headers?: AxiosRequestConfig['headers']; | ||
} | ||
|
||
export function giin({ | ||
query, | ||
variables, | ||
option, | ||
}: { | ||
query: string; | ||
variables?: any; | ||
option: Option; | ||
}): Promise<any> { | ||
return axios.post( | ||
option.url, | ||
{ | ||
query: formatQuery(query, variables), | ||
}, | ||
{ | ||
headers: option.headers, | ||
}, | ||
).then((res: AxiosResponse) => res.data.data); | ||
} |
Oops, something went wrong.