项目作者: geraldoramos

项目描述 :
🚀 Easy-to-use, Axios-based HTTP client for GraphQL API's
高级语言: TypeScript
项目地址: git://github.com/geraldoramos/graphios.git
创建时间: 2019-10-07T03:46:08Z
项目社区:https://github.com/geraldoramos/graphios

开源协议:MIT License

下载


Graphios

Easy-to-use Axios-based HTTP client for GraphQL API’s. This library is more suitable for back-end interactions with GraphQL servers (not frontends/UI’s). The Auto-pagination feature allows fetching of large amounts of data from paginated GraphQL API’s without the need to write custom code.

Features:

  • Simple API focused on GraphQL use case.
  • Auto-pagination (Relay pattern only, such as Github API)
  • Event listener for paginated requests
  • Configurable retries (and retryDelay)

Installing

Using npm:

  1. $ npm install graphios

Using yarn:

  1. $ yarn add graphios

Configuration object (input)

  1. interface Config {
  2. url: string // graphQL endpoint
  3. query: string // graphQl query
  4. headers?: object // Optional headers object
  5. timeout?: number // Optional custom timeout in ms (default to no timeout)
  6. retries?: number // Optional retries (default to 3)
  7. retryDelay?: number // Optional retryDelay in ms (default to 500)
  8. pagination?: boolean // Optional Auto-pagination (default to false)
  9. requestId?: string // Optional request identifier, useful for monitoring page iterations
  10. pageDelay?: number // Optional delay between pagination iterations in ms (default to 200)
  11. }

Response object (output)

  1. interface GraphiosResponse {
  2. data: object // Response data
  3. status?: number // not present on auto-paginated requests
  4. statusText?: string // not present on auto-paginated requests
  5. headers?: object // not present on auto-paginated requests
  6. pagesProcessed?: number // Only available for auto-paginated requests
  7. }

Examples

Performing a simple query:

  1. import { graphios } from 'graphios';
  2. const query = `{
  3. users {
  4. firstName
  5. email
  6. }
  7. }`
  8. // using promises
  9. graphios({
  10. query,
  11. url: 'https://mygraphql.xyz/graphql'
  12. })
  13. .then( response => {
  14. // handle success
  15. console.log(response.data);
  16. })
  17. .catch( error => {
  18. // handle error
  19. console.log(error);
  20. })
  21. // Using async/await
  22. (async () => {
  23. try {
  24. const response = await graphios({
  25. query,
  26. url: 'https://mygraphql.xyz/graphql',
  27. })
  28. console.log(response.data);
  29. } catch (error) {
  30. console.error(error);
  31. }
  32. })()

Performing a paginated query

For auto-pagination support, a $cursor variable should be included in the query. The API server must follow the relay pagination pattern using nodes and edges. The paginated query is not required to be on the first level, but only one pagination query is allowed per request.

Example using an usable query for the Github API

  1. // import graphiosEvents for pagination events
  2. import { graphios, graphiosEvents } from 'graphios';
  3. // A query variable $cursor is required.
  4. const query = `
  5. query Repository($cursor:String){
  6. organization(login:"microsoft"){
  7. membersWithRole(first:100, after:$cursor){
  8. edges {
  9. node {
  10. name
  11. }
  12. }
  13. pageInfo {
  14. endCursor
  15. hasNextPage
  16. }
  17. }
  18. }
  19. }
  20. `
  21. const headers = {
  22. 'authorization': `Bearer 1234567SuperHardKey8910`
  23. }
  24. // using promises
  25. graphios({
  26. query,
  27. url: 'https://api.github.com/graphql',
  28. headers,
  29. retries: 3,
  30. retryDelay: 500,
  31. pagination: true,
  32. requestId: 'myRepoReq123'
  33. pageDelay: 100
  34. })
  35. .then( response => {
  36. // handle success
  37. console.log(response.data);
  38. })
  39. .catch( error => {
  40. // If any page fails, it will end here
  41. console.log(error);
  42. })
  43. // Using async/await
  44. (async () => {
  45. try {
  46. const response = await graphios({
  47. query,
  48. url: 'https://api.github.com/graphql',
  49. headers,
  50. retries: 3,
  51. retryDelay: 500
  52. pagination: true,
  53. requestId: 'myRepoReq123',
  54. pageDelay: 100
  55. })
  56. console.log(response.data);
  57. } catch (error) {
  58. // If any page fails, it will end here
  59. console.error(error);
  60. }
  61. })()
  62. // Optional pagination event handler
  63. graphiosEvents.on('pagination', pageData => {
  64. console.log(
  65. pageData.page // Page number,
  66. pageData.response, // Response object, same as an individual request
  67. pageData.requestId // Request identifier set on the config: 'myRepoReq123'
  68. })

Axios custom configuration (optional input)

An optional second object can be passed if any specific configuration from Axios is needed. This will override any configuration made on the graphios config (first parameter).

  1. interface AxiosRequestConfig {
  2. url?: string
  3. method?: Method
  4. baseURL?: string
  5. transformRequest?: AxiosTransformer | AxiosTransformer[]
  6. transformResponse?: AxiosTransformer | AxiosTransformer[]
  7. headers?: any
  8. params?: any
  9. paramsSerializer?: (params: any) => string
  10. data?: any
  11. timeout?: number
  12. withCredentials?: boolean
  13. adapter?: AxiosAdapter
  14. auth?: AxiosBasicCredentials
  15. responseType?: ResponseType
  16. xsrfCookieName?: string
  17. xsrfHeaderName?: string
  18. onUploadProgress?: (progressEvent: any) => void
  19. onDownloadProgress?: (progressEvent: any) => void
  20. maxContentLength?: number
  21. validateStatus?: (status: number) => boolean
  22. maxRedirects?: number
  23. socketPath?: string | null
  24. httpAgent?: any
  25. httpsAgent?: any
  26. proxy?: AxiosProxyConfig | false
  27. cancelToken?: CancelToken
  28. }

Example:

  1. graphios(
  2. {
  3. query: `{ Users { name } }`,
  4. url: 'https://mygraphql.xyz/graphql'
  5. },
  6. {
  7. maxRedirects: 10
  8. }
  9. )
  10. .then( response => {
  11. // handle success
  12. console.log(response.data)
  13. })
  14. .catch( error => {
  15. // If any page fails, it will end here
  16. console.log(error)
  17. })

Credits

Relies on the excellent work provided by axios and axios-retry.

License

MIT