|
1 | 1 | import { resolve } from 'path';
|
2 | 2 | import { readFileSync } from 'fs';
|
| 3 | +import { spawn } from 'child_process'; |
3 | 4 | import { GraphQLSchema, printSchema } from 'graphql';
|
4 | 5 | import { getSchema } from '../src/schema';
|
5 | 6 | import { loadGraphqlConfig } from '../src/graphql-config';
|
@@ -37,10 +38,61 @@ describe('schema', () => {
|
37 | 38 | });
|
38 | 39 | });
|
39 | 40 |
|
40 |
| - // TODO: make works when url-loader will be updated to v7 |
41 |
| - // describe('UrlLoader', () => { |
42 |
| - // it('should load schema from URL', () => { |
43 |
| - // testSchema(url); |
44 |
| - // }); |
45 |
| - // }); |
| 41 | + describe('UrlLoader', () => { |
| 42 | + let local; |
| 43 | + let url; |
| 44 | + |
| 45 | + beforeAll(done => { |
| 46 | + const tsNodeCommand = resolve(process.cwd(), 'node_modules/.bin/ts-node'); |
| 47 | + const serverPath = resolve(__dirname, 'mocks/graphql-server.ts'); |
| 48 | + |
| 49 | + // Import `TestGraphQLServer` and run it in this file will don't work |
| 50 | + // because `@graphql-tools/url-loader` under the hood uses `sync-fetch` package that uses |
| 51 | + // `child_process.execFileSync` that block Node.js event loop |
| 52 | + local = spawn(tsNodeCommand, [serverPath]); |
| 53 | + local.stdout.on('data', chunk => { |
| 54 | + url = chunk.toString().trimRight(); |
| 55 | + done(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(done => { |
| 60 | + local.on('close', done); |
| 61 | + local.kill(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should load schema from URL', () => { |
| 65 | + testSchema(url); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should passe headers', () => { |
| 69 | + expect.assertions(2); |
| 70 | + const schemaUrl = `${url}/my-headers`; |
| 71 | + const schemaOptions = { |
| 72 | + headers: { |
| 73 | + Authorization: 'Bearer Foo', |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + try { |
| 78 | + // https://graphql-config.com/schema#passing-headers |
| 79 | + const gqlConfig = loadGraphqlConfig({ |
| 80 | + schema: { |
| 81 | + [schemaUrl]: schemaOptions, |
| 82 | + }, |
| 83 | + }); |
| 84 | + getSchema(undefined, gqlConfig); |
| 85 | + } catch (e) { |
| 86 | + expect(e.message).toMatch('"authorization":"Bearer Foo"'); |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + // https://github.com/dotansimha/graphql-eslint/blob/master/docs/parser-options.md#schemaoptions |
| 91 | + const gqlConfig = loadGraphqlConfig({ schema: schemaUrl }); |
| 92 | + getSchema({ schemaOptions }, gqlConfig); |
| 93 | + } catch (e) { |
| 94 | + expect(e.message).toMatch('"authorization":"Bearer Foo"'); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
46 | 98 | });
|
0 commit comments