|
| 1 | +import { NoMissingClientDirectives } from "../validation"; |
| 2 | +import { GraphQLClientProject } from "../../project/client"; |
| 3 | +import { readFileSync } from "fs"; |
| 4 | +import { basename } from "path"; |
| 5 | + |
| 6 | +import { vol } from "memfs"; |
| 7 | +import { LoadingHandler } from "../../loadingHandler"; |
| 8 | +import { ApolloConfig, ClientConfig } from "../../config"; |
| 9 | +import URI from "vscode-uri"; |
| 10 | + |
| 11 | +const serviceSchema = /* GraphQL */ ` |
| 12 | + type Query { |
| 13 | + me: User |
| 14 | + } |
| 15 | +
|
| 16 | + type User { |
| 17 | + name: String |
| 18 | + friends: [User] |
| 19 | + } |
| 20 | +`; |
| 21 | +const clientSchema = /* GraphQL */ ` |
| 22 | + extend type Query { |
| 23 | + isOnline: Boolean |
| 24 | + } |
| 25 | + extend type User { |
| 26 | + isLiked: Boolean |
| 27 | + localUser: User |
| 28 | + } |
| 29 | +`; |
| 30 | +const a = /* GraphQL */ ` |
| 31 | + query a { |
| 32 | + isOnline |
| 33 | + me { |
| 34 | + name |
| 35 | + localUser @client { |
| 36 | + friends { |
| 37 | + isLiked |
| 38 | + } |
| 39 | + } |
| 40 | + friends { |
| 41 | + name |
| 42 | + isLiked |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +`; |
| 47 | + |
| 48 | +const b = /* GraphQL */ ` |
| 49 | + query b { |
| 50 | + me { |
| 51 | + ... { |
| 52 | + isLiked |
| 53 | + } |
| 54 | + ... @client { |
| 55 | + localUser { |
| 56 | + name |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +`; |
| 62 | + |
| 63 | +const c = /* GraphQL */ ` |
| 64 | + query c { |
| 65 | + me { |
| 66 | + ...isLiked |
| 67 | + } |
| 68 | + } |
| 69 | + fragment localUser on User @client { |
| 70 | + localUser { |
| 71 | + name |
| 72 | + } |
| 73 | + } |
| 74 | + fragment isLiked on User { |
| 75 | + isLiked |
| 76 | + ...localUser |
| 77 | + } |
| 78 | +`; |
| 79 | + |
| 80 | +const d = /* GraphQL */ ` |
| 81 | + fragment isLiked on User { |
| 82 | + isLiked |
| 83 | + } |
| 84 | + query d { |
| 85 | + me { |
| 86 | + ...isLiked |
| 87 | + ...locaUser |
| 88 | + } |
| 89 | + } |
| 90 | + fragment localUser on User @client { |
| 91 | + localUser { |
| 92 | + name |
| 93 | + } |
| 94 | + } |
| 95 | +`; |
| 96 | + |
| 97 | +const e = /* GraphQL */ ` |
| 98 | + fragment friends on User { |
| 99 | + friends { |
| 100 | + ...isLiked |
| 101 | + ... on User @client { |
| 102 | + localUser { |
| 103 | + name |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + query e { |
| 109 | + isOnline @client |
| 110 | + me { |
| 111 | + ...friends |
| 112 | + } |
| 113 | + } |
| 114 | + fragment isLiked on User { |
| 115 | + isLiked |
| 116 | + } |
| 117 | +`; |
| 118 | + |
| 119 | +// TODO support inline fragment spreads |
| 120 | +const f = /* GraphQL */ ` |
| 121 | + query f { |
| 122 | + me { |
| 123 | + ...isLiked @client |
| 124 | + } |
| 125 | + } |
| 126 | + fragment isLiked on User { |
| 127 | + isLiked |
| 128 | + } |
| 129 | +`; |
| 130 | + |
| 131 | +const rootURI = URI.file(process.cwd()); |
| 132 | + |
| 133 | +const config = new ApolloConfig({ |
| 134 | + client: { |
| 135 | + service: { |
| 136 | + name: "server", |
| 137 | + localSchemaFile: "./schema.graphql" |
| 138 | + }, |
| 139 | + includes: ["./src/**.graphql"], |
| 140 | + excludes: ["./__tests__"], |
| 141 | + validationRules: [NoMissingClientDirectives] |
| 142 | + }, |
| 143 | + engine: {} |
| 144 | +}) as ClientConfig; |
| 145 | + |
| 146 | +class MockLoadingHandler implements LoadingHandler { |
| 147 | + handle<T>(_message: string, value: Promise<T>): Promise<T> { |
| 148 | + return value; |
| 149 | + } |
| 150 | + handleSync<T>(_message: string, value: () => T): T { |
| 151 | + return value(); |
| 152 | + } |
| 153 | + showError(_message: string): void {} |
| 154 | +} |
| 155 | + |
| 156 | +jest.mock("fs"); |
| 157 | + |
| 158 | +describe("client state", () => { |
| 159 | + beforeEach(() => { |
| 160 | + vol.fromJSON({ |
| 161 | + "apollo.config.js": `module.exports = { |
| 162 | + client: { |
| 163 | + service: { |
| 164 | + localSchemaFile: './schema.graphql' |
| 165 | + } |
| 166 | + } |
| 167 | + }`, |
| 168 | + "schema.graphql": serviceSchema, |
| 169 | + "src/client-schema.graphql": clientSchema, |
| 170 | + "src/a.graphql": a, |
| 171 | + "src/b.graphql": b, |
| 172 | + "src/c.graphql": c, |
| 173 | + "src/d.graphql": d, |
| 174 | + "src/e.graphql": e |
| 175 | + // "src/f.graphql": f, |
| 176 | + }); |
| 177 | + }); |
| 178 | + afterEach(jest.restoreAllMocks); |
| 179 | + |
| 180 | + it("should report validation errors for missing @client directives", async () => { |
| 181 | + const project = new GraphQLClientProject({ |
| 182 | + config, |
| 183 | + loadingHandler: new MockLoadingHandler(), |
| 184 | + rootURI |
| 185 | + }); |
| 186 | + |
| 187 | + const errors = Object.create(null); |
| 188 | + project.onDiagnostics(({ diagnostics, uri }) => { |
| 189 | + const path = basename(URI.parse(uri).path); |
| 190 | + diagnostics.forEach(({ error }: any) => { |
| 191 | + if (!errors[path]) errors[path] = []; |
| 192 | + errors[path].push(error); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + await project.whenReady; |
| 197 | + await project.validate(); |
| 198 | + |
| 199 | + expect(errors).toMatchInlineSnapshot(` |
| 200 | + Object { |
| 201 | + "a.graphql": Array [ |
| 202 | + [GraphQLError: @client directive is missing on local field "isOnline"], |
| 203 | + [GraphQLError: @client directive is missing on local field "isLiked"], |
| 204 | + ], |
| 205 | + "b.graphql": Array [ |
| 206 | + [GraphQLError: @client directive is missing on fragment around local fields "isLiked"], |
| 207 | + ], |
| 208 | + "c.graphql": Array [ |
| 209 | + [GraphQLError: @client directive is missing on fragment "isLiked" around local fields "isLiked,localUser"], |
| 210 | + ], |
| 211 | + "d.graphql": Array [ |
| 212 | + [GraphQLError: @client directive is missing on fragment "isLiked" around local fields "isLiked"], |
| 213 | + ], |
| 214 | + "e.graphql": Array [ |
| 215 | + [GraphQLError: @client directive is missing on fragment "isLiked" around local fields "isLiked"], |
| 216 | + ], |
| 217 | + } |
| 218 | + `); |
| 219 | + }); |
| 220 | +}); |
0 commit comments