Skip to content

Commit

Permalink
Merge pull request #7 from thuoe/feature/thu-50-create-type-definitio…
Browse files Browse the repository at this point in the history
…ns-npm-packaging-config

[THU-50]: Package release configuration
  • Loading branch information
thuoe authored Mar 1, 2024
2 parents d9ab34b + 54cf95f commit 6888535
Show file tree
Hide file tree
Showing 12 changed files with 16,582 additions and 7,017 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
cache: "npm"
- name: Install
run: "npm ci"
- name: Build
run: "npm run build:prod"
- name: Lint
run: npm run lint
- name: Unit Test
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Release
on:
push:
branches:
- main

permissions:
contents: read

jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "lts/*"
- name: Install
run: npm ci
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm run release
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
bundle
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.github
.vscode
bundle
server
src
test
.editorconfig
.eslintrc.json
.prettierrc
jest.config.js
nodemon.json
tsconfig.json
tsconfig.*.json
99 changes: 91 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,106 @@
</p>

<h3 align="center">
Simple utlity library for custom GraphQL schema directives
Simple utility library for custom GraphQL schema directives
</h3>

- [Get started](#get-started)
- [Local Development](#local-development)
- [Directives](#directives)
- [@encode](#encode)
- [@regex](#regex)
- [@cache](#cache)
- [@encode `encodingDirective()`](#encode-encodingdirective)
- [@regex `regexDirective()`](#regex-regexdirective)
- [@cache `cacheDirective()`](#cache-cachedirective)
- [Overriding in-memory cache](#overriding-in-memory-cache)

# Get started

🛠️ Work in progress
Install package:

```sh
npm install --save @thuoe/gql-util-directive
```

Example of importing the `@regex` directive & instantiating with Apollo Server:

```typescript
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { makeExecutableSchema } from '@graphql-tools/schema';
import directives from "@thuoe/gql-util-directives";

const typeDefs = String.raw`#graphql
type User {
firstName: String
lastName: String @regex(pattern: "\\b[A-Z]\\w+\\b")
age: Int
}
type Query {
user: User
}
`;

const resolvers = {
Query: {
user: () => ({
firstName: 'Michael',
lastName: 'Jordan',
age: 61,
})
},
};

const { regexDirective } = directives
const { regexDirectiveTypeDefs, regexDirectiveTransformer } = regexDirective('regex')

const transformers = [
regexDirectiveTransformer,
]

let schema = makeExecutableSchema(({
typeDefs: [
regexDirectiveTypeDefs,
typeDefs
],
resolvers
}))

schema = transformers.reduce((curSchema, transformer) => transformer(curSchema), schema)

const server = new ApolloServer({
schema,
});

startStandaloneServer(server, {
listen: { port: 4000 },
}).then(({ url }) => {
console.log(`🚀 Server ready at: ${url}`);
})
```

Here are the possible directive functions that are exposed as part of this util package:

`regexDirective | encodingDirective | cacheDirective`

# Local Development

Install local dependencies:

```sh
npm install
```

Run local environment (Apollo Studio):

```sh
npm run dev
```

Link to Apollo Studio can be found on http://localhost:4000 to perform mutations and queries.

# Directives

## @encode
## @encode `encodingDirective()`

You can use the `@encode` directive on fields defined using the `String` scalar type.

Expand All @@ -38,7 +121,7 @@ type User {
}
```

## @regex
## @regex `regexDirective()`

You can use the `@regex` directive to validate fields using the `String` scalar type. It will throw an
`ValidationError` in the event that the pattern defined has a syntax if no matches are found against the field value.
Expand Down Expand Up @@ -68,7 +151,7 @@ const typeDefs = String.raw`
`;
```

## @cache
## @cache `cacheDirective()`

You can use `@cache` directive to take advantage of a in-memory cache for a field value

Expand Down
Loading

0 comments on commit 6888535

Please sign in to comment.