Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): Fix resolver types not reflecting the fact that you can provide fields instead of declarations for each mutation. And cleanup a few warnings. #84

Merged
merged 14 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"javascript.validate.enable": false,
"workbench.editor.enablePreview": false,
"workbench.editor.enablePreviewFromQuickOpen": false,
"workbench.editor.showTabs": true,
"workbench.editor.showTabs": "multiple",
"typescript.surveys.enabled": false,
"editor.quickSuggestions": {
"other": true,
Expand All @@ -33,8 +33,8 @@
"editor.formatOnSave": true
},
"editor.codeActionsOnSave": {
"source.fixAll.tslint": true,
"source.fixAll.eslint": true
"source.fixAll.tslint": "explicit",
"source.fixAll.eslint": "explicit"
},
"eslint.validate": [
"javascript",
Expand Down
10 changes: 6 additions & 4 deletions lib/root/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ function generateSubscriptions(graphqlSchemaDeclaration, types, pubSubInstance)
// ex: name = "userUpdated"
var name_1 = "".concat(modelName).concat(capitalizeFirstLetter(action), "d");
var configuration = declaration[action];
var subscriptionFunction = function () { return true; };
if (typeof configuration !== 'undefined' &&
Object.prototype.hasOwnProperty.call(configuration, 'subscriptionFilter')) {
subscriptionFunction = configuration.subscriptionFilter;
}
subscriptions[name_1] = {
type: outputType,
args: {
id: { type: graphql_1.GraphQLInt }
},
subscribe: (0, graphql_subscriptions_1.withFilter)(function () { return pubSubInstance.asyncIterator(name_1); }, typeof configuration !== 'undefined' &&
configuration.subscriptionFilter
? configuration.subscriptionFilter
: function () { return true; })
subscribe: (0, graphql_subscriptions_1.withFilter)(function () { return pubSubInstance.asyncIterator(name_1); }, subscriptionFunction)
};
}
});
Expand Down
5 changes: 2 additions & 3 deletions src/associations/field.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
GraphQLInt,
GraphQLList,
GraphQLType,
GraphQLScalarType,
GraphQLString,
GraphQLInt,
GraphQLType,
} from 'graphql'
import { GraphQLField } from 'graphql/type'
import { Association } from 'sequelize/types'
import { injectAssociations } from '..'
import { OutputTypes } from '../../types'
Expand Down
2 changes: 1 addition & 1 deletion src/root/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLObjectType, GraphQLInt, GraphQLFieldConfig } from 'graphql'
import { GraphQLFieldConfig, GraphQLInt, GraphQLObjectType } from 'graphql'
import { defaultArgs, defaultListArgs } from 'graphql-sequelize'
import {
GlobalPreCallback,
Expand Down
31 changes: 26 additions & 5 deletions src/root/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { GraphQLInt, GraphQLObjectType } from 'graphql'
import { PubSub, withFilter } from 'graphql-subscriptions'
import { GraphqlSchemaDeclarationType, EventList } from '../../types'
import {
CreateFieldDeclarationType,
DeleteFieldDeclarationType,
EventList,
GraphqlSchemaDeclarationType,
UpdateFieldDeclarationType,
} from '../../types'
import { isGraphqlFieldDeclaration } from '../isGraphqlFieldDeclaration'

const availableActions: EventList = ['create', 'update', 'delete']
Expand Down Expand Up @@ -34,17 +40,32 @@ export default function generateSubscriptions(
// ex: name = "userUpdated"
const name = `${modelName}${capitalizeFirstLetter(action)}d`
const configuration = declaration[action]

let subscriptionFunction: any = () => true

if (
typeof configuration !== 'undefined' &&
Object.prototype.hasOwnProperty.call(
configuration,
'subscriptionFilter'
)
) {
subscriptionFunction = (
configuration as
| CreateFieldDeclarationType
| DeleteFieldDeclarationType
| UpdateFieldDeclarationType
).subscriptionFilter
}

subscriptions[name] = {
type: outputType,
args: {
id: { type: GraphQLInt },
},
subscribe: withFilter(
() => pubSubInstance.asyncIterator(name),
typeof configuration !== 'undefined' &&
configuration.subscriptionFilter
? configuration.subscriptionFilter
: () => true
subscriptionFunction
),
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/tests/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ const {
} = require('graphql')
const { PubSub } = require('graphql-subscriptions')
const { resolver, defaultListArgs } = require('graphql-sequelize')
const { createContext, EXPECTED_OPTIONS_KEY } = require('dataloader-sequelize')
const { EXPECTED_OPTIONS_KEY } = require('dataloader-sequelize')
const { WebSocketServer } = require('ws')
const { useServer } = require('graphql-ws/lib/use/ws')

const { Op } = require('sequelize')
const {
Expand Down
72 changes: 42 additions & 30 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
Model,
Sequelize,
BuildOptions,
FindOptions,
Association,
} from 'sequelize/types'
import {
GraphQLScalarType,
GraphQLNonNull,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLType,
GraphQLFieldConfig,
GraphQLFieldResolver,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLType,
} from 'graphql'
import {
Association,
BuildOptions,
FindOptions,
Model,
Sequelize,
} from 'sequelize/types'

export type Action = 'list' | 'create' | 'delete' | 'update' | 'count'
export type ActionList = Array<Action>
Expand Down Expand Up @@ -152,6 +152,27 @@ export type GraphqlSchemaDeclarationType = {
[key: string]: ModelDeclarationType | GraphQLFieldConfig<any, any, any>
}

export type CreateFieldDeclarationType = {
extraArg?: ExtraArg
before?: MutationBeforeHook
after?: CreateAfterHook
subscriptionFilter?: SubscriptionFilterHook
preventDuplicateOnAttributes?: string[]
}

export type UpdateFieldDeclarationType = {
extraArg?: ExtraArg
before?: MutationBeforeHook
after?: UpdateAfterHook
subscriptionFilter?: SubscriptionFilterHook
}

export type DeleteFieldDeclarationType = {
before?: DeleteBeforeHook
after?: DeleteAfterHook
subscriptionFilter?: SubscriptionFilterHook
}

export type ModelDeclarationType = {
model: SequelizeModel
actions?: ActionList
Expand All @@ -168,29 +189,20 @@ export type ModelDeclarationType = {
after?: ListAfterHook
resolver?: GraphQLFieldResolver<TSource, TArgs, TContext>
}
create?: {
extraArg?: ExtraArg
before?: MutationBeforeHook
after?: CreateAfterHook
subscriptionFilter?: SubscriptionFilterHook
preventDuplicateOnAttributes?: string[]
}
update?: {
extraArg?: ExtraArg
before?: MutationBeforeHook
after?: UpdateAfterHook
subscriptionFilter?: SubscriptionFilterHook
}
delete?: {
before?: DeleteBeforeHook
after?: DeleteAfterHook
subscriptionFilter?: SubscriptionFilterHook
}
count?: {
extraArg?: ExtraArg
before?: QueryBeforeHook
resolver?: GraphQLFieldResolver<TSource, TArgs, TContext>
}
create?:
| CreateFieldDeclarationType
| GraphQLFieldConfig<TSource, TContext, TArgs>
update?:
| UpdateFieldDeclarationType
| GraphQLFieldConfig<TSource, TContext, TArgs>
delete?:
| DeleteFieldDeclarationType
| GraphQLFieldConfig<TSource, TContext, TArgs>
}

export type SequelizeModels = { [key: string]: SequelizeModel } & {
Expand Down
Loading