Skip to content

Commit

Permalink
Merge pull request #250 from gadget-inc/tiny-required-variables
Browse files Browse the repository at this point in the history
Restore support for explicitly required variables in the tiny operation compiler
  • Loading branch information
airhorns authored Aug 11, 2023
2 parents eff5c39 + db47a5d commit a9d924b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/tiny-graphql-query-compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-graphql-query-compiler",
"version": "0.2.0",
"version": "0.2.1",
"type": "module",
"exports": {
".": {
Expand Down
40 changes: 40 additions & 0 deletions packages/tiny-graphql-query-compiler/spec/calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,46 @@ describe("compiling queries with field calls", () => {
`);
});

test("it should compile a query that calls a field with a required variable value", () => {
const result = compile({
type: "query",
fields: {
id: true,
name: true,
truncatedHTML: Call({ length: Var({ type: "Int", required: true }) }),
},
});

expectValidGraphQLQuery(result);
expect(result).toMatchInlineSnapshot(`
"query ($length: Int!) {
id
name
truncatedHTML(length: $length)
}"
`);
});

test("it should compile a query that calls a field with an explicitly non-required variable value", () => {
const result = compile({
type: "query",
fields: {
id: true,
name: true,
truncatedHTML: Call({ length: Var({ type: "Int", required: false }) }),
},
});

expectValidGraphQLQuery(result);
expect(result).toMatchInlineSnapshot(`
"query ($length: Int) {
id
name
truncatedHTML(length: $length)
}"
`);
});

test("it should compile a query that calls fields with multiple variable values", () => {
const result = compile({
type: "query",
Expand Down
3 changes: 2 additions & 1 deletion packages/tiny-graphql-query-compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface VariableOptions {
type: string;
name?: string;
value?: any;
required?: string;
}

/** Represents one reference to a variable somewhere in a selection */
Expand All @@ -100,7 +101,7 @@ export class Variable {
export const Call = (args: Record<string, Variable | any>, subselection?: FieldSelection) => new FieldCall(args, subselection);

/** Used for calling a field with a variable within the args to a field */
export const Var = (options: VariableOptions) => new Variable(options.type, options.name, options.value);
export const Var = (options: VariableOptions) => new Variable(options.type + (options.required ? "!" : ""), options.name, options.value);

/** Compiles one JS object describing a query into a GraphQL string */
export const compile = (operation: BuilderOperation): string => {
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9d924b

Please sign in to comment.