|
| 1 | +import { t } from "@typespec/compiler/testing"; |
| 2 | +import * as gql from "@alloy-js/graphql"; |
| 3 | +import { describe, expect, it, beforeEach } from "vitest"; |
| 4 | +import { |
| 5 | + QueryType, |
| 6 | + MutationType, |
| 7 | + SubscriptionType, |
| 8 | +} from "../../src/components/operations/index.js"; |
| 9 | +import { Tester } from "../test-host.js"; |
| 10 | +import { renderComponentToSDL } from "./component-test-utils.js"; |
| 11 | + |
| 12 | +describe("QueryType component", () => { |
| 13 | + let tester: Awaited<ReturnType<typeof Tester.createInstance>>; |
| 14 | + beforeEach(async () => { |
| 15 | + tester = await Tester.createInstance(); |
| 16 | + }); |
| 17 | + |
| 18 | + it("renders nothing when no operations", async () => { |
| 19 | + await tester.compile(t.code`model Placeholder { id: string; }`); |
| 20 | + |
| 21 | + const sdl = renderComponentToSDL(tester.program, <QueryType operations={[]} />); |
| 22 | + |
| 23 | + // Should only contain the placeholder Query from test utils |
| 24 | + expect(sdl).toMatchInlineSnapshot(` |
| 25 | + "type Query { |
| 26 | + _placeholder: Boolean |
| 27 | + }" |
| 28 | + `); |
| 29 | + }); |
| 30 | + |
| 31 | + it("renders single query operation with scalar return type", async () => { |
| 32 | + const { getVersion } = await tester.compile( |
| 33 | + t.code`op ${t.op("getVersion")}(): string;`, |
| 34 | + ); |
| 35 | + |
| 36 | + const sdl = renderComponentToSDL( |
| 37 | + tester.program, |
| 38 | + <QueryType operations={[getVersion]} />, |
| 39 | + { skipPlaceholderQuery: true }, |
| 40 | + ); |
| 41 | + |
| 42 | + expect(sdl).toMatchInlineSnapshot(` |
| 43 | + "type Query { |
| 44 | + getVersion: String! |
| 45 | + }" |
| 46 | + `); |
| 47 | + }); |
| 48 | + |
| 49 | + it("renders query operation with model return type", async () => { |
| 50 | + const { getBook } = await tester.compile( |
| 51 | + t.code` |
| 52 | + model ${t.model("Book")} { id: string; title: string; } |
| 53 | + op ${t.op("getBook")}(id: string): Book; |
| 54 | + `, |
| 55 | + ); |
| 56 | + |
| 57 | + // Stub the Book type so buildSchema can resolve the reference |
| 58 | + const sdl = renderComponentToSDL( |
| 59 | + tester.program, |
| 60 | + <> |
| 61 | + <gql.ObjectType name="Book"> |
| 62 | + <gql.Field name="id" type={gql.String} nonNull /> |
| 63 | + <gql.Field name="title" type={gql.String} nonNull /> |
| 64 | + </gql.ObjectType> |
| 65 | + <QueryType operations={[getBook]} /> |
| 66 | + </>, |
| 67 | + { skipPlaceholderQuery: true }, |
| 68 | + ); |
| 69 | + |
| 70 | + expect(sdl).toMatchInlineSnapshot(` |
| 71 | + "type Book { |
| 72 | + id: String! |
| 73 | + title: String! |
| 74 | + } |
| 75 | +
|
| 76 | + type Query { |
| 77 | + getBook(id: String!): Book! |
| 78 | + }" |
| 79 | + `); |
| 80 | + }); |
| 81 | + |
| 82 | + it("renders multiple query operations", async () => { |
| 83 | + const { getCount, getName } = await tester.compile( |
| 84 | + t.code` |
| 85 | + op ${t.op("getCount")}(): int32; |
| 86 | + op ${t.op("getName")}(): string; |
| 87 | + `, |
| 88 | + ); |
| 89 | + |
| 90 | + const sdl = renderComponentToSDL( |
| 91 | + tester.program, |
| 92 | + <QueryType operations={[getCount, getName]} />, |
| 93 | + { skipPlaceholderQuery: true }, |
| 94 | + ); |
| 95 | + |
| 96 | + expect(sdl).toMatchInlineSnapshot(` |
| 97 | + "type Query { |
| 98 | + getCount: Int! |
| 99 | + getName: String! |
| 100 | + }" |
| 101 | + `); |
| 102 | + }); |
| 103 | + |
| 104 | + it("renders query with parameters", async () => { |
| 105 | + const { search } = await tester.compile( |
| 106 | + t.code`op ${t.op("search")}(query: string, limit?: int32): string[];`, |
| 107 | + ); |
| 108 | + |
| 109 | + const sdl = renderComponentToSDL( |
| 110 | + tester.program, |
| 111 | + <QueryType operations={[search]} />, |
| 112 | + { skipPlaceholderQuery: true }, |
| 113 | + ); |
| 114 | + |
| 115 | + expect(sdl).toMatchInlineSnapshot(` |
| 116 | + "type Query { |
| 117 | + search(query: String!, limit: Int!): [String!]! |
| 118 | + }" |
| 119 | + `); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("MutationType component", () => { |
| 124 | + let tester: Awaited<ReturnType<typeof Tester.createInstance>>; |
| 125 | + beforeEach(async () => { |
| 126 | + tester = await Tester.createInstance(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("renders nothing when no operations", async () => { |
| 130 | + await tester.compile(t.code`model Placeholder { id: string; }`); |
| 131 | + |
| 132 | + const sdl = renderComponentToSDL(tester.program, <MutationType operations={[]} />); |
| 133 | + |
| 134 | + // Should only contain the placeholder Query from test utils |
| 135 | + expect(sdl).toMatchInlineSnapshot(` |
| 136 | + "type Query { |
| 137 | + _placeholder: Boolean |
| 138 | + }" |
| 139 | + `); |
| 140 | + }); |
| 141 | + |
| 142 | + it("renders single mutation operation", async () => { |
| 143 | + const { deleteItem } = await tester.compile( |
| 144 | + t.code`op ${t.op("deleteItem")}(id: string): boolean;`, |
| 145 | + ); |
| 146 | + |
| 147 | + const sdl = renderComponentToSDL( |
| 148 | + tester.program, |
| 149 | + <MutationType operations={[deleteItem]} />, |
| 150 | + ); |
| 151 | + |
| 152 | + expect(sdl).toMatchInlineSnapshot(` |
| 153 | + "type Mutation { |
| 154 | + deleteItem(id: String!): Boolean! |
| 155 | + } |
| 156 | +
|
| 157 | + type Query { |
| 158 | + _placeholder: Boolean |
| 159 | + }" |
| 160 | + `); |
| 161 | + }); |
| 162 | + |
| 163 | + it("renders mutation with input parameters", async () => { |
| 164 | + const { createUser } = await tester.compile( |
| 165 | + t.code` |
| 166 | + model ${t.model("User")} { id: string; name: string; } |
| 167 | + op ${t.op("createUser")}(name: string, email: string): User; |
| 168 | + `, |
| 169 | + ); |
| 170 | + |
| 171 | + const sdl = renderComponentToSDL( |
| 172 | + tester.program, |
| 173 | + <> |
| 174 | + <gql.ObjectType name="User"> |
| 175 | + <gql.Field name="id" type={gql.String} nonNull /> |
| 176 | + <gql.Field name="name" type={gql.String} nonNull /> |
| 177 | + </gql.ObjectType> |
| 178 | + <MutationType operations={[createUser]} /> |
| 179 | + </>, |
| 180 | + ); |
| 181 | + |
| 182 | + expect(sdl).toMatchInlineSnapshot(` |
| 183 | + "type User { |
| 184 | + id: String! |
| 185 | + name: String! |
| 186 | + } |
| 187 | +
|
| 188 | + type Mutation { |
| 189 | + createUser(name: String!, email: String!): User! |
| 190 | + } |
| 191 | +
|
| 192 | + type Query { |
| 193 | + _placeholder: Boolean |
| 194 | + }" |
| 195 | + `); |
| 196 | + }); |
| 197 | +}); |
| 198 | + |
| 199 | +describe("SubscriptionType component", () => { |
| 200 | + let tester: Awaited<ReturnType<typeof Tester.createInstance>>; |
| 201 | + beforeEach(async () => { |
| 202 | + tester = await Tester.createInstance(); |
| 203 | + }); |
| 204 | + |
| 205 | + it("renders nothing when no operations", async () => { |
| 206 | + await tester.compile(t.code`model Placeholder { id: string; }`); |
| 207 | + |
| 208 | + const sdl = renderComponentToSDL( |
| 209 | + tester.program, |
| 210 | + <SubscriptionType operations={[]} />, |
| 211 | + ); |
| 212 | + |
| 213 | + // Should only contain the placeholder Query from test utils |
| 214 | + expect(sdl).toMatchInlineSnapshot(` |
| 215 | + "type Query { |
| 216 | + _placeholder: Boolean |
| 217 | + }" |
| 218 | + `); |
| 219 | + }); |
| 220 | + |
| 221 | + it("renders single subscription operation", async () => { |
| 222 | + const { onMessage } = await tester.compile( |
| 223 | + t.code`op ${t.op("onMessage")}(): string;`, |
| 224 | + ); |
| 225 | + |
| 226 | + const sdl = renderComponentToSDL( |
| 227 | + tester.program, |
| 228 | + <SubscriptionType operations={[onMessage]} />, |
| 229 | + ); |
| 230 | + |
| 231 | + expect(sdl).toMatchInlineSnapshot(` |
| 232 | + "type Subscription { |
| 233 | + onMessage: String! |
| 234 | + } |
| 235 | +
|
| 236 | + type Query { |
| 237 | + _placeholder: Boolean |
| 238 | + }" |
| 239 | + `); |
| 240 | + }); |
| 241 | +}); |
0 commit comments