Skip to content

Commit

Permalink
Add options for new directives
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Dec 26, 2023
1 parent 0979efd commit 916cef7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
38 changes: 38 additions & 0 deletions packages/plugin-federation/src/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,44 @@ declare global {
) => void;
}

export interface UserSchemaTypes {
FederationScopes: unknown;
FederationPolicies: unknown;
}

export interface ExtendDefaultTypes<PartialTypes extends Partial<UserSchemaTypes>> {
FederationScopes: string extends PartialTypes['FederationScopes']
? string
: PartialTypes['FederationScopes'];
FederationPolicies: string extends PartialTypes['FederationPolicies']
? string
: PartialTypes['FederationPolicies'];
}

export interface ObjectTypeOptions<Types extends SchemaTypes, Shape> {
shareable?: boolean;
tag?: string[] | string;
authenticated?: boolean;
requiresScopes?: Types['FederationScopes'][][];
policy?: Types['FederationPolicies'][][];
}

export interface InterfaceTypeOptions<Types extends SchemaTypes, Shape> {
authenticated?: boolean;
requiresScopes?: Types['FederationScopes'][][];
policy?: Types['FederationPolicies'][][];
}

export interface ScalarTypeOptions<Types extends SchemaTypes> {
authenticated?: boolean;
requiresScopes?: Types['FederationScopes'][][];
policy?: Types['FederationPolicies'][][];
}

export interface EnumTypeOptions<Types extends SchemaTypes> {
authenticated?: boolean;
requiresScopes?: Types['FederationScopes'][][];
policy?: Types['FederationPolicies'][][];
}

export interface BaseTypeOptions<Types extends SchemaTypes = SchemaTypes> {
Expand All @@ -133,6 +168,9 @@ declare global {
inaccessible?: boolean;
override?: { from: string };
tag?: string[] | string;
authenticated?: boolean;
requiresScopes?: Types['FederationScopes'][][];
policy?: Types['FederationPolicies'][][];
}

export interface InputFieldOptions<
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin-federation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ function getCommonDirectives<
tag?: string[] | string;
inaccessible?: boolean;
shareable?: boolean;
authenticated?: boolean;
requiresScopes?: unknown[][];
policy?: unknown[][];
};
},
>(config: T) {
Expand All @@ -117,10 +120,19 @@ function getCommonDirectives<
? [config.pothosOptions.tag]
: config.pothosOptions.tag ?? [];
const tagDirectives = tags.map((tag) => ({ name: 'tag', args: { name: tag } }));
const requiresScopes = config.pothosOptions.requiresScopes
? { name: 'requiresScopes', args: { scopes: config.pothosOptions.requiresScopes } }
: null;
const policy = config.pothosOptions.policy
? { name: 'policy', args: { policies: config.pothosOptions.policy } }
: null;

return [
config.pothosOptions.inaccessible ? { name: 'inaccessible' } : null,
config.pothosOptions.shareable ? { name: 'shareable' } : null,
config.pothosOptions.authenticated ? { name: 'authenticated' } : null,
requiresScopes,
policy,
...tagDirectives,
].filter(Boolean) as { name: string }[];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FederationPlugin from '../../../src';

const builder = new SchemaBuilder<{
DefaultFieldNullability: true;
FederationPolicies: 'user:policy';
}>({
plugins: [DirectivesPlugin, FederationPlugin],
useGraphQLToolsUnorderedDirectives: true,
Expand Down
5 changes: 4 additions & 1 deletion website/pages/docs/plugins/federation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ t.field({
tag: ['someTag'],
inaccessible: true,
override: { from: 'users' },
authenticated: true,
scopes: ['read:field'],
policy: [['read:field']],
});
```

Expand Down Expand Up @@ -262,7 +265,7 @@ builder.asEntity(Media, {

See federation documentation for more details on `interfaceObject`s

### composeDirective =
### composeDirective

You can apply the `composeDirective` directive when building the subgraph schema:

Expand Down

0 comments on commit 916cef7

Please sign in to comment.