-
how to pass request and responce values to mutation and query using context |
Beta Was this translation helpful? Give feedback.
Answered by
ardatan
Jan 26, 2023
Replies: 2 comments 2 replies
-
Context already has them; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ardatan
-
By default request and response passed as third argument in every query and mutation resolvers = {
Query: {
hello: (parent, { name },rootValue,context) => {
console.log(rootValue)
return `hello ${name || "world"}`
},
},
console:
{
req: IncomingMessage {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [],
flowing: true,
ended: true,
endEmitted: true,
reading: false,
constructed: true,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
destroyed: true,
errored: null,
closed: true,
closeEmitted: true,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: true,
dataEmitted: true,
decoder: null,
encoding: null,
[Symbol(kPaused)]: false
},
_events: [Object: null prototype] {
end: [Array],
error: [Function (anonymous)]
},
_eventsCount: 2,
_maxListeners: undefined,
socket: Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: [Server],
_server: [Server],
parser: [HTTPParser],
on: [Function: socketListenerWrap],
addListener: [Function: socketListenerWrap],
prependListener: [Function: socketListenerWrap],
setEncoding: [Function: socketSetEncoding],
_paused: false,
_httpMessage: [ServerResponse],
timeout: 0,
[Symbol(async_id_symbol)]: 3391,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: true,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
},
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
rawHeaders: [
'Host',
'localhost:3000',
'Connection',
'keep-alive',
'Content-Length',
'120',
'sec-ch-ua',
'"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
'accept',
'application/graphql-response+json, application/json, multipart/mixed',
'content-type',
'application/json',
'sec-ch-ua-mobile',
'?0',
'User-Agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'sec-ch-ua-platform',
'"Windows"',
'Origin',
'http://localhost:3000',
'Sec-Fetch-Site',
'same-origin',
'Sec-Fetch-Mode',
'cors',
'Sec-Fetch-Dest',
'empty',
'Referer',
'http://localhost:3000/api/graphql?query=query+hello%28%24name%3A+String%29%7B%0A++hello%28name%3A+%24name%29%0A%7D',
'Accept-Encoding',
'gzip, deflate, br',
'Accept-Language',
'en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7'
],
rawTrailers: [],
aborted: false,
upgrade: false,
url: '/api/graphql',
method: 'POST',
statusCode: null,
statusMessage: null,
client: Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: [Server],
_server: [Server],
parser: [HTTPParser],
on: [Function: socketListenerWrap],
addListener: [Function: socketListenerWrap],
prependListener: [Function: socketListenerWrap],
setEncoding: [Function: socketSetEncoding],
_paused: false,
_httpMessage: [ServerResponse],
timeout: 0,
[Symbol(async_id_symbol)]: 3391,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: true,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
},
_consuming: false,
_dumped: false,
cookies: [Getter/Setter],
query: {},
previewData: [Getter/Setter],
preview: [Getter/Setter],
body: {
query: 'query hello($name: String) {\n hello(name: $name)\n}',
variables: {},
operationName: 'hello',
extensions: {}
},
[Symbol(kCapture)]: false,
[Symbol(kHeaders)]: {
host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '120',
'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
accept: 'application/graphql-response+json, application/json, multipart/mixed',
'content-type': 'application/json',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'sec-ch-ua-platform': '"Windows"',
origin: 'http://localhost:3000',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
referer: 'http://localhost:3000/api/graphql?query=query+hello%28%24name%3A+String%29%7B%0A++hello%28name%3A+%24name%29%0A%7D',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7'
},
[Symbol(kHeadersCount)]: 32,
[Symbol(kTrailers)]: null,
[Symbol(kTrailersCount)]: 0,
[Symbol(RequestTimeout)]: undefined,
[Symbol(NextRequestMeta)]: {
__NEXT_INIT_URL: 'http://localhost:3000/api/graphql',
__NEXT_INIT_QUERY: {},
_protocol: 'http',
__NEXT_CLONABLE_BODY: [Object],
__nextHadTrailingSlash: false,
__nextIsLocaleDomain: false
}
},
res: <ref *1> ServerResponse {
_events: [Object: null prototype] {
finish: [Function: bound resOnFinish],
pipe: [Function]
},
_eventsCount: 2,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
maxRequestsOnConnectionReached: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: true,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: false,
_closed: false,
socket: Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: [Server],
_server: [Server],
parser: [HTTPParser],
on: [Function: socketListenerWrap],
addListener: [Function: socketListenerWrap],
prependListener: [Function: socketListenerWrap],
setEncoding: [Function: socketSetEncoding],
_paused: false,
_httpMessage: [Circular *1],
timeout: 0,
[Symbol(async_id_symbol)]: 3391,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: true,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
},
_header: null,
_keepAliveTimeout: 5000,
_onPendingData: [Function: bound updateOutgoingData],
req: IncomingMessage {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
socket: [Socket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
rawHeaders: [Array],
rawTrailers: [],
aborted: false,
upgrade: false,
url: '/api/graphql',
method: 'POST',
statusCode: null,
statusMessage: null,
client: [Socket],
_consuming: false,
_dumped: false,
cookies: [Getter/Setter],
query: {},
previewData: [Getter/Setter],
preview: [Getter/Setter],
body: [Object],
[Symbol(kCapture)]: false,
[Symbol(kHeaders)]: [Object],
[Symbol(kHeadersCount)]: 32,
[Symbol(kTrailers)]: null,
[Symbol(kTrailersCount)]: 0,
[Symbol(RequestTimeout)]: undefined,
[Symbol(NextRequestMeta)]: [Object]
},
_sent100: false,
_expect_continue: false,
setHeader: [Function (anonymous)],
statusCode: 200,
flush: [Function: flush],
write: [Function (anonymous)],
end: [Function (anonymous)],
on: [Function: on],
writeHead: [Function: writeHead],
status: [Function (anonymous)],
send: [Function (anonymous)],
json: [Function (anonymous)],
redirect: [Function (anonymous)],
setPreviewData: [Function (anonymous)],
clearPreviewData: [Function (anonymous)],
revalidate: [Function (anonymous)],
unstable_revalidate: [Function (anonymous)],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: null
},
waitUntil: [Function: waitUntil],
request: Request {
size: 0,
timeout: 0,
follow: 20,
compress: true,
counter: 0,
agent: undefined,
formData: [Function: formData],
[Symbol(Body internals)]: { body: null, disturbed: false, error: null },
[Symbol(Request internals)]: {
method: 'POST',
redirect: 'follow',
headers: [Headers],
parsedURL: [Url],
signal: null
}
},
params: {
query: 'query hello($name: String) {\n hello(name: $name)\n}',
variables: {},
operationName: 'hello',
extensions: {}
},
[Symbol(rawDocument)]: 'query hello($name: String) {\n hello(name: $name)\n}'
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context already has them;
https://the-guild.dev/graphql/yoga-server/docs/features/context#server-context