-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
436 additions
and
31 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
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,27 @@ | ||
{ | ||
"name": "example-request-cancelation", | ||
"version": "0.0.0", | ||
"description": "", | ||
"author": "Laurin Quast <[email protected]>", | ||
"license": "MIT", | ||
"private": true, | ||
"module": "commonjs", | ||
"keywords": [], | ||
"scripts": { | ||
"check": "tsc --pretty --noEmit", | ||
"dev": "cross-env NODE_ENV=development ts-node-dev --exit-child --respawn src/main.ts", | ||
"start": "ts-node src/main.ts" | ||
}, | ||
"dependencies": { | ||
"graphql": "16.6.0", | ||
"graphql-yoga": "5.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "18.16.16", | ||
"@whatwg-node/fetch": "^0.9.17", | ||
"cross-env": "7.0.3", | ||
"ts-node": "10.9.1", | ||
"ts-node-dev": "2.0.0", | ||
"typescript": "5.1.6" | ||
} | ||
} |
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,61 @@ | ||
import { createServer } from 'node:http'; | ||
import { createLogger, createSchema, createYoga, useExecutionCancelation } from 'graphql-yoga'; | ||
|
||
const logger = createLogger('debug'); | ||
|
||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
user: User | ||
} | ||
type User { | ||
id: ID! | ||
name: String! | ||
bestFriend: User | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
async user(_, __, { request }) { | ||
logger.info('resolving user'); | ||
await new Promise((resolve, reject) => { | ||
const timeout = setTimeout(resolve, 5000); | ||
request.signal.addEventListener('abort', () => { | ||
clearTimeout(timeout); | ||
reject(request.signal.reason); | ||
}); | ||
}); | ||
logger.info('resolved user'); | ||
|
||
return { | ||
id: '1', | ||
name: 'Chewie', | ||
}; | ||
}, | ||
}, | ||
User: { | ||
bestFriend() { | ||
logger.info('resolving user best friend'); | ||
|
||
return { | ||
id: '2', | ||
name: 'Han Solo', | ||
}; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// Provide your schema | ||
const yoga = createYoga({ | ||
plugins: [useExecutionCancelation()], | ||
schema, | ||
logging: logger, | ||
}); | ||
|
||
// Start the server and explore http://localhost:4000/graphql | ||
const server = createServer(yoga); | ||
server.listen(4000, () => { | ||
console.info('Server is running on http://localhost:4000/graphql'); | ||
}); |
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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"module": "commonjs", | ||
"target": "esnext", | ||
"lib": ["esnext"], | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.