This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stitching): allow delegateToSchema, mergeSchemas and transformSc…
…hema to take remote schema configurations as parameters This removes the need for makeRemoteExecutableSchema, removing an unnecessary layer of schema delegation. This change introduces a RemoteGraphQLSchema type which consists of a GraphQLSchema object annotated with link, fetcher, or dispatcher properties that can be used by delegateToSchema to access the remote schema. The dispatcher function takes the graphql context eventually passed to delegateToSchema as an argument and returns a link or fetcher function.
- Loading branch information
Showing
14 changed files
with
373 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
IDelegateToSchemaOptions, | ||
RemoteSchemaExecutionConfig, | ||
Fetcher | ||
} from '../Interfaces'; | ||
import { ApolloLink } from 'apollo-link'; | ||
import { observableToAsyncIterable } from './observableToAsyncIterable'; | ||
import linkToFetcher, { execute } from './linkToFetcher'; | ||
import delegateToSchema from './delegateToSchema'; | ||
|
||
export default function delegateToRemoteSchema( | ||
options: IDelegateToSchemaOptions & RemoteSchemaExecutionConfig | ||
): Promise<any> { | ||
if (options.operation === 'query' || options.operation === 'mutation') { | ||
let fetcher: Fetcher; | ||
if (options.dispatcher) { | ||
const dynamicLinkOrFetcher = options.dispatcher(context); | ||
fetcher = (typeof dynamicLinkOrFetcher === 'function') ? | ||
dynamicLinkOrFetcher : | ||
linkToFetcher(dynamicLinkOrFetcher); | ||
} else if (options.link) { | ||
fetcher = linkToFetcher(options.link); | ||
} else { | ||
fetcher = options.fetcher; | ||
} | ||
|
||
if (!options.executor) { | ||
options.executor = ({ document, context, variables }) => fetcher({ | ||
query: document, | ||
variables, | ||
context: { graphqlContext: context } | ||
}); | ||
} | ||
|
||
return delegateToSchema(options); | ||
} | ||
|
||
if (options.operation === 'subscription') { | ||
let link: ApolloLink; | ||
if (options.dispatcher) { | ||
link = options.dispatcher(context) as ApolloLink; | ||
} else { | ||
link = options.link; | ||
} | ||
|
||
if (!options.subscriber) { | ||
options.subscriber = ({ document, context, variables }) => { | ||
const operation = { | ||
query: document, | ||
variables, | ||
context: { graphqlContext: context } | ||
}; | ||
const observable = execute(link, operation); | ||
return observableToAsyncIterable(observable); | ||
}; | ||
} | ||
|
||
return delegateToSchema(options); | ||
} | ||
} |
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
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
Oops, something went wrong.