Skip to content

Commit

Permalink
enhance(graphql): use 'source' to align our pattern (#4789)
Browse files Browse the repository at this point in the history
* enhance(graphql): use 'source' to align our pattern

* Update tests

* Go
  • Loading branch information
ardatan authored Nov 7, 2022
1 parent 8c7d96c commit 76deb32
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-cars-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/graphql': minor
'@graphql-mesh/types': minor
---

_BREAKING_ - Local schemas, SDLs and introspections are now provided as `source` instead of `introspection` or `schema`. See the docs for more information.
4 changes: 2 additions & 2 deletions examples/type-merging-batching-example/.meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sources:
- name: AuthorService
handler:
graphql:
schema: ./author-service-schema.ts
source: ./author-service-schema.ts
transforms:
- typeMerging:
queryFields:
Expand All @@ -17,7 +17,7 @@ sources:
- name: BookService
handler:
graphql:
schema: ./book-service-schema.ts
source: ./book-service-schema.ts
transforms:
# Rename type names and field names to let stitching merger merges them
- rename:
Expand Down
8 changes: 4 additions & 4 deletions packages/handlers/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ export default class GraphQLHandler implements MeshHandler {
});

const schemaHeadersFactory = getInterpolatedHeadersFactory(httpSourceConfig.schemaHeaders || {});
if (httpSourceConfig.introspection) {
if (httpSourceConfig.source) {
const headers = schemaHeadersFactory({
env: process.env,
});
const sdlOrIntrospection = await readFileOrUrl<string | IntrospectionQuery | DocumentNode>(
httpSourceConfig.introspection,
httpSourceConfig.source,
{
cwd: this.baseDir,
allowUnknownExtensions: true,
Expand Down Expand Up @@ -170,7 +170,7 @@ export default class GraphQLHandler implements MeshHandler {
}

async getCodeFirstSource({
schema: schemaConfig,
source: schemaConfig,
}: YamlConfig.GraphQLHandlerCodeFirstConfiguration): Promise<MeshSource> {
if (schemaConfig.endsWith('.graphql')) {
const rawSDL = await readFileOrUrl<string>(schemaConfig, {
Expand Down Expand Up @@ -383,7 +383,7 @@ export default class GraphQLHandler implements MeshHandler {
batch: this.config.batch != null ? this.config.batch : true,
contextVariables,
};
} else if ('schema' in this.config) {
} else if ('source' in this.config) {
return this.getCodeFirstSource(this.config);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/handlers/graphql/test/handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('graphql', () => {
const handler = new GraphQLHandler({
name: 'SDLSchema',
config: {
schema: sdlFilePath,
source: sdlFilePath,
},
baseDir: __dirname,
cache: new InMemoryLRUCache(),
Expand All @@ -49,7 +49,7 @@ describe('graphql', () => {
const handler = new GraphQLHandler({
name: 'SDLSchema',
config: {
schema: schemaFilePath,
source: schemaFilePath,
},
baseDir: __dirname,
cache: new InMemoryLRUCache(),
Expand All @@ -74,7 +74,7 @@ describe('graphql', () => {
const handler = new GraphQLHandler({
name: 'SDLSchema',
config: {
schema: schemaFilePath,
source: schemaFilePath,
},
baseDir: __dirname,
cache: new InMemoryLRUCache(),
Expand All @@ -100,7 +100,7 @@ describe('graphql', () => {
const handler = new GraphQLHandler({
name: 'SDLSchema',
config: {
schema: schemaFilePath,
source: schemaFilePath,
},
baseDir: __dirname,
cache: new InMemoryLRUCache(),
Expand Down
6 changes: 3 additions & 3 deletions packages/handlers/graphql/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type GraphQLHandlerCodeFirstConfiguration @md {
If you provide a path to a code file(js or ts),
other options will be ignored and the schema exported from the file will be used directly.
"""
schema: Any!
source: Any!
}

type GraphQLHandlerHTTPConfiguration @md {
Expand Down Expand Up @@ -81,9 +81,9 @@ type GraphQLHandlerHTTPConfiguration @md {
webSocketImpl: String
"""
Path to the introspection
You can separately give schema introspection
You can separately give schema introspection or SDL
"""
introspection: String
source: String
"""
SSE - Server Sent Events
WS - New graphql-ws
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/test/getMesh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('getMesh', () => {
pubsub,
name,
config: {
schema: `./schema${config.suffix}.ts`,
source: `./schema${config.suffix}.ts`,
},
store,
logger,
Expand Down
8 changes: 4 additions & 4 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@
"type": "object",
"title": "GraphQLHandlerCodeFirstConfiguration",
"properties": {
"schema": {
"source": {
"anyOf": [
{
"type": "object",
Expand All @@ -782,7 +782,7 @@
"description": "A file path to your GraphQL Schema\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."
}
},
"required": ["schema"]
"required": ["source"]
},
"GraphQLHandlerHTTPConfiguration": {
"additionalProperties": false,
Expand Down Expand Up @@ -832,9 +832,9 @@
"type": "string",
"description": "Path to a custom W3 Compatible WebSocket Implementation"
},
"introspection": {
"source": {
"type": "string",
"description": "Path to the introspection\nYou can separately give schema introspection"
"description": "Path to the introspection\nYou can separately give schema introspection or SDL"
},
"subscriptionsProtocol": {
"type": "string",
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ export interface GraphQLHandlerHTTPConfiguration {
webSocketImpl?: string;
/**
* Path to the introspection
* You can separately give schema introspection
* You can separately give schema introspection or SDL
*/
introspection?: string;
source?: string;
/**
* SSE - Server Sent Events
* WS - New graphql-ws
Expand All @@ -257,7 +257,7 @@ export interface GraphQLHandlerCodeFirstConfiguration {
* If you provide a path to a code file(js or ts),
* other options will be ignored and the schema exported from the file will be used directly.
*/
schema: any;
source: any;
}
export interface GraphQLHandlerMultipleHTTPConfiguration {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

* `schema` (type: `Any`, required) - A file path to your GraphQL Schema
* `source` (type: `Any`, required) - A file path to your GraphQL Schema
If you provide a path to a code file(js or ts),
other options will be ignored and the schema exported from the file will be used directly.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ other options will be ignored and the schema exported from the file will be used

@default "same-origin"
* `webSocketImpl` (type: `String`) - Path to a custom W3 Compatible WebSocket Implementation
* `introspection` (type: `String`) - Path to the introspection
You can separately give schema introspection
* `source` (type: `String`) - Path to the introspection
You can separately give schema introspection or SDL
* `subscriptionsProtocol` (type: `String (SSE | WS | LEGACY_WS)`) - SSE - Server Sent Events
WS - New graphql-ws
LEGACY_WS - Legacy subscriptions-transport-ws
Expand Down
20 changes: 19 additions & 1 deletion website/src/pages/docs/handlers/graphql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ sources:
Authorization: Bearer {env.MY_API_TOKEN}
```

## Fetching SDL or introspection from CDN or somewhere

Let's say the introspection disabled in your production environment of your GraphQL source, and you want to provide your SDL or introspection seperately;

```yaml filename=".meshrc.yaml"
sources:
- name: MyGraphQLApi
handler:
graphql:
endpoint: https://api.github.com/graphql
# You can provide your SDL or introspection seperately
source: https://docs.github.com/public/schema.docs.graphql
operationHeaders:
Authorization: Bearer {env.GITHUB_TOKEN}
```

> In this case, CLI's `build` command won't save the introspection in the artifacts, so your Mesh won't start if `source` URL is down.

## Local Schemas

We recommend providing local schema by using the `additionalTypeDefs` and `additionalResolvers` configuration options.
Expand All @@ -75,7 +93,7 @@ sources:
- name: MyGraphQLApi
handler:
graphql:
schema: ./my-local-schema.ts
source: ./my-local-schema.ts
```

```ts filename="my-local-schema.ts"
Expand Down

0 comments on commit 76deb32

Please sign in to comment.