Skip to content

Commit

Permalink
Fixes #29. Use GlobalId for primary key in query arguments
Browse files Browse the repository at this point in the history
Including support for "where" and "includes" arguments (SequelizeJSON)
  • Loading branch information
Glavin001 committed Nov 5, 2017
1 parent cd43d09 commit 26fa053
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/@types/graphql-sequelize/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ declare module "graphql-sequelize-teselagen" {
before?: Function;
after?: Function;
separate?: boolean;
list?: boolean;
}): GraphQLFieldResolver<any, any>;

function sequelizeNodeInterface(sequelize: Sequelize): {
Expand Down
38 changes: 33 additions & 5 deletions src/OperationFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GraphQLFieldConfigMap,
GraphQLFieldConfig,
GraphQLInputFieldConfigMap,
GraphQLFieldResolver,
} from 'graphql';
import * as _ from 'lodash';
import * as camelcase from 'camelcase';
Expand Down Expand Up @@ -172,11 +173,24 @@ export class OperationFactory {
modelType: GraphQLObjectType;
}) {
const findByIdQueryName = queryName(model, 'findById');

const queryArgs = defaultArgs(model);
convertFieldsToGlobalId(model, queryArgs);

const baseResolve = resolver(model, {});
// tslint:disable-next-line:max-func-args
const resolve: GraphQLFieldResolver<any, any> = (source, args, context, info) => {
convertFieldsFromGlobalId(model, args);
if (args.where) {
convertFieldsFromGlobalId(model, args.where);
}
return baseResolve(source, args, context, info);
};

queries[findByIdQueryName] = {
type: modelType,
args: defaultArgs(model),
resolve: resolver(model, {
})
args: queryArgs,
resolve
};
}

Expand All @@ -190,10 +204,24 @@ export class OperationFactory {
queries: Queries;
}) {
const findAllQueryName = queryName(model, 'findAll');
const queryArgs = defaultListArgs(model);

const baseResolve = createNonNullListResolver(resolver(model, { list: true }));
// tslint:disable-next-line:max-func-args
const resolve: GraphQLFieldResolver<any, any> = (source, args, context, info) => {
if (args.where) {
convertFieldsFromGlobalId(model, args.where);
}
if (args.include) {
convertFieldsFromGlobalId(model, args.include);
}
return baseResolve(source, args, context, info);
};

queries[findAllQueryName] = {
type: createNonNullList(modelType),
args: defaultListArgs(model),
resolve: createNonNullListResolver(resolver(model)),
args: queryArgs,
resolve,
};
}

Expand Down

0 comments on commit 26fa053

Please sign in to comment.