A repository for assembling named GraphQL types and schemas from modules
OTHER License
A repository for assembling GraphQL types and schemas from modules.
graphql-type-repository
provides a repository for assembling
GraphQL types and schemas for (GraphQL.js)[https://github.com/graphql/graphql-js].
It's goal is to provide additional checking to safely modularise a GraphQL
schema specified in the GraphQL schema language.
Install graphql-type-repository from npm
npm install --save graphql-type-repository graphql-js
or
yarn add graphql-type-repository graphql-js
Note that graphql-js is a peer dependency and must be installed separately.
graphql-type-repository
is tested against node 4, 6 and 7. It uses
(Babel)[https://babeljs.io/] to convert es6 to es6-node.
The npm
branch in this repository is automatically maintained to be the last
commit to master
to pass all tests, in the same form found on npm. It is
recommend to use builds deployed npm for many reasons, but if you want to use
the latest not-yet-released version of graphql-type-repository, you can do so by depending
directly on this branch:
npm install graphql@git://github.com/jamesgorman2/graphql-type-repository.git#npm
We actively welcome pull requests, learn how to contribute.
Changes are tracked as Github releases.
GraphQL is BSD-licensed.
Build steps and CONTRIBUTING.md are modified from (GraphQL.js)[https://github.com/graphql/graphql-js] under the same license.
personModule.js
import {
Module,
} from 'graphql-type-repository';
import personResolvers from './personResolvers';
export default const personModule: Module = new Module('Person')
.withSchema(`
type Name {
firstName: String!
lastName: String!
}
type Person {
id: ID!
name: Name
age: Int
}
type PersonQuery {
getPersonById(id: ID!): Person
}
schema {
query PersonQuery
}`,
personResolvers,
);
propertyModule.js
import {
GraphQLString,
GraphQLObjectType,
} from 'graphql';
import {
Module,
} from 'graphql-type-repository';
import propertyResolvers from './propertyResolvers';
export default const propertyModule: Module = new Module('Property')
.withType(
new GraphQLObjectType({
name: 'Address',
fields: {
street: { type: GraphQLString },
postCode: { type: GraphQLString },
},
})
)
.withSchema(`
type Property {
id: ID!
address: Address
occupants: [Person]
}
extend type Person {
livesAt: Property
}
type PropertyQuery {
getPropertyById(id: ID!): Property
}
schema {
query PropertyQuery
}`,
propertyResolvers,
);
buildSchema.js
import {
TypeRepository,
} from 'graphql-type-repository';
import personModule from './personModule';
import propertyModule from './propertyModule';
export const schema = new TypeRepository()
.add(personModule)
.add(propertyModule)
.getSchema({ query: 'Query' });
type Name {
firstName: String!
lastName: String!
}
type Person {
id: ID!
name: Name
age: Int
livesAt: Property
}
type Address {
street: String
postCode: String
}
type Property {
id: ID!
address: Address
occupants: [Person]
}
type Query {
getPersonById(id: ID!): Person
getPropertyById(id: ID!): Property
}
schema {
query Query
}
const internalRepository: ModuleRepository = new ModuleRepository()
.withModule(personModule)
.withModule(propertyModule);
const parsedTypeGraph: FlattenedTypeGraph = FlattenedTypeGraph.from(internalRepository)
.map(appendSystemTypes)
.map(appendSystemDirectives)
.map(assertNoSystemTypesDefinedByUser)
.map(assertNoSystemDirectivesDefinedByUser)
.map(assertNoModuleRepositoryErrors)
.map(assertNoModuleErrors)
.map(assertNoMissingTypes)
.map(assertNoDuplicateTypes)
.map(assertNoDuplicateExtensionFields)
.map(assertNoDuplicateSchemaFields)
.map(assertNoMissingSchemaResolvers)
.map(assertNoDisconnectedSubgraphs)
.map(assertNoDuplicateDirectives)
.map(assertNoMissingDirectives)
.map(assertNoUnusedDirectives)
.map(generateTypes);
const schema: GraphQLSchema = parsedTypeGraph.getSchema({ query: 'Query' });