Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add initial support for fragments #81

Merged
merged 18 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Tests and CI

on:
push:
branches-ignore: main
branches-ignore:
- main
pull_request:
branches: main
branches:
- main

jobs:
build:
Expand All @@ -16,14 +15,14 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [18.x, 20.x, 22.x, 23.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
15 changes: 6 additions & 9 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Build and Publish

on:
Expand All @@ -13,10 +10,10 @@ jobs:
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')"
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
node-version: 22
- name: Install Dependencies
run: npm ci
- name: Test
Expand All @@ -26,10 +23,10 @@ jobs:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 22
registry-url: https://registry.npmjs.org/
- name: Install Dependencies
run: npm ci
Expand Down
51 changes: 42 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
</a>
</h1>


This is a better implementation of the [GraphQL](https://github.com/facebook/graphql) query API via NodeJS, created as a wrapper of [Got](http://github.com/sindresorhus/got). It works like a transpiler, with a built in HTTPRequest Client (Got), allowing you to write your GraphQL queries as Javascript Objects instead of strings.

Built because manipulating strings is a real pain.
Expand All @@ -46,6 +45,7 @@ Built because manipulating strings is a real pain.
- [Query with variables](#query-with-variables)
- [Nested fields](#nested-fields)
- [Enum args](#enum-args)
- [Fragment Support](#fragment-support)
- [Contributing to this project](#contributing-to-this-project)

<!-- /TOC -->
Expand Down Expand Up @@ -76,14 +76,15 @@ const query = {

const options = {
headers: {
"Authorization": "Bearer <token>"
Authorization: 'Bearer <token>'
},
debug: false,
useHttp2: false
}

gotQL.query('mygraphqlendpoint.com.br/api', query, options)
.then(response => console.log(response.data))
gotQL
.query('mygraphqlendpoint.com.br/api', query, options)
.then((response) => console.log(response.data))
.catch(console.error)
```

Expand Down Expand Up @@ -119,7 +120,7 @@ const mutation = {
id: variables.id
}
},
fields: [ 'uuid' ]
fields: ['uuid']
}
}
```
Expand Down Expand Up @@ -203,7 +204,7 @@ Both `gotql.query` and `gotql.mutation` accept an optional user option object wi
- _useHttp2_: Boolean defining if the call should be made using HTTP2, defaults to `false` (see [release 11 of got](https://github.com/sindresorhus/got/releases/tag/v11.0.0))
- Type: `boolean`

>**Note:** GotQL uses [`debug`](https://npmjs.com/package/debug) internally as default debugger, so you can set debug levels by setting the `DEBUG` environment variable. These are the current levels:
> **Note:** GotQL uses [`debug`](https://npmjs.com/package/debug) internally as default debugger, so you can set debug levels by setting the `DEBUG` environment variable. These are the current levels:
>
> - `gotql:info`
> - `gotql:info:parser`
Expand Down Expand Up @@ -287,7 +288,7 @@ const query = {
- Example: `args { name: 'myName' }`
- **_Detailed args_**: A tagged template. This will give more control over escaping (mostly to use enums). Argument name should be the key
- Type: `tagged template`
- Examples: ```args: { status: literal`an_enum` }``` should output `operation (status: an_enum)...`
- Examples: `` args: { status: literal`an_enum` } `` should output `operation (status: an_enum)...`
- _fields_: The field list to get back from the operation
- Type: An `array` of `object` (to use nested fields) or `string`, or both.
- Properties (for nested fields):
Expand All @@ -299,7 +300,9 @@ const query = {
- Example: `args { name: 'myName' }`
- **_Detailed args_**: A tagged template. This will give more control over escaping (mostly to use enums). Argument name should be the key
- Type: `tagged template`
- Examples: ```args: { status: literal`an_enum` }``` should output `operation (status: an_enum)...`
- Examples: `` args: { status: literal`an_enum` } `` should output `operation (status: an_enum)...`
- **_fragments_**: The fragments of the query, see [Fragments Support](#fragment-support) for more information
- Type: `string[]`

### Examples

Expand Down Expand Up @@ -466,7 +469,37 @@ If `literal` is omitted, or if `escape` is set to `true`, the output would be:
query { users(type: "internal") { name age } }
```

> **Note:** Variables such as described [here](#query-with-variables) will __not__ be recognized. If the arg object is not an `[argName]: value`, variables will not pass through the definition check (GotQL warns if a variable is not declared but used on operation).
> **Note:** Variables such as described [here](#query-with-variables) will **not** be recognized. If the arg object is not an `[argName]: value`, variables will not pass through the definition check (GotQL warns if a variable is not declared but used on operation).

### Fragment support

Fragment support is in an alpha state (see #55), this means that, while the lib
supports fragments, it's not as pretty or as tested as I'd like it to be, but
PR's are welcome if you want to use it thoroughly.

You can use fragments by adding a new key in the query JSON, besides
`operation`, just like you do with variables. This key is called `fragments` and
it's an array of strings that represent fragments in operations, for example:

```js
const query = {
operation: {
fields: ['f1']
},
fragments: [`fragment Test on Character { name id }`]
}
```

You can then reference those fragments using the literal struct `fragment`:

```js
const query = {
operation: {
fields: [fragment`Test`]
},
fragments: [`fragment Test on Character { name id }`]
}
```

## Contributing to this project

Expand Down
3 changes: 2 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"/coverage/",
"/.devcontainer/",
"/.nyc_output/",
"/.vscode/"
"/.vscode/",
"/src/helpers/index.ts"
]
}
Loading
Loading