Skip to content

Commit

Permalink
initial subscription commit. Makes "subscription" behave like "query"
Browse files Browse the repository at this point in the history
  • Loading branch information
skevy committed Sep 6, 2015
1 parent 3bc93a7 commit 8a589f6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/execution/execute.js
Expand Up @@ -235,9 +235,18 @@ function getOperationRootType(
);
}
return mutationType;
case 'subscription':
var subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError(
'Schema is not configured for subscriptions',
[ operation ]
);
}
return subscriptionType;
default:
throw new GraphQLError(
'Can only execute queries and mutations',
'Can only execute queries, mutations and subscriptions',
[ operation ]
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/type/__tests__/introspection.js
Expand Up @@ -1190,6 +1190,11 @@ describe('Introspection', () => {
description: 'If this server supports mutation, the type that ' +
'mutation operations will be rooted at.'
},
{
name: 'subscriptionType',
description: 'If this server support subscription, the type ' +
'that subscription operations will be rooted at.',
},
{
name: 'directives',
description: 'A list of all directives supported by this server.'
Expand Down
6 changes: 6 additions & 0 deletions src/type/introspection.js
Expand Up @@ -51,6 +51,12 @@ export var __Schema = new GraphQLObjectType({
type: __Type,
resolve: schema => schema.getMutationType()
},
subscriptionType: {
description: 'If this server support subscription, the type that ' +
'subscription operations will be rooted at.',
type: __Type,
resolve: schema => schema.getSubscriptionType()
},
directives: {
description: 'A list of all directives supported by this server.',
type:
Expand Down
11 changes: 11 additions & 0 deletions src/type/schema.js
Expand Up @@ -58,12 +58,18 @@ export class GraphQLSchema {
`Schema mutation must be Object Type if provided but ` +
`got: ${config.mutation}.`
);
invariant(
!config.subscription || config.subscription instanceof GraphQLObjectType,
`Schema subscription must be Object Type if provided but ` +
`got: ${config.subscription}.`
);
this._schemaConfig = config;

// Build type map now to detect any errors within this schema.
this._typeMap = [
this.getQueryType(),
this.getMutationType(),
this.getSubscriptionType(),
__Schema
].reduce(typeMapReducer, {});

Expand All @@ -86,6 +92,10 @@ export class GraphQLSchema {
return this._schemaConfig.mutation;
}

getSubscriptionType(): ?GraphQLObjectType {
return this._schemaConfig.subscription;
}

getTypeMap(): TypeMap {
return this._typeMap;
}
Expand All @@ -111,6 +121,7 @@ type TypeMap = { [typeName: string]: GraphQLType }
type GraphQLSchemaConfig = {
query: GraphQLObjectType;
mutation?: ?GraphQLObjectType;
subscription?: ?GraphQLObjectType;
}

function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/TypeInfo.js
Expand Up @@ -127,6 +127,8 @@ export class TypeInfo {
type = schema.getQueryType();
} else if (node.operation === 'mutation') {
type = schema.getMutationType();
} else if (node.operation === 'subscription') {
type = schema.getSubscriptionType();
}
this._typeStack.push(type);
break;
Expand Down
1 change: 1 addition & 0 deletions src/utilities/__tests__/schemaPrinter.js
Expand Up @@ -544,6 +544,7 @@ type __Schema {
types: [__Type!]!
queryType: __Type!
mutationType: __Type
subscriptionType: __Type
directives: [__Directive!]!
}
Expand Down
29 changes: 20 additions & 9 deletions src/utilities/buildASTSchema.js
Expand Up @@ -88,7 +88,8 @@ function getInnerTypeName(typeAST) {
export function buildASTSchema(
ast: Document,
queryTypeName: string,
mutationTypeName: ?string
mutationTypeName: ?string,
subscriptionTypeName: ?string
): GraphQLSchema {

if (isNullish(ast)) {
Expand Down Expand Up @@ -121,6 +122,12 @@ export function buildASTSchema(
' not found in document.');
}

if (!isNullish(subscriptionTypeName) &&
isNullish(astMap[subscriptionTypeName])) {
throw new Error('Specified subscription type ' + subscriptionTypeName +
' not found in document.');
}

/**
* This generates a function that allows you to produce
* type definitions on demand. We produce the function
Expand Down Expand Up @@ -161,16 +168,20 @@ export function buildASTSchema(
ast.definitions.forEach(produceTypeDef);

var queryType = produceTypeDef(astMap[queryTypeName]);
var schema;
if (isNullish(mutationTypeName)) {
schema = new GraphQLSchema({ query: queryType });
} else {
schema = new GraphQLSchema({
query: queryType,
mutation: produceTypeDef(astMap[mutationTypeName]),
});

var schemaBody = {
query: queryType
};

if (!isNullish(mutationTypeName)) {
schemaBody.mutation = produceTypeDef(astMap[mutationTypeName]);
}

if (!isNullish(subscriptionTypeName)) {
schemaBody.subscription = produceTypeDef(astMap[subscriptionTypeName]);
}

var schema = new GraphQLSchema(schemaBody);
return schema;

function makeSchemaDef(def) {
Expand Down
8 changes: 6 additions & 2 deletions src/utilities/buildClientSchema.js
Expand Up @@ -312,16 +312,20 @@ export function buildClientSchema(
typeIntrospection => getNamedType(typeIntrospection.name)
);

// Get the root Query and Mutation types.
// Get the root Query, Mutation, and Subscription types.
var queryType = getType(schemaIntrospection.queryType);
var mutationType = schemaIntrospection.mutationType ?
getType(schemaIntrospection.mutationType) :
null;
var subscriptionType = schemaIntrospection.subscriptionType ?
getType(schemaIntrospection.subscriptionType) :
null;

// Then produce and return a Schema with these types.
var schema = new GraphQLSchema({
query: (queryType: any),
mutation: (mutationType: any)
mutation: (mutationType: any),
subscription: (subscriptionType: any)
});

return schema;
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/introspectionQuery.js
Expand Up @@ -13,6 +13,7 @@ export var introspectionQuery = `
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
Expand Down Expand Up @@ -94,6 +95,7 @@ export type IntrospectionQuery = {
export type IntrospectionSchema = {
queryType: IntrospectionNamedTypeRef;
mutationType: ?IntrospectionNamedTypeRef;
subscriptionType: ?IntrospectionNamedTypeRef;
types: Array<IntrospectionType>;
directives: Array<IntrospectionDirective>;
}
Expand Down

0 comments on commit 8a589f6

Please sign in to comment.