Skip to content

Commit

Permalink
feat(graphql): support exported schemaHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jan 6, 2021
1 parent 79d831b commit 0df817d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/perfect-books-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/graphql': minor
'@graphql-mesh/types': minor
---

feat(graphql): support exported schemaHeaders
14 changes: 12 additions & 2 deletions packages/handlers/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ export default class GraphQLHandler implements MeshHandler {
});
};
let schema: GraphQLSchema;
const schemaHeadersFactory = getInterpolatedHeadersFactory(this.config.schemaHeaders);
let schemaHeaders =
typeof this.config.schemaHeaders === 'string'
? await loadFromModuleExportExpression(this.config.schemaHeaders)
: this.config.schemaHeaders;
if (typeof schemaHeaders === 'function') {
schemaHeaders = schemaHeaders();
}
if ('then' in schemaHeaders) {
schemaHeaders = await schemaHeaders;
}
const schemaHeadersFactory = getInterpolatedHeadersFactory(schemaHeaders);
const introspectionExecutor: AsyncExecutor = async (params): Promise<any> => {
const { executor } = await getExecutorAndSubscriberForParams(
params,
Expand All @@ -71,7 +81,7 @@ export default class GraphQLHandler implements MeshHandler {
const result = await urlLoader.handleSDLAsync(this.config.introspection, {
customFetch: customFetch as any,
...this.config,
headers: this.config.schemaHeaders,
headers: schemaHeaders,
});
schema = result.schema;
} else if (this.config.cacheIntrospection) {
Expand Down
3 changes: 2 additions & 1 deletion packages/handlers/graphql/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type GraphQLHandler @md {
endpoint: String!
"""
JSON object representing the Headers to add to the runtime of the API calls only for schema introspection
You can also provide `.js` or `.ts` file path that exports schemaHeaders as an object
"""
schemaHeaders: JSON
schemaHeaders: Any
"""
JSON object representing the Headers to add to the runtime of the API calls only for operation during runtime
"""
Expand Down
13 changes: 10 additions & 3 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,16 @@
"description": "A url or file path to your remote GraphQL endpoint.\nIf you provide a path to a code file(js or ts),\nother options will be ignored and the schema exported from the file will be used directly."
},
"schemaHeaders": {
"type": "object",
"properties": {},
"description": "JSON object representing the Headers to add to the runtime of the API calls only for schema introspection"
"anyOf": [
{
"type": "object",
"additionalProperties": true
},
{
"type": "string"
}
],
"description": "JSON object representing the Headers to add to the runtime of the API calls only for schema introspection\nYou can also provide `.js` or `.ts` file path that exports schemaHeaders as an object"
},
"operationHeaders": {
"type": "object",
Expand Down
5 changes: 2 additions & 3 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ export interface GraphQLHandler {
endpoint: string;
/**
* JSON object representing the Headers to add to the runtime of the API calls only for schema introspection
* You can also provide `.js` or `.ts` file path that exports schemaHeaders as an object
*/
schemaHeaders?: {
[k: string]: any;
};
schemaHeaders?: any;
/**
* JSON object representing the Headers to add to the runtime of the API calls only for operation during runtime
*/
Expand Down

0 comments on commit 0df817d

Please sign in to comment.