Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kbrandwijk/graphql-gateway-tools
A set of tools to help you build a GraphQL Gateway using remote schemas and schema stitching.
https://github.com/kbrandwijk/graphql-gateway-tools
apollo-server graphql graphql-gateway remote-schemas schema-stitching
Last synced: 3 months ago
JSON representation
A set of tools to help you build a GraphQL Gateway using remote schemas and schema stitching.
- Host: GitHub
- URL: https://github.com/kbrandwijk/graphql-gateway-tools
- Owner: kbrandwijk
- License: mit
- Created: 2017-11-07T04:23:27.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-15T03:26:55.000Z (about 7 years ago)
- Last Synced: 2024-10-10T04:07:56.331Z (4 months ago)
- Topics: apollo-server, graphql, graphql-gateway, remote-schemas, schema-stitching
- Language: TypeScript
- Size: 26.4 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## UPDATE: `graphql-gateway-tools` has been abandoned in favor of my new GraphQL Application Framework: [Qewl](https://github.com/kbrandwijk/qewl)
# graphql-gateway-tools
A set of tools to help you build a GraphQL Gateway using remote schemas and schema stitching.
[![https://nodei.co/npm/graphql-gateway-tools.png?downloads=false&downloadRank=true&stars=true](https://nodei.co/npm/graphql-gateway-tools.png?downloads=false&downloadRank=true&stars=true)](https://www.npmjs.com/package/graphql-gateway-tools)
## SchemaGenerator
A higher order helper for creating a merged schema. It makes it easy to compose a schema, by providing methods to add remote schema endpoints, local GraphQL schemas, partial type definitions, and multiple resolver functions.
```ts
const schemaGenerator = new SchemaGenerator()// Create a remoteExecutableSchema by specifying the endpoint address
await schemaGenerator.registerEndpoint({uri: 'http://myendpointaddress/graphql'})// Create a remoteExecutableSchema by specifying endpoint adddress and introspection schema:
await schemaGenerator.registerEndpoint({uri: 'http://myendpointaddress/graphql', introspectionSchema: myIntrospectionSchema})// Create a remoteExecutableSchema by passing in an ApolloLink instance
await schemaGenerator.registerEndpoint({link: myApolloLink})// Create a remoteExecutableSchema that uses an Authorization bearer token
await schemaGenerator.registerEndpoint({uri: 'http://myendpointaddress/graphql', authenticationToken: 'ey.......'})// Add a schema
schemaGenerator.registerSchema(schema: myGraphQLSchema)// Add a type definition
schemaGenerator.registerTypeDefinition(typeDefs: myTypeDefinitionString)
// Add a resolver function
schemaGenerator.registerResolver(resolverFunction: myResolverFunction)// Generate the merged schema
const mySchema = schemaGenerator.generateSchema()
```See the [examples](./examples) folder for a complete example (coming soon).
## addTypeNameField
If you add an Interface Types to your merged schema, you have to manually add the `__typeName` field to your resolvers. This helper function makes it easy to do so.
```ts
// Assuming you have created a remote schema mySchema with types Car and Boatconst schemaGenerator = new SchemaGenerator()
schemaGenerator.registerEndpoint({uri: '...' })const typeDefs = `
interface Verhicle {
maxSpeed: Float
}extend type Car implements Vehicle { }
extend type Boat implements Vehicle { }
extend type Query {
allVehicles: [Verhicle]
}`const allVehiclesresolver = mergeInfo => ({
Query: {
allVehicles: {
async resolve(parent, args, context, info){
const newInfo = addTypeNameField(info)const cars = mergeInfo.delegate('query', 'allCars', args, context, info)
const boats = mergeInfo.delgate('query', 'allBoats', args, context, info)return [...cars, ...boats]
}
}
}
})schemaGenerator.registerTypeDefinition(typeDefs)
schemaGenerator.registerResolver(allVehiclesresolver)const mergedSchema = schemaGenerator.generateSchema()
```## addFields(mergeInfo: MergeInfo, fields: Array)
A generic helper for adding fields to the resolveInfo, by passing in a fieldName, or a complete FieldNode.
```ts
// Inside a resolver
const myField: FieldNode = { kind: 'Field', name: { kind: 'Name', value: 'myField' } }
const anotherField: 'anotherField'addFields(mergeInfo, [myField, anotherField])
```