Replies: 1 comment 5 replies
-
Hi! Thank you for reaching out and sorry for the response delay here. I'm not sure I understand your question. What I understood is you have a graphql-yoga server with a SOFA plugin which exposes your schema as a REST endpoint. And now you want to forward some headers from the request made on the REST endpoint to the APIs you are calling in your resolvers ? If this is the case, in Yoga, you always have access to the underlying HTTP request in the GraphQL context. So you can probably do something like this to forward all headers: import { createSchema, createYoga } from 'graphql-yoga';
const schema = createSchema({
typeDefs: /* GraphQL */`
type Query {
hello: String
}
`,
resolvers: {
hello: (_parent, _args, { request }) => {
const response = await fetch('downstream-api.com/some/url', {
// Keep in mind that ALL headers will be forwarded, you probably want to filter some headers out
headers: request.headers
})
return response.text()
}
}
})
const yoga = createYoga({
schema,
plugins: [
useSofa({
basePath: '/rest',
}),
],
}) You can even make a custom "fetcher" which already have all headers set to avoid duplicating this everywhere in your resolvers: import { createSchema, createYoga } from 'graphql-yoga';
const schema = createSchema({
typeDefs: /* GraphQL */`
type Query {
hello: String
}
`,
resolvers: {
hello: (_parent, _args, { fetchAPI }) => {
const response = await fetchAPI('/some/url')
return response.text()
}
}
})
const baseApiUrl = 'your-downstream-api.com/v1'
const yoga = createYoga({
schema,
context: ({ request }) => ({
fetchAPI: (url: string, options: RequestInit) => fetch(
`${baseApiUrl}${url}`,
{
headers: request.headers,
...options
},
)
}),
plugins: [
useSofa({
basePath: '/rest',
}),
],
}) |
Beta Was this translation helpful? Give feedback.
-
I'm currently call SOFA REST endpoint and providing additional HTTP headers (authorisation etc), and I want to attached those headers to downstream http sources. Do you know how to achieve this please?
By using "headers" in graphql queries its fine
Beta Was this translation helpful? Give feedback.
All reactions