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

WIP => [Do Not Merge]: Add new createConversation mutation #6401

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions _schemaV2.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8182,6 +8182,56 @@ union CreateConsignmentInquiryMutationType =
ConsignmentInquiryMutationFailure
| ConsignmentInquiryMutationSuccess

type CreateConversationFailure {
mutationError: GravityMutationError
}

input CreateConversationMutationInput {
clientMutationId: String

# The id of the exchange order.
exchangeOrderId: String

# The email of the user who sent the message.
fromEmail: String!

# The id of the user who sent the message.
fromId: String!

# The name of the user who sent the message.
fromName: String!

# The type of the user who sent the message.
fromType: CreateConversationTypeEnum!

# The initial message in the conversation.
initialMessage: String

# The id of the user who received the message.
toId: String!

# The type of the user who received the message.
toType: CreateConversationTypeEnum!
}

type CreateConversationMutationPayload {
clientMutationId: String
responseOrError: CreateConversationResponseOrError
}

union CreateConversationResponseOrError =
CreateConversationFailure
| CreateConversationSuccess

type CreateConversationSuccess {
conversation: Conversation
}

enum CreateConversationTypeEnum {
PARTNER
USER
}

type CreateFeatureFailure {
mutationError: GravityMutationError
}
Expand Down Expand Up @@ -13784,6 +13834,11 @@ type Mutation {
input: CreateSubmissionMutationInput!
): CreateSubmissionMutationPayload

# Create a conversation.
createConversation(
input: CreateConversationMutationInput!
): CreateConversationMutationPayload

# Create a credit card
createCreditCard(input: CreditCardInput!): CreditCardPayload

Expand Down
5 changes: 5 additions & 0 deletions src/lib/loaders/loaders_with_authentication/impulse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export default (accessToken, _userID, opts) => {
{},
{ method: "POST" }
),
conversationCreateLoader: impulseLoader(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised we didn't have this already - we have a bunch of conversation-related mutations in MP already. Are all conversations currently only created via inquiry creation workflow? I see ConversationService#create_conversation code in Gravity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we never exposed anything!

"conversations",
{},
{ method: "POST" }
),
conversationCreateConversationOrderLoader: impulseLoader(
`conversation_orders`,
{},
Expand Down
206 changes: 206 additions & 0 deletions src/schema/v2/conversation/createConversationMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import {
GraphQLObjectType,
GraphQLUnionType,
GraphQLNonNull,
GraphQLString,
GraphQLEnumType,
GraphQLList,
GraphQLInputObjectType,
} from "graphql"
import { mutationWithClientMutationId } from "graphql-relay"
import { ResolverContext } from "types/graphql"
import {
GravityMutationErrorType,
formatGravityError,
} from "lib/gravityErrorHandler"
import Conversation from "schema/v2/conversation"

type UserType = "Partner" | "User"

interface CreateConversationMutationInputProps {
fromId: string
fromType: UserType
fromName: string
fromEmail: string
toId: string
toType: UserType
toName: string
items: Array<{
itemId: string
itemType: string
}>
}

const SuccessType = new GraphQLObjectType<any, ResolverContext>({
name: "CreateConversationSuccess",
isTypeOf: (data) => data.id,
fields: () => ({
conversation: {
type: Conversation.type,
resolve: (conversation) => conversation,
},
}),
})

const ErrorType = new GraphQLObjectType<any, ResolverContext>({
name: "CreateConversationFailure",
isTypeOf: (data) => data._type === "GravityMutationError",
fields: () => ({
mutationError: {
type: GravityMutationErrorType,
resolve: (err) => err,
},
}),
})

const ResponseOrErrorType = new GraphQLUnionType({
name: "CreateConversationResponseOrError",
types: [SuccessType, ErrorType],
})

const CreateConversationTypeEnum = new GraphQLEnumType({
name: "CreateConversationTypeEnum",
values: {
PARTNER: {
value: "Partner",
},
USER: {
value: "User",
},
},
})

const CreateConversationItemTypeEnum = new GraphQLEnumType({
name: "CreateConversationItemTypeEnum",
values: {
ARTWORK: {
value: "Artwork",
},
},
})

export const createConversationMutation = mutationWithClientMutationId<
CreateConversationMutationInputProps,
any,
ResolverContext
>({
name: "CreateConversationMutation",
description: "Create a conversation.",
inputFields: {
fromId: {
type: new GraphQLNonNull(GraphQLString),
description: "The id of the user who sent the message.",
},
fromType: {
type: new GraphQLNonNull(CreateConversationTypeEnum),
description: "The type of the user who sent the message.",
},
fromName: {
type: new GraphQLNonNull(GraphQLString),
description: "The name of the user who sent the message.",
},
fromEmail: {
type: new GraphQLNonNull(GraphQLString),
description: "The email of the user who sent the message.",
},
toId: {
type: new GraphQLNonNull(GraphQLString),
description: "The id of the user who received the message.",
},
toType: {
type: new GraphQLNonNull(CreateConversationTypeEnum),
description: "The type of the user who received the message.",
},
toName: {
type: new GraphQLNonNull(GraphQLString),
description: "The name of the user who received the message.",
},
exchangeOrderId: {
type: GraphQLString,
description: "The id of the exchange order.",
},
initialMessage: {
type: GraphQLString,
description: "The initial message in the conversation.",
},
items: {
description: "The items in the conversation.",
type: new GraphQLNonNull(
new GraphQLList(
new GraphQLInputObjectType({
name: "CreateConversationItem",
fields: {
itemId: {
type: new GraphQLNonNull(GraphQLString),
description: "The id of the item.",
},
itemType: {
type: new GraphQLNonNull(CreateConversationItemTypeEnum),
description: "The type of the item.",
},
},
})
)
),
},
},
outputFields: {
responseOrError: {
type: ResponseOrErrorType,
resolve: (result) => result,
},
},
mutateAndGetPayload: async (
args,
{ conversationCreateLoader, conversationCreateConversationOrderLoader }
) => {
if (
!conversationCreateLoader ||
!conversationCreateConversationOrderLoader
) {
throw new Error("You need to be signed in to perform this action.")
}

try {
const conversation = await conversationCreateLoader({
from_id: args.fromId,
from_type: args.fromType,
from_name: args.fromName,
from_email: args.fromEmail,
intercepted: false,
to_id: args.toId,
to_type: args.toType,
to_name: args.toName,
exchange_order_id: args.exchangeOrderId,
initial_message: args.initialMessage,
items: args.items.map((item) => {
return {
item_id: item.itemId,
item_type: item.itemType,
}
}),
})

if (args.exchangeOrderId) {
const conversationOrder = await conversationCreateConversationOrderLoader(
{
conversation_id: conversation.id,
exchange_order_id: args.exchangeOrderId,
}
)

return conversationOrder
}

return conversation
} catch (error) {
const formattedErr = formatGravityError(error)

if (formattedErr) {
return { ...formattedErr, _type: "GravityMutationError" }
} else {
throw new Error(error)
}
}
},
})
2 changes: 2 additions & 0 deletions src/schema/v2/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ import { updateViewingRoomSubsectionsMutation } from "./viewingRooms/mutations/u
import { ViewingRoomConnection } from "./viewingRooms"
import { seoExperimentArtists } from "schema/v2/seoExperimentArtists"
import { Collection } from "./collection"
import { createConversationMutation } from "./conversation/createConversationMutation"

const viewingRoomUnstitchedRootField = config.USE_UNSTITCHED_VIEWING_ROOM_SCHEMA
? {
Expand Down Expand Up @@ -453,6 +454,7 @@ export default new GraphQLSchema({
createBidderPosition: BidderPositionMutation,
createCollection: createCollectionMutation,
createConsignmentInquiry: createConsignmentInquiryMutation,
createConversation: createConversationMutation,
createCreditCard: createCreditCardMutation,
createFeature: CreateFeatureMutation,
createFeaturedLink: CreateFeaturedLinkMutation,
Expand Down