codesandbox is here https://codesandbox.io/s/rtk-github-issues-example-03-final-async-su4hz?file=/src/features/issuesList/IssuesListPage.tsx
I've used AppDispatch which suggested https://react-redux.js.org/using-react-redux/static-typing#typing-the-usedispatch-hook and this question How do I resolve 'Property 'type' is missing in type 'AsyncThunkAction' using Redux Toolkit (with TypeScript)?
but transpile is faield.
No overload matches this call.
Overload 1 of 3, '(action: AnyAction): AnyAction', gave the following error.
Argument of type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' is not assignable to parameter of type 'AnyAction'.
Property 'type' is missing in type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' but required in type 'AnyAction'.
Overload 2 of 3, '(asyncAction: ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, null, AnyAction>): Promise<...> & { ...; }', gave the following error.
Argument of type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' is not assignable to parameter of type 'ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, null, AnyAction>'.
Types of parameters 'getState' and 'getState' are incompatible.
Type 'CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>' is missing the following properties from type 'RepoDetailsState': openIssuesCount, error
Overload 3 of 3, '(asyncAction: ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, undefined, AnyAction>): Promise<...> & { ...; }', gave the following error.
Argument of type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' is not assignable to parameter of type
ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, undefined, AnyAction>'.
Types of parameters 'getState' and 'getState' are incompatible.
Type 'CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>' is not assignable to type 'RepoDetailsState'.ts(2769)
I had to use RootState for thunkAPI parameter types in createAsyncThunk function.
{
dispatch: AppDispatch
state: RootState
rejectValue: MyKnownError
}
In my case I got this error because I used the wrong type for dispatch. I used AppDispatch for the thunkAPI param types, but where I tried to use the thunk I explicitly declared the wrong dispatch type. The reverse is surely also possible. Read here about AppDispatch: https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type
Using the right type for RootState like Hiroyuki Shirakawa said is a good thing to look for too.
Read here about createAsyncThunk in TypeScript: https://redux-toolkit.js.org/usage/usage-with-typescript#createasyncthunk
Related
I have very strange behavior in my Nest.js application with dynamoose. This is my location schema:
import { NULL, Schema } from 'dynamoose';
export const LocationSchema = new Schema({
country: String,
city: String,
zipCode: String,
companyName: {
type: [String, NULL],
default: null,
},
streetAddress: {
type: [String, NULL],
default: null,
},
contactPhone: {
type: [String, NULL],
default: null,
},
contactEmail: {
type: [String, NULL],
default: null,
},
});
In body I'm posting this:
"deliveryLocation":{"country":"United Kingdom","zipCode":"M11 9","city":"MANCHESTER AIRPORT"},"pickupLocation":{"country":"Belgium","zipCode":"1020","city":"BRUXELLES/BRUSSEL 2"}
And I got ValidationException: A value provided cannot be converted into a number
As you can see any property has type Number.
What is even more strange if I change in deliveryLocation zipCode to M119 it is valid for DynamoDB.
If I'm changing in deliveryLocation United Kingdom to 'United Kindon' and let zipCode stay 'M11 9' the error is not coming.
If I making request with same values in deliveryLocation and pickupLocation - {"country":"United Kingdom","zipCode":"M11 9","city":"MANCHESTER AIRPORT"} - there is also no error.
To sum up error occurs only in this combination
"deliveryLocation":{"country":"United Kingdom","zipCode":"M11 9","city":"MANCHESTER AIRPORT"},"pickupLocation":{"country":"Belgium","zipCode":"1020","city":"BRUXELLES/BRUSSEL 2"}
Any idea why it is happening?
I tried adding to schema
saveUnknown: true,
but it is not solving the problem.
Using serverless-stack.
I have a table company with multiple branches:
new sst.Table(this, 'Company', {
fields: {
userId: sst.TableFieldType.STRING,
companyId: sst.TableFieldType.STRING,
companyName: sst.TableFieldType.STRING,
branches: [
{
branchId: sst.TableFieldType.STRING,
branchName: sst.TableFieldType.STRING
}
]
},
primaryIndex: {partitionKey: "userId", sortKey: "companyId"}
})
I am trying to add branch to the branches:
const branch = {
branchId: uuid.v1(),
branchName: data.branchName
}
const params = {
TableName: process.env.COMPANY_TABLE_NAME,
Key: {userId: "1", companyId: data.companyId},
UpdateExpression: "ADD #branches :branch",
ExpressionAttributeNames: { "#branches" : "branches" },
ExpressionAttributeValues: { ":branch": [branch] }
}
But I get this error:
ERROR ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND
ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND
ADD is only for numbers and sets. Your branches attribute is a list. So you can use SET with list_append.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.UpdatingListElements
SET #branches = list_append(#branches, :branch) is correct. But ExpressionAttributeValues should be ExpressionAttributeValues: { ":branch": {"L":[branch]}}
You can refer to https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.update_item
I'm trying to migrate my mongoose code/db to dynamoose/dynamodb.
I get the following exception while trying to save custom object:
TypeMismatch: Expected name to be of type object, instead found type object
Here is my sample code on nodejs / typescript:
import dynamoose from "dynamoose";
export = {
run: async() => {
dynamoose.aws.sdk.config.update({
"accessKeyId": "AKID",
"secretAccessKey": "SECRET",
"region": "us-east-1"
});
dynamoose.aws.ddb.local();
const Man = dynamoose.model('Man', {
id: String,
name: {
type: Object,
schema: {
firstname: String,
lastname: String
}
}});
Man.create({
id:"random",
name: new Name("John", "Martin")
});
}
}.run();
class Name {
firstname: string;
lastname: string;
constructor(firstname: string, lastname: string){
this.firstname = firstname;
this.lastname = lastname;
}
}
How can I fix this?
I got it working using this as identified in this issue on Github:
Man.create({
id:"random",
name: { ...new Name("John", "Martin") }
});
Not sure if there is any better way though.
The following code works fine but I'm getting a Flow error for it:
case UPDATE_USER: {
return {
...state,
users: state.users.map((user) => {
if (user.id === action.id) {
return {...user, [action.propName]: action.payload};
} else {
return user;
}
})
};
}
The exact message is this:
<U>(
callbackfn: (
value: User,
index: number,
array: Array<User>
) => U,
thisArg?: any
) => Array<U>
any
Missing type annotation for `U`. `U` is a type parameter declared in function type [1] and was implicitly instantiated at call of method `map` [2].Flow(InferError)
I suspect the problem might be related to the way I've defined my Action types for the Reducer:
type ToggleModalAction = {type: typeof TOGGLE_MODAL};
type CancelRequestAction = {type: typeof CANCEL_REQUEST, payload: boolean};
type UpdateCompanyAction = {type: typeof UPDATE_COMPANY, payload: number};
type ResetStateAction = {type: typeof RESET_STATE};
type AddUserAction = {type: typeof ADD_USER, isDirty: boolean};
type UpdateUserAction = {type: typeof UPDATE_USER, id: number, propName: string, payload: string | number};
type RemoveUserAction = {type: typeof REMOVE_USER, id: number};
I've tried several things to resolve it but none work. Any ideas?
It seems that you need to provide the return value for map
case UPDATE_USER: {
return {
...state,
users: state.users.map((user): User => {
if (user.id === action.id) {
return {...user, [action.propName]: action.payload};
} else {
return user;
}
})
};
}
In GraphiQL at http://localhost:8080/graphiql, I'm using this query:
{
instant_message(fromID: "1"){
fromID
toID
msgText
}
}
I'm getting this response:
{
"data": {
"instant_message": {
"fromID": null,
"toID": null,
"msgText": null
}
},
"errors": [
{
"message": "Resolve function for \"instant_message.fromID\" returned undefined",
"locations": [
{
"line": 3,
"column": 5
}
]
},
{
"message": "Resolve function for \"instant_message.toID\" returned undefined",
"locations": [
{
"line": 4,
"column": 5
}
]
},
{
"message": "Resolve function for \"instant_message.msgText\" returned undefined",
"locations": [
{
"line": 5,
"column": 5
}
]
}
]
}
I tried to set up my system according to the examples found here:
https://medium.com/apollo-stack/tutorial-building-a-graphql-server-cddaa023c035#.s7vjgjkb7
Looking at that article, it doesn't seem to be necessary to set up individual resolvers for string fields, but I must be missing something.
What is the correct way to update my resolvers so as to return results from string fields? Example code would be greatly appreciated!
Thanks very much in advance to all for any thoughts or info.
CONNECTORS
import Sequelize from 'sequelize';
//SQL CONNECTORS
const db = new Sequelize(Meteor.settings.postgres.current_dev_system.dbname, Meteor.settings.postgres.current_dev_system.dbuser, Meteor.settings.postgres.current_dev_system.dbpsd, {
host: 'localhost',
dialect: 'postgres',
});
db
.authenticate()
.then(function(err) {
console.log('Connection to Sequelize has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the Sequelize database:', err);
});
const IMModel = db.define('IM', {
id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
fromID: {type: Sequelize.STRING},
toID: {type: Sequelize.STRING},
msgText: {type: Sequelize.STRING}
});
IMModel.sync({force: true}).then(function () {
// Table created
return IMModel.create({
fromID: '1',
toID: '2',
msgText: 'msg set up via IMModel.create'
});
});
const IM = db.models.IM;
export {db, IM };
SCHEMA
const typeDefinitions = [`
type instant_message {
id: Int
fromID: String
toID: String
msgText: String
}
type Query {
instant_message(fromID: String, toID: String, msgText: String): instant_message
}
type RootMutation {
createInstant_message(
fromID: String!
toID: String!
msgText: String!
): instant_message
}
schema {
query: Query,
mutation: RootMutation
}
`];
export default typeDefinitions;
RESOLVERS
import * as connectors from './db-connectors';
import { Kind } from 'graphql/language';
const b = 100;
const resolvers = {
Query: {
instant_message(_, args) {
const a = 100;
return connectors.IM.find({ where: args });
}
},
RootMutation: {
createInstant_message: (__, args) => { return connectors.IM.create(args); },
},
};
export default resolvers;
When you define your GraphQLObjectTypes you need to provide a resolver for each of their fields.
You defined your instant_message with multiple fields but did not provide resolvers for each of these fields.
More over you defined the types of those field with regular typescript fields while you need to define it with GraphQL types (GraphQLInt, GraphQLString, GrapQLFloat etc..)
So defining your type should look something like this:
let instant_message = new GraphQLObjectType({
id: {
type: GraphQLInt,
resolve: (instantMsg)=> {return instantMsg.id}
}
fromID: {
type: GraphQLString,
resolve: (instantMsg)=> {return instantMsg.fromID}
}
toID: {
type: GraphQLString,
resolve: (instantMsg)=> {return instantMsg.toID}
}
msgText: {
type: GraphQLString,
resolve: (instantMsg)=> {return instantMsg.msgText}
}
})
In addition, you will need to define your Query as follows:
let Query = new GraphQLObjectType({
name: "query",
description: "...",
fields: () => ({
instant_messages: {
type: new GraphQLList(instant_message),
args: {
id: {type: GraphQLInt}
},
resolve: (root, args) => {
connectors.IM.find({ where: args })
}
}
})
})
The issue is that the query does not expect an array,
Please fix it:
type Query {
instant_message(fromID: String, toID: String, msgText: String): [instant_message]
}
Then you should make sure the resolver returns Array of objects, if it doesnt work then the resolver is not returning an Array.