Skip to content

Commit

Permalink
feat: adding spectacular/role-houdini plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Jul 7, 2024
1 parent 23a259a commit 4a8345a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
19 changes: 1 addition & 18 deletions apps/console/src/lib/graphql/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,6 @@ const logMetadata: ClientPlugin = () => ({
},
});

const setRolePlugin: ClientPlugin = () => ({
start(ctx, { next }) {
const {
artifact: { pluginData },
metadata,
} = ctx;
const role = pluginData?.['@spectacular/role-houdini']?.role;

if (role && !metadata?.useRole) {
console.log('setting role:', role);
if (metadata) metadata.useRole = role;
else ctx.metadata = { useRole: role };
}
next(ctx);
},
});

const subClient: ClientPlugin = subscription(({ session }) =>
createWSClient({
url: url.replace('https://', 'wss://').replace('http://', 'ws://'),
Expand Down Expand Up @@ -105,5 +88,5 @@ export default new HoudiniClient({
// error(500, `(${ctx.artifact.name}): ` + errors.map((err) => err.message).join('. ') + '.')
},
},
plugins: [setRolePlugin, subClient, ...(browser ? [logMetadata] : [])],
plugins: [subClient, ...(browser ? [logMetadata] : [])],
});
4 changes: 2 additions & 2 deletions packages/role-houdini/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Inspired by [grafbase-houdini](https://github.com/grafbase/grafbase/tree/main/pa
}
```

1. Add Role directive to any GLQ. e.g., `@role(name: "user")`
1. Add `@role` directive to your GraphQL Docs. e.g., `@role(name: "user")`

```gql
query GetUser($userId: uuid!) @role(name: "user") {
user(id: $userId) {
...UserDetailsFragment @loading
...UserDetailsFragment
userOrgRoles(order_by: {organization: asc}) {
organization
role
Expand Down
7 changes: 6 additions & 1 deletion packages/role-houdini/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
"./client": {
"import": "./dist/client.js",
"require": "./dist/client.js"
},
"./config": {
"import": "./dist/config.js",
"require": "./dist/config.js"
}
},
"scripts": {
"clean": "rm -rf dist",
"lint": "biome check",
"format": "biome check --write",
"build": "pnpm build:plugin && pnpm build:client",
"build": "pnpm build:plugin && pnpm build:client && pnpm build:config",
"build:plugin": "tsup src/index.ts --format esm,cjs --dts",
"build:client": "tsup src/client.ts --format esm,cjs --dts",
"build:config": "tsup src/config.ts --format esm,cjs --dts",
"dev": "tsup src/index.ts --format esm,cjs --watch --dts"
},
"dependencies": {
Expand Down
15 changes: 4 additions & 11 deletions packages/role-houdini/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ import type { ClientPlugin } from 'houdini';

const plugin: ClientPlugin = () => ({
start(ctx, { next }) {
const {
artifact: { pluginData },
metadata,
} = ctx;
const role = pluginData?.['@spectacular/role-houdini']?.role;

if (role && !metadata?.useRole) {
console.log('setting role:', role);
if (metadata) metadata.useRole = role;
else ctx.metadata = { useRole: role };
const role = ctx.artifact.pluginData?.['@spectacular/role-houdini']?.role;
if (role && ctx.fetchParams?.headers) {
ctx.fetchParams.headers = { ...ctx.fetchParams.headers, 'x-hasura-role': role };
console.log(ctx.fetchParams.headers);
}

next(ctx);
},
});
Expand Down
34 changes: 34 additions & 0 deletions packages/role-houdini/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { ConfigFile } from 'houdini';

/** Configure the default set of scalars supported by Hasura */
export default function config(config: ConfigFile): ConfigFile {
return {
...config,
scalars: {
...config.scalars,
hstore: {
type: 'string',
unmarshal(val) {
return Object.entries(val)
.map(([k, v]) => `"${k}" => "${v}"`)
.join(', ');
},
marshal(val) {
return val;
},
},
citext: {
type: 'string',
...config.scalars?.citext,
},
uuid: {
type: 'string',
...config.scalars?.citext,
},
jsonb: {
type: 'Record<string, any>',
...config.scalars?.JSON,
},
},
};
}
16 changes: 13 additions & 3 deletions packages/role-houdini/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ export default plugin('role-houdini', async () => {
// add the @role directive
schema() {
return `
directive @role(
name: [String!]
) on QUERY
directive @role(
name: String!
) on QUERY
`;
},

/**
* Add the client plugin to the runtime
*/
clientPlugins: {
'@spectacular/role-houdini/client': null,
},

/** Configure the default set of scalars supported by Hasura */
config: '@spectacular/role-houdini/config',

/**
* We want to perform special logic for the the @role directive so we're going to persist
* data in the artifact if we detect it
Expand Down

0 comments on commit 4a8345a

Please sign in to comment.