Skip to content

Commit

Permalink
fix(graphql): fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer committed Oct 9, 2016
1 parent 4b99a52 commit dd75e37
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLFieldConfig, GraphQLNonNull, GraphQLID, GraphQLArgumentConfig } from 'graphql'
import { Collection, CollectionKey, ObjectType } from '../../../interface'
import { Context, Collection, CollectionKey, ObjectType } from '../../../interface'
import { formatName, idSerde, buildObject } from '../../utils'
import BuildToken from '../BuildToken'
import getType from '../getType'
Expand Down Expand Up @@ -78,6 +78,9 @@ function createCollectionPrimaryKeyField <TKey>(
},

async resolve (source, args, context): Promise<ObjectType.Value | null> {
if (!(context instanceof Context))
throw new Error('GraphQL context must be an instance of `Context`.')

const { name, key } = idSerde.deserialize(args[options.nodeIdFieldName] as string)

if (name !== collection.name)
Expand Down Expand Up @@ -119,6 +122,9 @@ function createCollectionKeyField <TKey>(
)
),
async resolve (source, args, context): Promise<ObjectType.Value | null> {
if (!(context instanceof Context))
throw new Error('GraphQL context must be an instance of `Context`.')

if (!keyType.isTypeOf(args))
throw new Error('The provided arguments are not the correct type.')

Expand All @@ -139,6 +145,9 @@ function createCollectionKeyField <TKey>(
},
},
async resolve (source, args, context): Promise<ObjectType.Value | null> {
if (!(context instanceof Context))
throw new Error('GraphQL context must be an instance of `Context`.')

const key: mixed = args['input']

if (!keyType.isTypeOf(key))
Expand Down
5 changes: 4 additions & 1 deletion src/graphql/schema/collection/getCollectionType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLObjectType, GraphQLFieldConfig, GraphQLNonNull, GraphQLID, GraphQLOutputType } from 'graphql'
import { Collection, ObjectType, Relation } from '../../../interface'
import { Context, Collection, ObjectType, Relation } from '../../../interface'
import { memoize2, formatName, buildObject, idSerde } from '../../utils'
import getNodeInterfaceType from '../node/getNodeInterfaceType'
import getType from '../getType'
Expand Down Expand Up @@ -91,6 +91,9 @@ function createCollectionType (buildToken: BuildToken, collection: Collection):
type: getCollectionType(buildToken, headCollection),

async resolve (value, args, context): Promise<ObjectType.Value | undefined> {
if (!(context instanceof Context))
throw new Error('GraphQL context must be an instance of `Context`.')

const key = relation.getHeadKeyFromTailValue(value)
const headValue = await headCollectionKey.read!(context, key)

Expand Down
12 changes: 10 additions & 2 deletions src/graphql/schema/createConnectionField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
Kind,
} from 'graphql'

import { Paginator, Condition, Type } from '../../interface'
import { Context, Paginator, Condition, Type } from '../../interface'
import { buildObject, formatName, memoize2 } from '../utils'
import getType from './getType'
import BuildToken from './BuildToken'
Expand Down Expand Up @@ -83,6 +83,9 @@ export default function createConnectionField <TValue, TOrdering extends Paginat
context: mixed,
info: GraphQLResolveInfo<mixed, mixed>,
): Promise<Connection<TValue, TOrdering, TCursor>> {
if (!(context instanceof Context))
throw new Error('GraphQL context must be an instance of `Context`.')

const {
orderBy: ordering = paginator.defaultOrdering,
before: beforeCursor,
Expand Down Expand Up @@ -163,7 +166,12 @@ export function _createConnectionType <TValue, TOrdering extends Paginator.Order
},
totalCount: {
type: GraphQLInt,
resolve: ({ paginator, condition }, args, context) => paginator.count(context, condition),
resolve: ({ paginator, condition }, args, context) => {
if (!(context instanceof Context))
throw new Error('GraphQL context must be an instance of `Context`.')

return paginator.count(context, condition)
},
// TODO: description
},
edges: {
Expand Down
12 changes: 8 additions & 4 deletions src/graphql/schema/node/createNodeFieldEntry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GraphQLFieldConfig, GraphQLNonNull, GraphQLID } from 'graphql'
import { Context } from '../../../interface'
import idSerde from '../../utils/idSerde'
import BuildToken from '../BuildToken'
import getNodeInterfaceType from './getNodeInterfaceType'
Expand All @@ -15,7 +16,10 @@ export default function createNodeFieldEntry (buildToken: BuildToken): [string,
type: new GraphQLNonNull(GraphQLID),
},
},
resolve (source, args) {
resolve (source, args, context) {
if (!(context instanceof Context))
throw new Error('GraphQL context must be an instance of `Context`.')

const { name, key } = idSerde.deserialize(args[options.nodeIdFieldName])
const collection = inventory.getCollection(name)

Expand All @@ -24,10 +28,10 @@ export default function createNodeFieldEntry (buildToken: BuildToken): [string,

const primaryKey = collection.primaryKey

if (!primaryKey)
throw new Error(`Invalid id, no primary key on collection named '${name}'.`)
if (!primaryKey || !primaryKey.read)
throw new Error(`Invalid id, no readable primary key on collection named '${name}'.`)

return primaryKey.read(key)
return primaryKey.read(context, key)
},
}]
}
Expand Down
2 changes: 1 addition & 1 deletion src/interface/collection/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface Collection {
* the same `Type` as the `Collection`. This isn鈥檛 a hard requirement and
* things might work fine if they鈥檙e different, but it may not work forever.
*/
readonly paginator?: Paginator<ObjectType.Value, mixed> | null
readonly paginator?: Paginator<ObjectType.Value, Paginator.Ordering, mixed> | null

/**
* Creates a value in our collection. Returns the newly created value.
Expand Down

0 comments on commit dd75e37

Please sign in to comment.