-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3️⃣ Test schema loaders, fix `Expected signal to be an instanceof Abo…
…rtSignal` error when `UrlLoader` is used (#542)
- Loading branch information
Dimitri POSTOLOV
committed
Jul 29, 2021
1 parent
5065482
commit 5e8ebd8
Showing
10 changed files
with
153 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-eslint/eslint-plugin': patch | ||
--- | ||
|
||
add tests for schema loaders |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { readFileSync } from 'fs'; | ||
import { resolve } from 'path'; | ||
import { createServer, Server, IncomingMessage, ServerResponse } from 'http'; | ||
import { buildSchema, getIntrospectionQuery, graphqlSync } from 'graphql'; | ||
|
||
const sdlSchema = readFileSync(resolve(__dirname, 'user-schema.graphql'), 'utf8'); | ||
const graphqlSchemaObj = buildSchema(sdlSchema); | ||
const introspectionQueryResult = graphqlSync(graphqlSchemaObj, getIntrospectionQuery()); | ||
|
||
class TestGraphQLServer { | ||
private server: Server; | ||
private base: string; | ||
|
||
constructor(private port = 1337) { | ||
this.server = createServer(this.router.bind(this)); | ||
this.base = `http://localhost:${this.port}`; | ||
} | ||
|
||
start(): Promise<{ url: string }> { | ||
return new Promise(resolve => { | ||
this.server.listen(this.port, () => { | ||
resolve({ url: this.base }); | ||
}); | ||
}); | ||
} | ||
|
||
stop(): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this.server.close(err => { | ||
err ? reject(err) : resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
private async router(req: IncomingMessage, res: ServerResponse): Promise<void> { | ||
const { pathname } = new URL(req.url, this.base); | ||
|
||
if (pathname === '/') { | ||
const { query } = await this.parseData(req); | ||
if (query.includes('query IntrospectionQuery')) { | ||
res.end(JSON.stringify(introspectionQueryResult)); | ||
} | ||
} else if (pathname === '/my-headers') { | ||
res.end(JSON.stringify(req.headers)); | ||
} | ||
} | ||
|
||
parseData(req: IncomingMessage): Promise<any | string> { | ||
return new Promise(resolve => { | ||
let data = ''; | ||
req.on('data', chunk => { | ||
data += chunk; | ||
}); | ||
req.on('end', () => { | ||
if (req.headers['content-type'] === 'application/json') { | ||
resolve(JSON.parse(data)); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
const graphqlServer = new TestGraphQLServer(); | ||
|
||
graphqlServer.start().then(({ url }) => { | ||
// eslint-disable-next-line no-console | ||
console.log(url); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters