Skip to content

Commit

Permalink
Rename @pure to @readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers committed Jan 17, 2024
1 parent a62dc33 commit 4323071
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Any functions exported from `functions.ts` are made available as NDC functions/p
Arguments to the function end up being field arguments in GraphQL and the return value is what the field will return when queried. Every function must return a value; `void`, `null` or `undefined` is not supported.

```typescript
/** @pure */
/** @readonly */
export function hello(name: string, year: number): string {
return `Hello ${name}, welcome to ${year}`
}
Expand Down Expand Up @@ -130,16 +130,16 @@ These types are unsupported as function parameter types or return types for func


### Impure/pure functions
If you write a function that performs a read-only operation, or is otherwise a pure function (no side-effects), you can mark it with the `@pure` JSDoc tag, and it will be exposed as an NDC function, which will ultimately show up as a GraphQL query field in Hasura.
If you write a function that performs a read-only operation, you should mark it with the `@readonly` JSDoc tag, and it will be exposed as an NDC function, which will ultimately show up as a GraphQL query field in Hasura.

```typescript
/** @pure */
/** @readonly */
export function add(x: number, y: number): number {
return x + y;
}
```

Functions without the `@pure` JSDoc tag are exposed as NDC procedures, which will ultimately show up as a GraphQL mutation field in Hasura.
Functions without the `@readonly` JSDoc tag are exposed as NDC procedures, which will ultimately show up as a GraphQL mutation field in Hasura.

## Deploying with `hasura3 connector create`

Expand Down
4 changes: 2 additions & 2 deletions ndc-lambda-sdk/src/inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function deriveFunctionSchema(functionDeclaration: ts.FunctionDeclaration, conte
const functionType = context.typeChecker.getTypeOfSymbolAtLocation(functionSymbol, functionDeclaration);

const functionDescription = ts.displayPartsToString(functionSymbol.getDocumentationComment(context.typeChecker)).trim();
const markedPureInJsDoc = functionSymbol.getJsDocTags().find(e => e.name === "pure") !== undefined;
const markedReadonlyInJsDoc = functionSymbol.getJsDocTags().find(e => e.name === "readonly") !== undefined;

const functionCallSig = functionType.getCallSignatures()[0] ?? throwError(`Function '${functionName}' didn't have a call signature`)
const functionSchemaArguments: Result<schema.ArgumentDefinition[], string[]> = Result.traverseAndCollectErrors(functionCallSig.getParameters(), paramSymbol => {
Expand All @@ -190,7 +190,7 @@ function deriveFunctionSchema(functionDeclaration: ts.FunctionDeclaration, conte
const functionDefinition = Result.collectErrors(functionSchemaArguments, returnTypeResult)
.map(([functionSchemaArgs, returnType]) => ({
description: functionDescription ? functionDescription : null,
ndcKind: markedPureInJsDoc ? schema.FunctionNdcKind.Function : schema.FunctionNdcKind.Procedure,
ndcKind: markedReadonlyInJsDoc ? schema.FunctionNdcKind.Function : schema.FunctionNdcKind.Procedure,
arguments: functionSchemaArgs,
resultType: returnType
}));
Expand Down
6 changes: 3 additions & 3 deletions ndc-lambda-sdk/test/inference/basic-inference/simple-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export function hello(): string {
}

/**
* @pure
* @readonly
*/
export function add(a: number, b: number): number {
return a + b;
}

/**
* @pure
* @readonly
*/
export function isEven(x: bigint): boolean {
return x % 2n === 0n;
Expand All @@ -26,7 +26,7 @@ export function dateTime(): Date {
}

/**
* @pure
* @readonly
*/
export function json(input: sdk.JSONValue): sdk.JSONValue {
const jsonValue = input.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { emojify } from "node-emoji";

/**
* @pure
* @readonly
*/
export function useImportedPackage(s: string): string {
return emojify(`${s} :t-rex: :heart: NPM`);
Expand Down
2 changes: 1 addition & 1 deletion yeoman-generator/src/app/templates/functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @pure Exposes the function as an NDC function (the function should only query data without making modifications)
* @readonly Exposes the function as an NDC function (the function should only query data without making modifications)
*/
export function hello(name?: string) {
return `hello ${name ?? "world"}`;
Expand Down

0 comments on commit 4323071

Please sign in to comment.